Total Complexity | 7 |
Total Lines | 46 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
13 | abstract class Mapper |
||
14 | { |
||
15 | /** |
||
16 | * Attributes map. |
||
17 | * |
||
18 | * @return array |
||
19 | */ |
||
20 | abstract protected function map(): array; |
||
21 | |||
22 | public function normalizeData(string $class, array $data) |
||
30 | } |
||
31 | |||
32 | public function populate(string $class, array $data, $relations = false) |
||
33 | { |
||
34 | $itemMap = ArrayHelper::getValue($this->map(), "$class.item", []); |
||
35 | |||
36 | $model = new $class(); |
||
37 | foreach ($itemMap as $to => $from) { |
||
38 | $model->$to = ArrayHelper::getValue($data, $from); |
||
39 | } |
||
40 | |||
41 | if ($relations) { |
||
42 | $this->populateRelations($model, $data); |
||
43 | } |
||
44 | |||
45 | return $model; |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * @param array $data |
||
50 | * @param $model |
||
51 | */ |
||
52 | private function populateRelations($model, array $data): void |
||
59 | } |
||
60 | } |
||
62 | } |