Overview

Namespaces

  • NGS
    • Client
      • Exception
    • Converter
    • Patterns
  • PHP

Classes

  • BigDecimalConverter
  • BigIntConverter
  • ByteStreamConverter
  • LocalDateConverter
  • MoneyConverter
  • ObjectConverter
  • PrimitiveConverter
  • TimestampConverter
  • UUIDConverter
  • XmlConverter

Interfaces

  • ConverterInterface
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: namespace NGS\Converter;
  3: 
  4: require_once(__DIR__.'/../Utils.php');
  5: 
  6: use InvalidArgumentException;
  7: use NGS\Utils;
  8: 
  9: /**
 10:  * Converts values to primitive php types (string, integer, float, boolean)
 11:  */
 12: abstract class PrimitiveConverter
 13: {
 14:     /**
 15:      * Converts value to integer type
 16:      * @param float|integer|string $value
 17:      * @return integer
 18:      * @throws InvalidArgumentException If $value cannot be converted to integer
 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:      * Converts all values in array to integer types. Resulting array will be
 31:      * reindexed with numeric indices.
 32:      *
 33:      * @param array $items
 34:      * @return array
 35:      * @throws InvalidArgumentException If any element in array containts null
 36:      * value or cannot be converted to integer
 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:      * Converts value to string
 60:      *
 61:      * @param mixed $value Value
 62:      * @return string Resulting string
 63:      * @throws InvalidArgumentException If value cannot be converted to string,
 64:      * or its length exceeds given $length
 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:      * Converts value to string and checks it doesn't exceed maximum length
 79:      *
 80:      * @param mixed $value Value
 81:      * @param integer $length Maximum allowed string length
 82:      * @return string Resulting string
 83:      * @throws InvalidArgumentException If value cannot be converted to string,
 84:      * or its length exceeds given $length
 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:             //return mb_substr($value, $length);
 96:         return $value;
 97:     }
 98: 
 99:     /**
100:      * Converts all values in array to string types. Resulting array will be
101:      * reindexed with numeric indices.
102:      *
103:      * @param array $items Array of values convertible to strings
104:      * @return array Array of strings with reindexed keys
105:      * @throws InvalidArgumentException If any array element cannot be converted
106:      * to string
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:      * Converts all values in array to string types and checks that each string
130:      * length doesn't exceed maximum length.
131:      * Resulting array will be reindexed with numeric indices.
132:      *
133:      * @param array $items Array of values convertible to strings
134:      * @param type $length Maximum allowed string length
135:      * @return array Array of strings with reindexed keys
136:      * @throws InvalidArgumentException If any array element cannot be converted
137:      * to string, or its value exceeds given $length
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:      * Converts value to boolean; valid string values are: 'true','1',
155:      * 'on','false','0','off' (all case-insensitive); valid integer values are
156:      * 0 and 1
157:      *
158:      * @param bool|string|integer Source value
159:      * @return boolean Converted value
160:      * @throws InvalidArgumentException If value cannot be converted to boolean
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:     * Converts all values in array to bool types. Resulting array will be
186:     * reindexed with numeric indices.
187:     * For list of valid values, see: {@see PrimitiveConverter::toBoolean()}
188:     *
189:     * @param array $items Array of values convertible to booleans.
190:     * @return array Array of booleans with reindexed keys
191:     * @throws InvalidArgumentException If any array element cannot be converted
192:     * to string, or its value exceeds given $length
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:      * Converts value to float.
216:      *
217:      * @param bool|string|integer Source value
218:      * @return float Converted value
219:      * @throws InvalidArgumentException If value cannot be converted to float
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:      * Converts all values in array to float types. Resulting array will be
232:      * reindexed with numeric indices.
233:      *
234:      * @param array $items
235:      * @return array
236:      * @throws InvalidArgumentException If any element in array containts null
237:      * value or cannot be converted to float
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:      * Converts all values in array to string type. As toStringArray, but
261:      * preserves original array keys.
262:      *
263:      * @param array $items Array of values convertible to strings
264:      * @return array Array of strings with preserved original keys
265:      * @throws InvalidArgumentException If any array element cannot be converted
266:      * to string
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:      * Converts all values in array to string map (string array), runs toMap()
287:      * on each element.
288:      *
289:      * @param array $items Array of string maps
290:      * @return array Array of string arrays with preserved original keys
291:      * @throws InvalidArgumentException If any array element cannot be converted
292:      * to string map
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: 
API documentation generated by ApiGen 2.8.0