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\Model\AbstractProperty;
15: use rsanchez\Deep\Model\AbstractEntity;
16: use rsanchez\Deep\Hydrator\AbstractHydrator;
17: use rsanchez\Deep\Model\File;
18: use rsanchez\Deep\Collection\EntryCollection;
19: use rsanchez\Deep\Repository\UploadPrefRepositoryInterface;
20:
21: 22: 23:
24: class FileHydrator extends AbstractHydrator
25: {
26: 27: 28:
29: protected $model;
30:
31: 32: 33: 34:
35: protected $files;
36:
37: 38: 39: 40:
41: protected $uploadPrefRepository;
42:
43: 44: 45: 46: 47: 48: 49:
50: public function __construct(EntryCollection $collection, $fieldtype, File $model, UploadPrefRepositoryInterface $uploadPrefRepository)
51: {
52: parent::__construct($collection, $fieldtype);
53:
54: $this->model = $model;
55:
56: $this->uploadPrefRepository = $uploadPrefRepository;
57: }
58:
59: 60: 61:
62: public function preload(array $entryIds)
63: {
64: $files = $this->model->fromEntryCollection($this->collection)->get();
65:
66: foreach ($files as $file) {
67: if (! $file->upload_location_id || ! $uploadPref = $this->uploadPrefRepository->find($file->upload_location_id)) {
68: continue;
69: }
70:
71: $file->setUploadPref($uploadPref);
72:
73: $this->files['{filedir_'.$file->upload_location_id.'}'.$file->file_name] = $file;
74: }
75: }
76:
77: 78: 79:
80: public function hydrate(AbstractEntity $entity, AbstractProperty $property)
81: {
82: $value = $entity->getAttribute($property->getIdentifier());
83:
84: $value = $value && isset($this->files[$value]) ? $this->files[$value] : null;
85:
86: $entity->setAttribute($property->getName(), $value);
87:
88: return $value;
89: }
90: }
91: