1 | <?php |
||
2 | |||
3 | namespace Funstaff\RefLibRis; |
||
4 | |||
5 | /** |
||
6 | * RisMappings |
||
7 | * |
||
8 | * @author Bertrand Zuchuat <[email protected]> |
||
9 | */ |
||
10 | class RisMappings implements RisMappingsInterface |
||
11 | { |
||
12 | /** |
||
13 | * @var array |
||
14 | */ |
||
15 | private $mappings; |
||
16 | |||
17 | /** |
||
18 | * @var string |
||
19 | */ |
||
20 | private $fallback; |
||
21 | |||
22 | /** |
||
23 | * RisMappings constructor. |
||
24 | * @param array $mappings |
||
25 | * @param string $fallback |
||
26 | */ |
||
27 | public function __construct(array $mappings, string $fallback) |
||
28 | { |
||
29 | $this->mappings = $this->mappings($mappings); |
||
30 | $this->fallback = $fallback; |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * @param string $type |
||
35 | * @return mixed |
||
36 | * @throws \Exception |
||
37 | */ |
||
38 | public function findRisFieldByType(string $type) |
||
39 | { |
||
40 | if (!array_key_exists($type, $this->mappings)) { |
||
41 | $type = $this->fallback; |
||
42 | } |
||
43 | |||
44 | if (!array_key_exists($type, $this->mappings)) { |
||
45 | throw new \InvalidArgumentException(sprintf( |
||
46 | 'Missing mapping for fallback: "%s"', |
||
47 | $type |
||
48 | )); |
||
49 | } |
||
50 | |||
51 | return $this->mappings[$type]; |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * @param $mappings |
||
56 | * @return array |
||
57 | */ |
||
58 | private function mappings(array $mappings) |
||
59 | { |
||
60 | $output = []; |
||
61 | foreach ($mappings as $key => $mappingFields) { |
||
62 | if (!array_key_exists($key, $output)) { |
||
63 | $output[$key] = New RisFieldsMapping($mappingFields); |
||
64 | } |
||
65 | } |
||
66 | |||
67 | return $output; |
||
68 | } |
||
69 | } |
||
70 |