Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
MapTypeCaster.php
1<?php
2
4
8
9// ToDo check it with real world on product/sku editing
10
20{
21 public const NOTHING = 'Nothing';
22
23 public const STRING = 'String';
24 public const NULLABLE_STRING = 'NullableString';
25
26 public const INT = 'Int';
27 public const NULLABLE_INT = 'NullableInt';
28 public const MULTI_INT = 'MultiInt';
29 public const NULLABLE_MULTI_INT = 'NullableMultiInt';
30
31 public const FLOAT = 'Float';
32 public const NULLABLE_FLOAT = 'NullableFloat';
33
34 public const BOOLEAN = 'Boolean';
35 public const Y_OR_N = 'YesOrNo';
36 public const Y_OR_N_OR_D = 'YesOrNoOrDefault';
37
38 public const DATE = 'Date';
39 public const DATETIME = 'DateTime';
40
41 private $fieldMap = [];
42
43 public function __construct(array $fieldMap = null)
44 {
45 if (!empty($fieldMap))
46 {
47 $this->fieldMap = $fieldMap;
48 }
49 }
50
51 private function castToNothing($value)
52 {
53 return $value;
54 }
55
56 private function castToString($value): string
57 {
58 return (string)$value;
59 }
60
61 private function castToNullableString($value): ?string
62 {
63 if ($value !== null)
64 {
65 $value = $this->castToString($value);
66 }
67
68 return $value;
69 }
70
71 private function castToInt($value): int
72 {
73 return (int)$value;
74 }
75
76 private function castToNullableInt($value): ?int
77 {
78 if ($value !== null)
79 {
80 $value = $this->castToInt($value);
81 }
82
83 return $value;
84 }
85
86 private function castToMultiInt($value): array
87 {
88 $result = [];
89 if (!is_array($value))
90 {
91 $value = [$value];
92 }
93 foreach ($value as $item)
94 {
95 if ($item === '' || $item === null)
96 {
97 continue;
98 }
99 $result[] = (int)$item;
100 }
101
102 return $result;
103 }
104
105 private function castToNullableMultiInt($value): ?array
106 {
107 if ($value !== null)
108 {
109 $value = $this->castToMultiInt($value);
110 if (empty($value))
111 {
112 $value = [];
113 }
114 }
115
116 return $value;
117 }
118
119 private function castToFloat($value): float
120 {
121 return (float)$value;
122 }
123
124 private function castToNullableFloat($value): ?float
125 {
126 if ($value !== null)
127 {
128 $value = $this->castToFloat($value);
129 }
130
131 return $value;
132 }
133
134 private function castToYesOrNo($value): string
135 {
136 if (is_bool($value))
137 {
138 return $value ? 'Y' : 'N';
139 }
140
141 return (string)$value === 'Y' ? 'Y' : 'N';
142 }
143
144 private function castToYesOrNoOrDefault($value): string
145 {
146 if (is_bool($value))
147 {
148 return $value ? 'Y' : 'N';
149 }
150
151 $value = (string)$value;
152
153 if ($value !== 'Y' && $value !== 'D')
154 {
155 $value = 'N';
156 }
157
158 return $value;
159 }
160
161 private function castToBoolean($value): bool
162 {
163 return (bool)$value;
164 }
165
166 private function castToDate($value): ?Date
167 {
168 if ($value !== null && $value !== '')
169 {
170 return new Date($value);
171 }
172
173 return null;
174 }
175
176 private function castToDateTime($value): ?DateTime
177 {
178 if ($value !== null && $value !== '')
179 {
180 return new DateTime($value);
181 }
182
183 return null;
184 }
185
186 public function cast($name, $value)
187 {
188 if ($this->has($name))
189 {
190 if (is_string($this->fieldMap[$name]))
191 {
192 $castMethod = "castTo{$this->fieldMap[$name]}";
193 if (is_callable([$this, $castMethod]))
194 {
195 return $this->$castMethod($value);
196 }
197 }
198
199 if (is_callable($this->fieldMap[$name]))
200 {
201 return $this->fieldMap[$name]($value);
202 }
203
204 throw new NotSupportedException(sprintf(
205 'Could not find casting {%s} for field {%s}.',
206 $this->fieldMap[$name], $name
207 ));
208 }
209
210 return $value;
211 }
212
213 public function has($name): bool
214 {
215 return isset($this->fieldMap[$name]);
216 }
217}