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\Collection\RelationshipCollection;
15: use rsanchez\Deep\Model\AbstractProperty;
16: use rsanchez\Deep\Model\AbstractEntity;
17: use rsanchez\Deep\Hydrator\AbstractHydrator;
18: use rsanchez\Deep\Model\RelationshipEntry;
19:
20: 21: 22:
23: class SiblingsHydrator extends AbstractHydrator
24: {
25: 26: 27:
28: protected $model;
29:
30: 31: 32:
33: public function __construct(EntryCollection $collection, $fieldtype, RelationshipEntry $model)
34: {
35: parent::__construct($collection, $fieldtype);
36:
37: $this->model = $model;
38:
39: $this->relationshipCollection = $this->model->siblings($collection->modelKeys())->get();
40:
41: foreach ($this->relationshipCollection as $entry) {
42: if (! isset($this->entries[$entry->sibling_id])) {
43: $this->entries[$entry->sibling_id] = array();
44: }
45:
46: $this->entries[$entry->sibling_id][] = $entry;
47: }
48:
49:
50: $collection->addEntryIds($this->relationshipCollection->modelKeys());
51: }
52:
53: 54: 55:
56: public function hydrate(AbstractEntity $entity, AbstractProperty $property)
57: {
58: $value = isset($this->entries[$entity->getId()]) ? $this->entries[$entity->getId()] : array();
59:
60: $value = $this->relationshipCollection->createChildCollection($entries);
61:
62: $entity->setAttribute($property->getName(), $value);
63:
64: return $value;
65: }
66: }
67: