1 | <?php |
||
2 | |||
3 | namespace Funstaff\RefLibRis; |
||
4 | |||
5 | /** |
||
6 | * RisWriter |
||
7 | * |
||
8 | * @author Bertrand Zuchuat <[email protected]> |
||
9 | */ |
||
10 | class RisWriter |
||
11 | { |
||
12 | const RIS_EOL = "\r\n"; |
||
13 | |||
14 | /** |
||
15 | * @var RisDefinitionInterface |
||
16 | */ |
||
17 | private $definition; |
||
18 | |||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | private $records = []; |
||
23 | |||
24 | /** |
||
25 | * RisWriter constructor. |
||
26 | * @param RisDefinitionInterface $definition |
||
27 | */ |
||
28 | public function __construct(RisDefinitionInterface $definition) |
||
29 | { |
||
30 | $this->definition = $definition; |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * @param array $record |
||
35 | * @return $this |
||
36 | */ |
||
37 | public function addRecord(array $record) |
||
38 | { |
||
39 | array_push($this->records, $record); |
||
40 | |||
41 | return $this; |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @param array $records |
||
46 | * @return $this |
||
47 | */ |
||
48 | public function setRecords(array $records) |
||
49 | { |
||
50 | $this->records = $records; |
||
51 | |||
52 | return $this; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @param bool $validation |
||
57 | * @return string |
||
58 | */ |
||
59 | public function process($validation = false): string |
||
60 | { |
||
61 | if (count($this->records) == 0) { |
||
62 | throw new \LengthException('Add Record before call process function.'); |
||
63 | } |
||
64 | |||
65 | $buffer = []; |
||
66 | foreach ($this->records as $record) { |
||
67 | $buffer[] = $this->processRecord($record, $validation); |
||
68 | } |
||
69 | |||
70 | return implode(self::RIS_EOL, $buffer); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @param array $record |
||
75 | * @param $validation |
||
76 | * @return string |
||
77 | */ |
||
78 | private function processRecord(array $record, $validation): string |
||
79 | { |
||
80 | $buffer = []; |
||
81 | |||
82 | if (!array_key_exists('TY', $record)) { |
||
83 | throw new \InvalidArgumentException('TY Tag field not found.'); |
||
84 | } |
||
85 | |||
86 | if (is_string($record['TY'])) { |
||
87 | $record['TY'] = [$record['TY']]; |
||
88 | } |
||
89 | |||
90 | /* First position for TY (Type) */ |
||
91 | array_push($buffer, sprintf('TY - %s', $record['TY'][0])); |
||
92 | unset($record['TY']); |
||
93 | |||
94 | /* Order the array */ |
||
95 | ksort($record); |
||
96 | foreach ($record as $tag => $values) { |
||
97 | if ($validation && !$this->definition->hasField($tag)) { |
||
98 | throw new \InvalidArgumentException('Field Tag not found.'); |
||
99 | } |
||
100 | |||
101 | if (is_string($values)) { |
||
102 | $values = [$values]; |
||
103 | } |
||
104 | |||
105 | foreach ($values as $value) { |
||
106 | array_push($buffer, sprintf('%s - %s', $tag, $value)); |
||
107 | } |
||
108 | } |
||
109 | /* End record */ |
||
110 | array_push($buffer, 'ER - '.self::RIS_EOL); |
||
111 | |||
112 | return implode(self::RIS_EOL, $buffer); |
||
113 | } |
||
114 | } |
||
115 |