1: <?php
2: namespace NGS\Converter;
3:
4: require_once(__DIR__.'/../Utils.php');
5:
6: use InvalidArgumentException;
7: use NGS\Utils;
8:
9: 10: 11:
12: abstract class PrimitiveConverter
13: {
14: 15: 16: 17: 18: 19:
20: public static function toInteger($value)
21: {
22: $result = filter_var($value, FILTER_VALIDATE_INT);
23: if ($result === false) {
24: throw new InvalidArgumentException('Could not convert value '.$value.' of type "'.Utils::getType($value).'" to integer!');
25: }
26: return $result;
27: }
28:
29: 30: 31: 32: 33: 34: 35: 36: 37:
38: public static function toIntegerArray(array $items, $allowNullValues=false)
39: {
40: $results = array();
41: try {
42: foreach ($items as $key => $val) {
43: if ($allowNullValues && $val===null) {
44: $results[] = null;
45: } elseif ($val === null) {
46: throw new InvalidArgumentException('Null value found in provided array');
47: } else {
48: $results[] = self::toInteger($val);
49: }
50: }
51: }
52: catch(\Exception $e) {
53: throw new InvalidArgumentException('Element at index '.$key.' could not be converted to integer!', 42, $e);
54: }
55: return $results;
56: }
57:
58: 59: 60: 61: 62: 63: 64: 65:
66: public static function toString($value)
67: {
68: if (is_string($value)) {
69: return $value;
70: }
71: if (is_int($value) || is_float($value)) {
72: return (string) $value;
73: }
74: throw new InvalidArgumentException('Could not convert value '.$value.' of type "'.Utils::getType($value).'" to string!');
75: }
76:
77: 78: 79: 80: 81: 82: 83: 84: 85:
86: public static function toFixedString($value, $length)
87: {
88: $string = self::toString($value);
89: if (!is_int($length)) {
90: throw new InvalidArgumentException('Fixed string length must be an integer, invalid type was: '.Utils::getType($length));
91: }
92: if (mb_strlen($string)>$length) {
93: throw new InvalidArgumentException('String exceeds fixed length ('.$length.')');
94: }
95:
96: return $value;
97: }
98:
99: 100: 101: 102: 103: 104: 105: 106: 107:
108: public static function toStringArray(array $items, $allowNullValues=false)
109: {
110: $results = array();
111: try {
112: foreach ($items as $key => $val) {
113: if ($allowNullValues && $val===null) {
114: $results[] = null;
115: } elseif ($val === null) {
116: throw new InvalidArgumentException('Null value found in provided array');
117: } else {
118: $results[] = self::toString($val);
119: }
120: }
121: }
122: catch(\Exception $e) {
123: throw new InvalidArgumentException('Element at index '.$key.' could not be converted to string!', 42, $e);
124: }
125: return $results;
126: }
127:
128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138:
139: public static function toFixedStringArray(array $items, $length, $allowNullValues=false)
140: {
141: $strings = self::toStringArray($items, $allowNullValues);
142: if (!is_int($length)) {
143: throw new InvalidArgumentException('Fixed string length must be an integer, invalid type was: '.Utils::getType($length));
144: }
145: foreach ($strings as $index=>$string) {
146: if ($length!==null && mb_strlen($string)>$length) {
147: throw new InvalidArgumentException('String at index '.$index.' exceeds fixed length ('.$length.')');
148: }
149: }
150: return $strings;
151: }
152:
153: 154: 155: 156: 157: 158: 159: 160: 161:
162: public static function toBoolean($value)
163: {
164: if(is_bool($value))
165: return $value;
166: if(is_string($value)) {
167: $lcValue = strtolower($value);
168: if($lcValue === 'true' || $lcValue === 'on' || $lcValue === '1')
169: return true;
170: if($lcValue === 'false' || $lcValue === 'off' || $lcValue === '0' || $lcValue === '')
171: return false;
172: throw new InvalidArgumentException('Could not convert value "'.$value.'" of type "string" to boolean!');
173: }
174: if(is_int($value)) {
175: if($value === 1)
176: return true;
177: if($value === 0)
178: return false;
179: throw new InvalidArgumentException('Could not convert value '.$value.' of type "integer" to boolean!');
180: }
181: throw new InvalidArgumentException('Could not convert value '.$value.' of type "'.Utils::getType($value).'" to boolean!');
182: }
183:
184: 185: 186: 187: 188: 189: 190: 191: 192: 193:
194: public static function toBooleanArray(array $items, $allowNullValues=false)
195: {
196: $results = array();
197: try {
198: foreach ($items as $key => $val) {
199: if ($allowNullValues && $val===null) {
200: $results[] = null;
201: } elseif ($val === null) {
202: throw new InvalidArgumentException('Null value found in provided array');
203: } else {
204: $results[] = self::toBoolean($val);
205: }
206: }
207: }
208: catch(\Exception $e) {
209: throw new \InvalidArgumentException('Element at index '.$key.' could not be converted to boolean!', 42, $e);
210: }
211: return $results;
212: }
213:
214: 215: 216: 217: 218: 219: 220:
221: public static function toFloat($value)
222: {
223: $result = filter_var($value, FILTER_VALIDATE_FLOAT);
224: if ($result === false) {
225: throw new InvalidArgumentException('Could not convert value '.$value.' of type "'.Utils::getType($value).'" to float!');
226: }
227: return $result;
228: }
229:
230: 231: 232: 233: 234: 235: 236: 237: 238:
239: public static function toFloatArray(array $items, $allowNullValues=false)
240: {
241: $results = array();
242: try {
243: foreach ($items as $key => $val) {
244: if ($allowNullValues && $val===null) {
245: $results[] = null;
246: } elseif ($val === null) {
247: throw new InvalidArgumentException('Null value found in provided array');
248: } else {
249: $results[] = self::toFloat($val);
250: }
251: }
252: }
253: catch(\Exception $e) {
254: throw new InvalidArgumentException('Element at index '.$key.' could not be converted to float!', 42, $e);
255: }
256: return $results;
257: }
258:
259: 260: 261: 262: 263: 264: 265: 266: 267:
268: public static function toMap(array $items)
269: {
270: try {
271: foreach ($items as $key => &$val) {
272: if($val === null) {
273: throw new InvalidArgumentException('Null value found in provided array');
274: }
275:
276: $val = self::toString($val);
277: }
278: }
279: catch(\Exception $e) {
280: throw new InvalidArgumentException('Element '.$key.' could not be converted to string!', 42, $e);
281: }
282: return $items;
283: }
284:
285: 286: 287: 288: 289: 290: 291: 292: 293:
294: public static function toMapArray(array $items)
295: {
296: try {
297: foreach ($items as $key => &$val) {
298: if($val === null) {
299: throw new InvalidArgumentException('Null value found in provided array');
300: }
301: $val = self::toMap($val);
302: }
303: }
304: catch(\Exception $e) {
305: throw new InvalidArgumentException('Element at index '.$key.' could not be converted to array[string, string]!', 42, $e);
306: }
307: return $items;
308: }
309: }
310: