1: <?php
  2: 
  3:   4:   5:   6:   7:   8: 
  9: 
 10: namespace rsanchez\Deep\Hydrator;
 11: 
 12: use rsanchez\Deep\Collection\EntryCollection;
 13: use rsanchez\Deep\Collection\AbstractTitleCollection;
 14: use rsanchez\Deep\Hydrator\DefaultHydrator;
 15: use rsanchez\Deep\Repository\SiteRepository;
 16: use rsanchez\Deep\Repository\UploadPrefRepositoryInterface;
 17: use rsanchez\Deep\Model\Asset;
 18: use rsanchez\Deep\Model\File;
 19: use rsanchez\Deep\Model\GridCol;
 20: use rsanchez\Deep\Model\GridRow;
 21: use rsanchez\Deep\Model\MatrixCol;
 22: use rsanchez\Deep\Model\MatrixRow;
 23: use rsanchez\Deep\Model\PlayaEntry;
 24: use rsanchez\Deep\Model\RelationshipEntry;
 25: 
 26:  27:  28: 
 29: class HydratorFactory
 30: {
 31:      32:  33:  34: 
 35:     protected $hydrators = array(
 36:         'matrix'                => '\\rsanchez\\Deep\\Hydrator\\MatrixHydrator',
 37:         'grid'                  => '\\rsanchez\\Deep\\Hydrator\\GridHydrator',
 38:         'playa'                 => '\\rsanchez\\Deep\\Hydrator\\PlayaHydrator',
 39:         'relationship'          => '\\rsanchez\\Deep\\Hydrator\\RelationshipHydrator',
 40:         'assets'                => '\\rsanchez\\Deep\\Hydrator\\AssetsHydrator',
 41:         'file'                  => '\\rsanchez\\Deep\\Hydrator\\FileHydrator',
 42:         'date'                  => '\\rsanchez\\Deep\\Hydrator\\DateHydrator',
 43:         'multi_select'          => '\\rsanchez\\Deep\\Hydrator\\PipeHydrator',
 44:         'checkboxes'            => '\\rsanchez\\Deep\\Hydrator\\PipeHydrator',
 45:         'fieldpack_checkboxes'  => '\\rsanchez\\Deep\\Hydrator\\ExplodeHydrator',
 46:         'fieldpack_multiselect' => '\\rsanchez\\Deep\\Hydrator\\ExplodeHydrator',
 47:         'fieldpack_list'        => '\\rsanchez\\Deep\\Hydrator\\ExplodeHydrator',
 48:         'wygwam'                => '\\rsanchez\\Deep\\Hydrator\\WysiwygHydrator',
 49:         'parents'               => '\\rsanchez\\Deep\\Hydrator\\ParentsHydrator',
 50:         'siblings'              => '\\rsanchez\\Deep\\Hydrator\\SiblingsHydrator',
 51:     );
 52: 
 53:      54:  55:  56: 
 57:     protected $siteRepository;
 58: 
 59:      60:  61:  62: 
 63:     protected $uploadPrefRepository;
 64: 
 65:      66:  67:  68:  69:  70: 
 71:     public function __construct(
 72:         SiteRepository $siteRepository,
 73:         UploadPrefRepositoryInterface $uploadPrefRepository,
 74:         Asset $asset,
 75:         File $file,
 76:         GridCol $gridCol,
 77:         GridRow $gridRow,
 78:         MatrixCol $matrixCol,
 79:         MatrixRow $matrixRow,
 80:         PlayaEntry $playaEntry,
 81:         RelationshipEntry $relationshipEntry
 82:     ) {
 83:         $this->siteRepository = $siteRepository;
 84:         $this->uploadPrefRepository = $uploadPrefRepository;
 85:         $this->asset = $asset;
 86:         $this->file =  $file;
 87:         $this->gridCol = $gridCol;
 88:         $this->gridRow = $gridRow;
 89:         $this->matrixCol = $matrixCol;
 90:         $this->matrixRow = $matrixRow;
 91:         $this->playaEntry = $playaEntry;
 92:         $this->relationshipEntry = $relationshipEntry;
 93:     }
 94: 
 95:      96:  97:  98:  99: 100: 
101:     public function getHydrators(AbstractTitleCollection $collection, array $extraHydrators = array())
102:     {
103:         $hydrators = array();
104: 
105:         if ($collection->hasCustomFields()) {
106:             
107:             foreach ($this->hydrators as $type => $class) {
108:                 if ($collection->hasFieldtype($type)) {
109:                     $hydrators[$type] = $this->newHydrator($collection, $type);
110:                 }
111:             }
112:         }
113: 
114:         foreach ($extraHydrators as $type) {
115:             if (isset($this->hydrators[$type])) {
116:                 $hydrators[$type] = $this->newHydrator($collection, $type);
117:             }
118:         }
119: 
120:         return $hydrators;
121:     }
122: 
123:     124: 125: 126: 127: 128: 
129:     public function newHydrator(EntryCollection $collection, $fieldtype)
130:     {
131:         $class = $this->hydrators[$fieldtype];
132: 
133:         $baseClass = basename(str_replace('\\', DIRECTORY_SEPARATOR, $class));
134: 
135:         $method = 'new'.$baseClass;
136: 
137:         
138:         if (method_exists($this, $method)) {
139:             return $this->$method($collection, $fieldtype);
140:         }
141: 
142:         return new $class($collection, $fieldtype);
143:     }
144: 
145:     146: 147: 148: 149: 150: 
151:     public function newAssetsHydrator(EntryCollection $collection, $fieldtype)
152:     {
153:         return new AssetsHydrator($collection, $fieldtype, $this->asset, $this->uploadPrefRepository);
154:     }
155: 
156:     157: 158: 159: 160: 161: 
162:     public function newFileHydrator(EntryCollection $collection, $fieldtype)
163:     {
164:         return new FileHydrator($collection, $fieldtype, $this->file, $this->uploadPrefRepository);
165:     }
166: 
167:     168: 169: 170: 171: 172: 
173:     public function newGridHydrator(EntryCollection $collection, $fieldtype)
174:     {
175:         return new GridHydrator($collection, $fieldtype, $this->gridCol, $this->gridRow);
176:     }
177: 
178:     179: 180: 181: 182: 183: 
184:     public function newMatrixHydrator(EntryCollection $collection, $fieldtype)
185:     {
186:         return new MatrixHydrator($collection, $fieldtype, $this->matrixCol, $this->matrixRow);
187:     }
188: 
189:     190: 191: 192: 193: 194: 
195:     public function newPlayaHydrator(EntryCollection $collection, $fieldtype)
196:     {
197:         return new PlayaHydrator($collection, $fieldtype, $this->playaEntry);
198:     }
199: 
200:     201: 202: 203: 204: 205: 
206:     public function newRelationshipHydrator(EntryCollection $collection, $fieldtype)
207:     {
208:         return new RelationshipHydrator($collection, $fieldtype, $this->relationshipEntry);
209:     }
210: 
211:     212: 213: 214: 215: 216: 
217:     public function newParentsHydrator(EntryCollection $collection, $fieldtype)
218:     {
219:         return new ParentsHydrator($collection, $fieldtype, $this->relationshipEntry);
220:     }
221: 
222:     223: 224: 225: 226: 227: 
228:     public function newSiblingsHydrator(EntryCollection $collection, $fieldtype)
229:     {
230:         return new SiblingsHydrator($collection, $fieldtype, $this->relationshipEntry);
231:     }
232: 
233:     234: 235: 236: 237: 238: 
239:     public function newWysiwygHydrator(EntryCollection $collection, $fieldtype)
240:     {
241:         return new WysiwygHydrator($collection, $fieldtype, $this->siteRepository, $this->uploadPrefRepository);
242:     }
243: }
244: