1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Wowstack\Dbc\MappingField; |
||
6 | |||
7 | /** |
||
8 | * Provides generic features for all field types. |
||
9 | */ |
||
10 | abstract class AbstractField |
||
11 | { |
||
12 | /** |
||
13 | * Name of type. |
||
14 | */ |
||
15 | const TYPE = 'undefined'; |
||
16 | |||
17 | /** |
||
18 | * @var string field name |
||
19 | */ |
||
20 | protected $name = 'undefined'; |
||
21 | |||
22 | /** |
||
23 | * Size of field in bytes. |
||
24 | */ |
||
25 | protected $size = 0; |
||
26 | |||
27 | /** |
||
28 | * Amount of fields to follow. |
||
29 | * |
||
30 | * @var int |
||
31 | */ |
||
32 | protected $count = 0; |
||
33 | |||
34 | /** |
||
35 | * Defines required parameters. |
||
36 | */ |
||
37 | const PARAMETERS = ['count']; |
||
38 | |||
39 | /** |
||
40 | * Defines optional parameters and their defaults. |
||
41 | */ |
||
42 | const OPTIONAL_PARAMETERS = []; |
||
43 | |||
44 | /** |
||
45 | * Format used to pack/unpack this field type. |
||
46 | */ |
||
47 | const PACK_FORMAT = ''; |
||
48 | |||
49 | /** |
||
50 | * Sets required parameters. |
||
51 | * |
||
52 | * @param array $parameters |
||
53 | * |
||
54 | * @throws MappingException |
||
55 | */ |
||
56 | 64 | public function setParameters(array $parameters = []) |
|
57 | { |
||
58 | 64 | foreach ($this::PARAMETERS as $key) { |
|
59 | 64 | if (!array_key_exists($key, $parameters)) { |
|
60 | 9 | throw new MappingException("Parameter ${key} missing."); |
|
61 | } |
||
62 | |||
63 | 55 | $this->{$key} = $parameters[$key]; |
|
64 | } |
||
65 | 55 | } |
|
66 | |||
67 | /** |
||
68 | * Sets optional parameters. |
||
69 | * |
||
70 | * @param array $parameters |
||
71 | */ |
||
72 | 55 | public function setOptionalParameters(array $parameters = []) |
|
73 | { |
||
74 | 55 | foreach ($this::OPTIONAL_PARAMETERS as $key => $default) { |
|
75 | 25 | if (array_key_exists($key, $parameters)) { |
|
76 | 7 | $this->{$key} = $parameters[$key]; |
|
77 | } else { |
||
78 | 25 | $this->{$key} = $default; |
|
79 | } |
||
80 | } |
||
81 | 55 | } |
|
82 | |||
83 | /** |
||
84 | * Provides the name of the field. |
||
85 | * |
||
86 | * @return string |
||
87 | */ |
||
88 | 33 | public function getName(): string |
|
89 | { |
||
90 | 33 | return $this->name; |
|
91 | } |
||
92 | |||
93 | /** |
||
94 | * Provides the type of the field. |
||
95 | * |
||
96 | * @return string |
||
97 | */ |
||
98 | 49 | public function getType(): string |
|
99 | { |
||
100 | 49 | return $this::TYPE; |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * Provides how many bytes this field requires. |
||
105 | * |
||
106 | * @return int |
||
107 | */ |
||
108 | 28 | public function getSize(): int |
|
109 | { |
||
110 | 28 | return $this->size; |
|
111 | } |
||
112 | |||
113 | /** |
||
114 | * Provides how many of this field follow. |
||
115 | * |
||
116 | * @return int |
||
117 | */ |
||
118 | 33 | public function getCount(): int |
|
119 | { |
||
120 | 33 | return $this->count; |
|
121 | } |
||
122 | |||
123 | /** |
||
124 | * Provides how many of this field follow. |
||
125 | * |
||
126 | * @return int |
||
127 | */ |
||
128 | 23 | public function getTotalCount(): int |
|
129 | { |
||
130 | 23 | return $this->count; |
|
131 | } |
||
132 | |||
133 | /** |
||
134 | * Provides how many bytes for all fields follow. |
||
135 | * |
||
136 | * @return int |
||
137 | */ |
||
138 | 25 | public function getTotalSize(): int |
|
139 | { |
||
140 | 25 | return $this->count * $this->size; |
|
141 | } |
||
142 | |||
143 | /** |
||
144 | * Returns the offset at which to find the data. |
||
145 | * |
||
146 | * @return int |
||
147 | */ |
||
148 | 21 | public function getOffset(): int |
|
149 | { |
||
150 | 21 | return 0; |
|
151 | } |
||
152 | |||
153 | /** |
||
154 | * Returns the resulting field(s). |
||
155 | * |
||
156 | * @return array |
||
157 | */ |
||
158 | 21 | public function getParsedFields(): array |
|
159 | { |
||
160 | 21 | $count = 1; |
|
161 | 21 | $parsedFields = []; |
|
162 | |||
163 | 21 | while ($count <= $this->getCount()) { |
|
164 | 21 | $fieldName = ($this->getCount() > 1 ? $this->getName().$count : $this->getName()); |
|
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
165 | $parsedField = [ |
||
166 | 21 | 'type' => $this->getType(), |
|
167 | 21 | 'size' => $this->getSize(), |
|
168 | 21 | 'format' => $this::PACK_FORMAT.'1'.$fieldName, |
|
0 ignored issues
–
show
|
|||
169 | 21 | 'offset' => $this->getOffset(), |
|
170 | ]; |
||
171 | |||
172 | 21 | $parsedFields[$fieldName] = $parsedField; |
|
173 | 21 | ++$count; |
|
174 | } |
||
175 | |||
176 | 21 | return $parsedFields; |
|
177 | } |
||
178 | } |
||
179 |