1: <?php
2:
3: 4: 5: 6: 7: 8:
9:
10: namespace rsanchez\Deep\Hydrator;
11:
12: use Illuminate\Database\Eloquent\Model;
13: use rsanchez\Deep\Collection\EntryCollection;
14: use rsanchez\Deep\Model\AbstractProperty;
15: use rsanchez\Deep\Model\AbstractEntity;
16: use rsanchez\Deep\Hydrator\AbstractHydrator;
17: use rsanchez\Deep\Model\PlayaEntry;
18: use rsanchez\Deep\Collection\PlayaCollection;
19:
20: 21: 22:
23: class PlayaHydrator extends AbstractHydrator
24: {
25: 26: 27:
28: protected $model;
29:
30: 31: 32: 33: 34:
35: protected $entries;
36:
37: 38: 39: 40:
41: protected $playaCollection;
42:
43: 44: 45:
46: public function __construct(EntryCollection $collection, $fieldtype, PlayaEntry $model)
47: {
48: parent::__construct($collection, $fieldtype);
49:
50: $this->model = $model;
51:
52: $this->playaCollection = $this->model->parentEntryId($collection->modelKeys())->get();
53:
54: foreach ($this->playaCollection as $entry) {
55: $type = $entry->parent_row_id ? 'matrix' : 'entry';
56: $entityId = $entry->parent_row_id ? $entry->parent_row_id : $entry->parent_entry_id;
57: $propertyId = $entry->parent_row_id ? $entry->parent_col_id : $entry->parent_field_id;
58:
59: if (! isset($this->entries[$type][$entityId][$propertyId])) {
60: $this->entries[$type][$entityId][$propertyId] = array();
61: }
62:
63: $this->entries[$type][$entityId][$propertyId][] = $entry;
64: }
65:
66:
67: $collection->addEntryIds($this->playaCollection->modelKeys());
68: }
69:
70: 71: 72:
73: public function hydrate(AbstractEntity $entity, AbstractProperty $property)
74: {
75: $entries = isset($this->entries[$entity->getType()][$entity->getId()][$property->getId()])
76: ? $this->entries[$entity->getType()][$entity->getId()][$property->getId()] : array();
77:
78: $value = $this->playaCollection->createChildCollection($entries);
79:
80: $entity->setAttribute($property->getName(), $value);
81:
82: return $value;
83: }
84: }
85: