1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Wowstack\Dbc\MappingField; |
||
6 | |||
7 | /** |
||
8 | * Implements a field containing localized strings. |
||
9 | */ |
||
10 | class LocalizedStringField extends AbstractField implements MappingFieldInterface |
||
11 | { |
||
12 | /** |
||
13 | * Supported locales and their offset in the field. |
||
14 | */ |
||
15 | const LOCALE_INDEX = [ |
||
16 | 'enUS' => 0, |
||
17 | 'enGB' => 0, |
||
18 | 'koKR' => 1, |
||
19 | 'frFR' => 2, |
||
20 | 'deDE' => 3, |
||
21 | 'zhCN' => 4, |
||
22 | 'zhTW' => 5, |
||
23 | 'esES' => 6, |
||
24 | 'esMX' => 7, |
||
25 | ]; |
||
26 | |||
27 | /** |
||
28 | * Amount of bytes used by localization checksum. |
||
29 | */ |
||
30 | const CHECKSUM_SIZE = 4; |
||
31 | |||
32 | /** |
||
33 | * Name of type. |
||
34 | */ |
||
35 | const TYPE = 'localized_string'; |
||
36 | |||
37 | /** |
||
38 | * {@inheritdoc} |
||
39 | */ |
||
40 | protected $size = 4; |
||
41 | |||
42 | /** |
||
43 | * Amount of fields to follow. |
||
44 | * |
||
45 | * @var int |
||
46 | */ |
||
47 | protected $count = 0; |
||
48 | |||
49 | /** |
||
50 | * Default locale to use. |
||
51 | */ |
||
52 | protected $locale = 'enUS'; |
||
53 | |||
54 | /** |
||
55 | * Default amount of locale fields to read. |
||
56 | */ |
||
57 | protected $locale_count = 8; |
||
58 | |||
59 | /** |
||
60 | * Defines required parameters. |
||
61 | */ |
||
62 | const PARAMETERS = ['count']; |
||
63 | |||
64 | /** |
||
65 | * Defines optional parameters and their defaults. |
||
66 | */ |
||
67 | const OPTIONAL_PARAMETERS = ['locale' => 'enUS', 'locale_count' => 8]; |
||
68 | |||
69 | /** |
||
70 | * Format used to pack/unpack this field type. |
||
71 | */ |
||
72 | const PACK_FORMAT = 'V'; |
||
73 | |||
74 | /** |
||
75 | * Constructs the field. |
||
76 | * |
||
77 | * @param string $name |
||
78 | * @param array $parameters |
||
79 | * |
||
80 | * @throws MappingException |
||
81 | */ |
||
82 | 27 | public function __construct(string $name, array $parameters = []) |
|
83 | { |
||
84 | 27 | $this->name = $name; |
|
85 | 27 | $this->setParameters($parameters); |
|
86 | 25 | $this->setOptionalParameters($parameters); |
|
87 | 25 | } |
|
88 | |||
89 | /** |
||
90 | * {@inheritdoc} |
||
91 | */ |
||
92 | 9 | public function getOffset(): int |
|
93 | { |
||
94 | 9 | return self::LOCALE_INDEX[$this->locale]; |
|
95 | } |
||
96 | |||
97 | /** |
||
98 | * {@inheritdoc} |
||
99 | */ |
||
100 | 16 | public function getTotalCount(): int |
|
101 | { |
||
102 | 16 | return $this->count * ($this->locale_count + 1); |
|
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
103 | } |
||
104 | |||
105 | /** |
||
106 | * {@inheritdoc} |
||
107 | */ |
||
108 | 23 | public function getSize(): int |
|
109 | { |
||
110 | 23 | return ($this->size * $this->locale_count) + self::CHECKSUM_SIZE; |
|
0 ignored issues
–
show
|
|||
111 | } |
||
112 | |||
113 | /** |
||
114 | * {@inheritdoc} |
||
115 | */ |
||
116 | 18 | public function getTotalSize(): int |
|
117 | { |
||
118 | 18 | return $this->getSize() * $this->count; |
|
119 | } |
||
120 | |||
121 | /** |
||
122 | * {@inheritdoc} |
||
123 | */ |
||
124 | 9 | public function getParsedFields(): array |
|
125 | { |
||
126 | 9 | $count = 1; |
|
127 | 9 | $locale_count = $this->getLocaleCount(); |
|
0 ignored issues
–
show
|
|||
128 | 9 | $string_offset = 0; |
|
0 ignored issues
–
show
|
|||
129 | 9 | $parsed_fields = []; |
|
0 ignored issues
–
show
|
|||
130 | |||
131 | 9 | while ($count <= $this->getCount()) { |
|
132 | 9 | $field_name = ($this->getCount() > 1 ? $this->getName().$count : $this->getName()); |
|
0 ignored issues
–
show
|
|||
133 | 9 | $pack_fields = []; |
|
0 ignored issues
–
show
|
|||
134 | 9 | while ($string_offset < $locale_count) { |
|
0 ignored issues
–
show
|
|||
135 | 9 | if ($string_offset === $this->getOffset()) { |
|
0 ignored issues
–
show
|
|||
136 | 9 | $pack_fields[] = $this::PACK_FORMAT.'1'.$field_name; |
|
0 ignored issues
–
show
|
|||
137 | } else { |
||
138 | 9 | $pack_fields[] = $this::PACK_FORMAT.'1'.$field_name.'_unused'.$string_offset; |
|
0 ignored issues
–
show
|
|||
139 | } |
||
140 | 9 | ++$string_offset; |
|
0 ignored issues
–
show
|
|||
141 | } |
||
142 | 9 | $string_offset = 0; |
|
0 ignored issues
–
show
|
|||
143 | |||
144 | 9 | $pack_fields[] = $this::PACK_FORMAT.'1'.$field_name.'_checksum'; |
|
0 ignored issues
–
show
|
|||
145 | |||
146 | $parsed_field = [ |
||
0 ignored issues
–
show
|
|||
147 | 9 | 'type' => $this->getType(), |
|
148 | 9 | 'size' => $this->getSize(), |
|
149 | 9 | 'format' => implode('/', $pack_fields), |
|
0 ignored issues
–
show
|
|||
150 | 9 | 'offset' => $this->getOffset(), |
|
151 | ]; |
||
152 | |||
153 | 9 | $parsed_fields[$field_name] = $parsed_field; |
|
0 ignored issues
–
show
|
|||
154 | 9 | ++$count; |
|
155 | } |
||
156 | |||
157 | 9 | return $parsed_fields; |
|
0 ignored issues
–
show
|
|||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Returns the selected locale. |
||
162 | * |
||
163 | * @return string |
||
164 | */ |
||
165 | 2 | public function getLocale(): string |
|
166 | { |
||
167 | 2 | return $this->locale; |
|
168 | } |
||
169 | |||
170 | /** |
||
171 | * Returns the selected locale count. |
||
172 | * |
||
173 | * @return int |
||
174 | */ |
||
175 | 11 | public function getLocaleCount(): int |
|
176 | { |
||
177 | 11 | return $this->locale_count; |
|
0 ignored issues
–
show
|
|||
178 | } |
||
179 | } |
||
180 |