1 | <?php |
||
22 | class ModelMapper extends BaseComponent implements MapperInterface |
||
23 | { |
||
24 | /** |
||
25 | * {@inheritdoc} |
||
26 | */ |
||
27 | 3 | public function export(array $records): array |
|
28 | { |
||
29 | 3 | $result = []; |
|
30 | 3 | foreach ($records as $record) { |
|
31 | 2 | $modelClass = get_class($record); |
|
32 | 2 | $converter = Craft::$app->controller->module->getConverter($modelClass); |
|
33 | 2 | if ($converter) { |
|
34 | 2 | $index = $converter->getRecordIndex(); |
|
35 | 2 | $result[$record->$index] = $converter->getRecordDefinition($record); |
|
36 | } |
||
37 | } |
||
38 | |||
39 | 3 | return $result; |
|
40 | } |
||
41 | |||
42 | /** |
||
43 | * Import records. |
||
44 | * |
||
45 | * @param array $definitions |
||
46 | * @param Model $records The existing records |
||
47 | * @param array $defaultAttributes Default attributes to use for each record |
||
48 | * @param bool $persist Whether to persist the parsed records |
||
49 | * |
||
50 | * @return array |
||
51 | */ |
||
52 | 12 | public function import(array $definitions, array $records, array $defaultAttributes = [], $persist = true): array |
|
91 | |||
92 | /** |
||
93 | * Get records by handle. |
||
94 | * |
||
95 | * @param array $records |
||
96 | * |
||
97 | * @return array |
||
98 | */ |
||
99 | 12 | private function getRecordsByHandle(array $records): array |
|
111 | |||
112 | /** |
||
113 | * Find record from records by handle or new record. |
||
114 | * |
||
115 | * @param Model[] $recordsByHandle |
||
116 | * @param array $definition |
||
117 | * @param string $handle |
||
118 | * |
||
119 | * @return Model |
||
120 | */ |
||
121 | 4 | private function findOrNewRecord(array $recordsByHandle, array $definition, string $handle): Model |
|
136 | } |
||
137 |