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 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:  * Hydrator for the Playa fieldtype
22:  */
23: class PlayaHydrator extends AbstractHydrator
24: {
25:     /**
26:      * @var \rsanchez\Deep\Model\PlayaEntry
27:      */
28:     protected $model;
29: 
30:     /**
31:      * List of entries in this collection, organized by
32:      * type, entity and property
33:      * @var array
34:      */
35:     protected $entries;
36: 
37:     /**
38:      * Collection of entries being loaded by the parent collection
39:      * @var \rsanchez\Deep\Collection\PlayaCollection
40:      */
41:     protected $playaCollection;
42: 
43:     /**
44:      * {@inheritdoc}
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:         // add these entry IDs to the main collection
67:         $collection->addEntryIds($this->playaCollection->modelKeys());
68:     }
69: 
70:     /**
71:      * {@inheritdoc}
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: 
API documentation generated by ApiGen 2.8.0