Overview

Namespaces

  • rsanchez
    • Deep
      • App
        • EE
        • Laravel
          • Facade
      • Collection
      • Hydrator
      • Model
      • Plugin
      • Relations
      • Repository

Classes

  • AbstractHydrator
  • AssetsHydrator
  • DateHydrator
  • ExplodeHydrator
  • FileHydrator
  • GridHydrator
  • HydratorFactory
  • MatrixHydrator
  • ParentsHydrator
  • PipeHydrator
  • PlayaHydrator
  • RelationshipHydrator
  • SiblingsHydrator
  • WysiwygHydrator

Interfaces

  • HydratorInterface
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * Deep
  5:  *
  6:  * @package      rsanchez\Deep
  7:  * @author       Rob Sanchez <info@robsanchez.com>
  8:  */
  9: 
 10: namespace rsanchez\Deep\Hydrator;
 11: 
 12: use Illuminate\Database\Eloquent\Model;
 13: use Illuminate\Database\Eloquent\Collection;
 14: use rsanchez\Deep\Collection\EntryCollection;
 15: use rsanchez\Deep\Model\AbstractProperty;
 16: use rsanchez\Deep\Model\AbstractEntity;
 17: use rsanchez\Deep\Hydrator\AbstractHydrator;
 18: use rsanchez\Deep\Repository\SiteRepository;
 19: use rsanchez\Deep\Repository\UploadPrefRepositoryInterface;
 20: 
 21: /**
 22:  * Hydrator for the WYSIWYG fields
 23:  */
 24: class WysiwygHydrator extends AbstractHydrator
 25: {
 26:     /**
 27:      * UploadPref model repository
 28:      * @var \rsanchez\Deep\Repository\SiteRepository
 29:      */
 30:     protected $siteRepository;
 31: 
 32:     /**
 33:      * UploadPref model repository
 34:      * @var \rsanchez\Deep\Repository\UploadPrefRepositoryInterface
 35:      */
 36:     protected $uploadPrefRepository;
 37: 
 38:     /**
 39:      * {@inheritdoc}
 40:      *
 41:      * @param \rsanchez\Deep\Collection\EntryCollection               $collection
 42:      * @param string                                                  $fieldtype
 43:      * @param \rsanchez\Deep\Repository\SiteRepository                $siteRepository
 44:      * @param \rsanchez\Deep\Repository\UploadPrefRepositoryInterface $uploadPrefRepository
 45:      */
 46:     public function __construct(EntryCollection $collection, $fieldtype, SiteRepository $siteRepository, UploadPrefRepositoryInterface $uploadPrefRepository)
 47:     {
 48:         parent::__construct($collection, $fieldtype);
 49: 
 50:         $this->siteRepository = $siteRepository;
 51: 
 52:         $this->uploadPrefRepository = $uploadPrefRepository;
 53:     }
 54: 
 55:     /**
 56:      * {@inheritdoc}
 57:      */
 58:     public function hydrate(AbstractEntity $entity, AbstractProperty $property)
 59:     {
 60:         $value = $this->parse($entity->getAttribute($property->getIdentifier()));
 61: 
 62:         $entity->setAttribute($property->getName(), $value);
 63: 
 64:         return $value;
 65:     }
 66: 
 67:     /**
 68:      * Parse a string for these values:
 69:      *
 70:      * {filedir_X}, {assets_X:file_name}, {page_X}
 71:      *
 72:      * @param  string $value WYSIWYG content
 73:      * @return string
 74:      */
 75:     public function parse($value)
 76:     {
 77:         if ($value === null || $value === false || $value === '') {
 78:             return '';
 79:         }
 80: 
 81:         preg_match_all('#{page_(\d+)}#', $value, $pageMatches);
 82: 
 83:         foreach ($pageMatches[1] as $i => $entryId) {
 84:             if ($pageUri = $this->siteRepository->getPageUri($entryId)) {
 85:                 $value = str_replace($pageMatches[0][$i], $pageUri, $value);
 86:             }
 87:         }
 88: 
 89:         preg_match_all('#{filedir_(\d+)}#', $value, $filedirMatches);
 90: 
 91:         foreach ($filedirMatches[1] as $i => $id) {
 92:             if ($uploadPref = $this->uploadPrefRepository->find($id)) {
 93:                 $value = str_replace($filedirMatches[0][$i], $uploadPref->url, $value);
 94:             }
 95:         }
 96: 
 97:         // this is all we need to do for now, since we are only supporting Assets locally, not S3 etc.
 98:         preg_match_all('#{assets_\d+:(.*?)}#', $value, $assetsMatches);
 99: 
100:         foreach ($assetsMatches[1] as $i => $url) {
101:             $value = str_replace($assetsMatches[0][$i], $url, $value);
102:         }
103: 
104:         return $value;
105:     }
106: }
107: 
API documentation generated by ApiGen 2.8.0