1 | <?php |
||
2 | |||
3 | namespace Funstaff\RefLibRis; |
||
4 | |||
5 | /** |
||
6 | * RecordProcessing |
||
7 | * |
||
8 | * @author Bertrand Zuchuat <[email protected]> |
||
9 | */ |
||
10 | class RecordProcessing |
||
11 | { |
||
12 | /** |
||
13 | * @var RisMappingsInterface |
||
14 | */ |
||
15 | private $mappings; |
||
16 | |||
17 | /** |
||
18 | * RecordProcessing constructor. |
||
19 | * @param RisMappingsInterface $mappings |
||
20 | */ |
||
21 | public function __construct(RisMappingsInterface $mappings) |
||
22 | { |
||
23 | $this->mappings = $mappings; |
||
24 | } |
||
25 | |||
26 | /** |
||
27 | * @param array $recordFields |
||
28 | * @param string $columnTypeName |
||
29 | * @return mixed |
||
30 | * @throws \Exception |
||
31 | */ |
||
32 | public function process(array $recordFields, string $columnTypeName = 'type') |
||
33 | { |
||
34 | if (!array_key_exists($columnTypeName, $recordFields)) { |
||
35 | throw new \InvalidArgumentException(sprintf( |
||
36 | 'The name "%s" of Column Type does not exists on fields record', |
||
37 | $columnTypeName |
||
38 | )); |
||
39 | } |
||
40 | |||
41 | $type = $recordFields[$columnTypeName][0]; |
||
42 | |||
43 | $mapping = $this->mappings->findRisFieldByType($type); |
||
44 | $risFields = $this->getRisFieldsArray($mapping); |
||
45 | foreach ($recordFields as $field => $values) { |
||
46 | if (is_string($values)) { |
||
47 | $values = [$values]; |
||
48 | } |
||
49 | $risField = $mapping->findRisFieldByFieldName($field); |
||
50 | if (null !== $risField) { |
||
51 | foreach ($values as $value) { |
||
52 | $risFields[$risField][] = $value; |
||
53 | } |
||
54 | } |
||
55 | } |
||
56 | |||
57 | return $risFields; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @param RisFieldsMappingInterface $mapping |
||
62 | * @return array|null |
||
63 | */ |
||
64 | private function getRisFieldsArray(RisFieldsMappingInterface $mapping) |
||
65 | { |
||
66 | $risFields = array_flip($mapping->getAllRisFields()); |
||
67 | array_walk($risFields, function(&$line) { |
||
68 | $line = []; |
||
69 | }); |
||
70 | |||
71 | return $risFields; |
||
72 | } |
||
73 | } |
||
74 |