1: <?php
2:
3: 4: 5: 6: 7: 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: 23:
24: class WysiwygHydrator extends AbstractHydrator
25: {
26: 27: 28: 29:
30: protected $siteRepository;
31:
32: 33: 34: 35:
36: protected $uploadPrefRepository;
37:
38: 39: 40: 41: 42: 43: 44: 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: 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: 69: 70: 71: 72: 73: 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:
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: