@@ -14,21 +14,21 @@ |
||
14 | 14 | */ |
15 | 15 | class Arr |
16 | 16 | { |
17 | - /** |
|
18 | - * Deep copy given array |
|
19 | - * |
|
20 | - * @param array $arr |
|
21 | - * |
|
22 | - * @return array |
|
23 | - */ |
|
24 | - public static function copy(array $arr) |
|
25 | - { |
|
26 | - $copy = array(); |
|
27 | - foreach ($arr as $key => $value) { |
|
28 | - if (is_array($value)) $copy[$key] = static::copy($value); |
|
29 | - else if (is_object($value)) $copy[$key] = clone $value; |
|
30 | - else $copy[$key] = $value; |
|
31 | - } |
|
32 | - return $copy; |
|
33 | - } |
|
17 | + /** |
|
18 | + * Deep copy given array |
|
19 | + * |
|
20 | + * @param array $arr |
|
21 | + * |
|
22 | + * @return array |
|
23 | + */ |
|
24 | + public static function copy(array $arr) |
|
25 | + { |
|
26 | + $copy = array(); |
|
27 | + foreach ($arr as $key => $value) { |
|
28 | + if (is_array($value)) $copy[$key] = static::copy($value); |
|
29 | + else if (is_object($value)) $copy[$key] = clone $value; |
|
30 | + else $copy[$key] = $value; |
|
31 | + } |
|
32 | + return $copy; |
|
33 | + } |
|
34 | 34 | } |
35 | 35 | \ No newline at end of file |
@@ -25,9 +25,13 @@ |
||
25 | 25 | { |
26 | 26 | $copy = array(); |
27 | 27 | foreach ($arr as $key => $value) { |
28 | - if (is_array($value)) $copy[$key] = static::copy($value); |
|
29 | - else if (is_object($value)) $copy[$key] = clone $value; |
|
30 | - else $copy[$key] = $value; |
|
28 | + if (is_array($value)) { |
|
29 | + $copy[$key] = static::copy($value); |
|
30 | + } else if (is_object($value)) { |
|
31 | + $copy[$key] = clone $value; |
|
32 | + } else { |
|
33 | + $copy[$key] = $value; |
|
34 | + } |
|
31 | 35 | } |
32 | 36 | return $copy; |
33 | 37 | } |
@@ -14,19 +14,19 @@ |
||
14 | 14 | */ |
15 | 15 | interface iValidate { |
16 | 16 | |
17 | - /** |
|
18 | - * method used for validation. |
|
19 | - * |
|
20 | - * @param mixed $input |
|
21 | - * data that needs to be validated |
|
22 | - * @param ValidationInfo $info |
|
23 | - * information to be used for validation |
|
24 | - * @return boolean false in case of failure or fixed value in the expected |
|
25 | - * type |
|
26 | - * @throws \Luracast\Restler\RestException 400 with information about the |
|
27 | - * failed |
|
28 | - * validation |
|
29 | - */ |
|
30 | - public static function validate($input, ValidationInfo $info); |
|
17 | + /** |
|
18 | + * method used for validation. |
|
19 | + * |
|
20 | + * @param mixed $input |
|
21 | + * data that needs to be validated |
|
22 | + * @param ValidationInfo $info |
|
23 | + * information to be used for validation |
|
24 | + * @return boolean false in case of failure or fixed value in the expected |
|
25 | + * type |
|
26 | + * @throws \Luracast\Restler\RestException 400 with information about the |
|
27 | + * failed |
|
28 | + * validation |
|
29 | + */ |
|
30 | + public static function validate($input, ValidationInfo $info); |
|
31 | 31 | } |
32 | 32 |
@@ -15,143 +15,143 @@ |
||
15 | 15 | */ |
16 | 16 | class Obj |
17 | 17 | { |
18 | - /** |
|
19 | - * @var bool|string|callable |
|
20 | - */ |
|
21 | - public static $stringEncoderFunction = false; |
|
22 | - /** |
|
23 | - * @var bool|string|callable |
|
24 | - */ |
|
25 | - public static $numberEncoderFunction = false; |
|
26 | - /** |
|
27 | - * @var array key value pairs for fixing value types using functions. |
|
28 | - * For example |
|
29 | - * |
|
30 | - * 'id'=>'intval' will make sure all values of the id properties |
|
31 | - * will be converted to integers intval function |
|
32 | - * 'password'=> null will remove all the password entries |
|
33 | - */ |
|
34 | - public static $fix = array(); |
|
35 | - /** |
|
36 | - * @var string character that is used to identify sub objects |
|
37 | - * |
|
38 | - * For example |
|
39 | - * |
|
40 | - * when Object::$separatorChar = '.'; |
|
41 | - * |
|
42 | - * array('my.object'=>true) will result in |
|
43 | - * |
|
44 | - * array( |
|
45 | - * 'my'=>array('object'=>true) |
|
46 | - * ); |
|
47 | - */ |
|
48 | - public static $separatorChar = null; |
|
49 | - /** |
|
50 | - * @var bool set it to true when empty arrays, blank strings, null values |
|
51 | - * to be automatically removed from response |
|
52 | - */ |
|
53 | - public static $removeEmpty = false; |
|
54 | - /** |
|
55 | - * @var bool set it to true to remove all null values from the result |
|
56 | - */ |
|
57 | - public static $removeNull = false; |
|
18 | + /** |
|
19 | + * @var bool|string|callable |
|
20 | + */ |
|
21 | + public static $stringEncoderFunction = false; |
|
22 | + /** |
|
23 | + * @var bool|string|callable |
|
24 | + */ |
|
25 | + public static $numberEncoderFunction = false; |
|
26 | + /** |
|
27 | + * @var array key value pairs for fixing value types using functions. |
|
28 | + * For example |
|
29 | + * |
|
30 | + * 'id'=>'intval' will make sure all values of the id properties |
|
31 | + * will be converted to integers intval function |
|
32 | + * 'password'=> null will remove all the password entries |
|
33 | + */ |
|
34 | + public static $fix = array(); |
|
35 | + /** |
|
36 | + * @var string character that is used to identify sub objects |
|
37 | + * |
|
38 | + * For example |
|
39 | + * |
|
40 | + * when Object::$separatorChar = '.'; |
|
41 | + * |
|
42 | + * array('my.object'=>true) will result in |
|
43 | + * |
|
44 | + * array( |
|
45 | + * 'my'=>array('object'=>true) |
|
46 | + * ); |
|
47 | + */ |
|
48 | + public static $separatorChar = null; |
|
49 | + /** |
|
50 | + * @var bool set it to true when empty arrays, blank strings, null values |
|
51 | + * to be automatically removed from response |
|
52 | + */ |
|
53 | + public static $removeEmpty = false; |
|
54 | + /** |
|
55 | + * @var bool set it to true to remove all null values from the result |
|
56 | + */ |
|
57 | + public static $removeNull = false; |
|
58 | 58 | |
59 | - /** |
|
60 | - * Convenience function that converts the given object |
|
61 | - * in to associative array |
|
62 | - * |
|
63 | - * @static |
|
64 | - * |
|
65 | - * @param mixed $object that needs to be converted |
|
66 | - * |
|
67 | - * @param bool $forceObjectTypeWhenEmpty when set to true outputs |
|
68 | - * actual type (array or |
|
69 | - * object) rather than |
|
70 | - * always an array when the |
|
71 | - * array/object is empty |
|
72 | - * |
|
73 | - * @return array |
|
74 | - */ |
|
75 | - public static function toArray($object, |
|
76 | - $forceObjectTypeWhenEmpty = false) |
|
77 | - { |
|
78 | - //if ($object instanceof JsonSerializable) { //wont work on PHP < 5.4 |
|
79 | - if (is_object($object)) { |
|
80 | - if (method_exists($object, 'jsonSerialize')) { |
|
81 | - $object = $object->jsonSerialize(); |
|
82 | - } elseif (method_exists($object, '__sleep')) { |
|
83 | - $properties = $object->__sleep(); |
|
84 | - $array = array(); |
|
85 | - foreach ($properties as $key) { |
|
86 | - $value = self::toArray($object->{$key}, |
|
87 | - $forceObjectTypeWhenEmpty); |
|
88 | - if (self::$stringEncoderFunction && is_string($value)) { |
|
89 | - $value = self::$stringEncoderFunction ($value); |
|
90 | - } elseif (self::$numberEncoderFunction && is_numeric($value)) { |
|
91 | - $value = self::$numberEncoderFunction ($value); |
|
92 | - } |
|
93 | - $array [$key] = $value; |
|
94 | - } |
|
95 | - return $array; |
|
96 | - } |
|
97 | - } |
|
98 | - if (is_array($object) || is_object($object)) { |
|
99 | - $count = 0; |
|
100 | - $array = array(); |
|
101 | - foreach ($object as $key => $value) { |
|
102 | - if ( |
|
103 | - is_string(self::$separatorChar) && |
|
104 | - false !== strpos($key, self::$separatorChar) |
|
105 | - ) { |
|
106 | - list($key, $obj) = explode(self::$separatorChar, $key, 2); |
|
107 | - $object[$key][$obj] = $value; |
|
108 | - $value = $object[$key]; |
|
109 | - } |
|
110 | - if (self::$removeEmpty && empty($value) && !is_numeric($value) && !is_bool($value)) { |
|
111 | - continue; |
|
112 | - } elseif (self::$removeNull && is_null($value)) { |
|
113 | - continue; |
|
114 | - } |
|
115 | - if (array_key_exists($key, self::$fix)) { |
|
116 | - if (isset(self::$fix[$key])) { |
|
117 | - $value = call_user_func(self::$fix[$key], $value); |
|
118 | - } else { |
|
119 | - continue; |
|
120 | - } |
|
121 | - } |
|
122 | - $value = self::toArray($value, $forceObjectTypeWhenEmpty); |
|
123 | - if (self::$stringEncoderFunction && is_string($value)) { |
|
124 | - $value = self::$encoderFunctionName ($value); |
|
125 | - } elseif (self::$numberEncoderFunction && is_numeric($value)) { |
|
126 | - $value = self::$numberEncoderFunction ($value); |
|
127 | - } |
|
128 | - $array [$key] = $value; |
|
129 | - $count++; |
|
130 | - } |
|
131 | - return $forceObjectTypeWhenEmpty && $count == 0 ? $object : $array; |
|
132 | - } |
|
59 | + /** |
|
60 | + * Convenience function that converts the given object |
|
61 | + * in to associative array |
|
62 | + * |
|
63 | + * @static |
|
64 | + * |
|
65 | + * @param mixed $object that needs to be converted |
|
66 | + * |
|
67 | + * @param bool $forceObjectTypeWhenEmpty when set to true outputs |
|
68 | + * actual type (array or |
|
69 | + * object) rather than |
|
70 | + * always an array when the |
|
71 | + * array/object is empty |
|
72 | + * |
|
73 | + * @return array |
|
74 | + */ |
|
75 | + public static function toArray($object, |
|
76 | + $forceObjectTypeWhenEmpty = false) |
|
77 | + { |
|
78 | + //if ($object instanceof JsonSerializable) { //wont work on PHP < 5.4 |
|
79 | + if (is_object($object)) { |
|
80 | + if (method_exists($object, 'jsonSerialize')) { |
|
81 | + $object = $object->jsonSerialize(); |
|
82 | + } elseif (method_exists($object, '__sleep')) { |
|
83 | + $properties = $object->__sleep(); |
|
84 | + $array = array(); |
|
85 | + foreach ($properties as $key) { |
|
86 | + $value = self::toArray($object->{$key}, |
|
87 | + $forceObjectTypeWhenEmpty); |
|
88 | + if (self::$stringEncoderFunction && is_string($value)) { |
|
89 | + $value = self::$stringEncoderFunction ($value); |
|
90 | + } elseif (self::$numberEncoderFunction && is_numeric($value)) { |
|
91 | + $value = self::$numberEncoderFunction ($value); |
|
92 | + } |
|
93 | + $array [$key] = $value; |
|
94 | + } |
|
95 | + return $array; |
|
96 | + } |
|
97 | + } |
|
98 | + if (is_array($object) || is_object($object)) { |
|
99 | + $count = 0; |
|
100 | + $array = array(); |
|
101 | + foreach ($object as $key => $value) { |
|
102 | + if ( |
|
103 | + is_string(self::$separatorChar) && |
|
104 | + false !== strpos($key, self::$separatorChar) |
|
105 | + ) { |
|
106 | + list($key, $obj) = explode(self::$separatorChar, $key, 2); |
|
107 | + $object[$key][$obj] = $value; |
|
108 | + $value = $object[$key]; |
|
109 | + } |
|
110 | + if (self::$removeEmpty && empty($value) && !is_numeric($value) && !is_bool($value)) { |
|
111 | + continue; |
|
112 | + } elseif (self::$removeNull && is_null($value)) { |
|
113 | + continue; |
|
114 | + } |
|
115 | + if (array_key_exists($key, self::$fix)) { |
|
116 | + if (isset(self::$fix[$key])) { |
|
117 | + $value = call_user_func(self::$fix[$key], $value); |
|
118 | + } else { |
|
119 | + continue; |
|
120 | + } |
|
121 | + } |
|
122 | + $value = self::toArray($value, $forceObjectTypeWhenEmpty); |
|
123 | + if (self::$stringEncoderFunction && is_string($value)) { |
|
124 | + $value = self::$encoderFunctionName ($value); |
|
125 | + } elseif (self::$numberEncoderFunction && is_numeric($value)) { |
|
126 | + $value = self::$numberEncoderFunction ($value); |
|
127 | + } |
|
128 | + $array [$key] = $value; |
|
129 | + $count++; |
|
130 | + } |
|
131 | + return $forceObjectTypeWhenEmpty && $count == 0 ? $object : $array; |
|
132 | + } |
|
133 | 133 | |
134 | - return $object; |
|
135 | - } |
|
134 | + return $object; |
|
135 | + } |
|
136 | 136 | |
137 | - public function __get($name) |
|
138 | - { |
|
139 | - isset(self::$fix[$name]) ? self::$fix[$name] : null; |
|
140 | - } |
|
137 | + public function __get($name) |
|
138 | + { |
|
139 | + isset(self::$fix[$name]) ? self::$fix[$name] : null; |
|
140 | + } |
|
141 | 141 | |
142 | - public function __set($name, $function) |
|
143 | - { |
|
144 | - self::$fix[$name] = $function; |
|
145 | - } |
|
142 | + public function __set($name, $function) |
|
143 | + { |
|
144 | + self::$fix[$name] = $function; |
|
145 | + } |
|
146 | 146 | |
147 | - public function __isset($name) |
|
148 | - { |
|
149 | - return isset(self::$fix[$name]); |
|
150 | - } |
|
147 | + public function __isset($name) |
|
148 | + { |
|
149 | + return isset(self::$fix[$name]); |
|
150 | + } |
|
151 | 151 | |
152 | - public function __unset($name) |
|
153 | - { |
|
154 | - unset(self::$fix[$name]); |
|
155 | - } |
|
152 | + public function __unset($name) |
|
153 | + { |
|
154 | + unset(self::$fix[$name]); |
|
155 | + } |
|
156 | 156 | } |
157 | 157 |
@@ -86,9 +86,9 @@ discard block |
||
86 | 86 | $value = self::toArray($object->{$key}, |
87 | 87 | $forceObjectTypeWhenEmpty); |
88 | 88 | if (self::$stringEncoderFunction && is_string($value)) { |
89 | - $value = self::$stringEncoderFunction ($value); |
|
89 | + $value = self::$stringEncoderFunction($value); |
|
90 | 90 | } elseif (self::$numberEncoderFunction && is_numeric($value)) { |
91 | - $value = self::$numberEncoderFunction ($value); |
|
91 | + $value = self::$numberEncoderFunction($value); |
|
92 | 92 | } |
93 | 93 | $array [$key] = $value; |
94 | 94 | } |
@@ -121,9 +121,9 @@ discard block |
||
121 | 121 | } |
122 | 122 | $value = self::toArray($value, $forceObjectTypeWhenEmpty); |
123 | 123 | if (self::$stringEncoderFunction && is_string($value)) { |
124 | - $value = self::$encoderFunctionName ($value); |
|
124 | + $value = self::$encoderFunctionName($value); |
|
125 | 125 | } elseif (self::$numberEncoderFunction && is_numeric($value)) { |
126 | - $value = self::$numberEncoderFunction ($value); |
|
126 | + $value = self::$numberEncoderFunction($value); |
|
127 | 127 | } |
128 | 128 | $array [$key] = $value; |
129 | 129 | $count++; |
@@ -16,46 +16,46 @@ |
||
16 | 16 | class ValueObject implements iValueObject |
17 | 17 | { |
18 | 18 | |
19 | - public function __toString() |
|
20 | - { |
|
21 | - return ' new ' . get_called_class() . '() '; |
|
22 | - } |
|
19 | + public function __toString() |
|
20 | + { |
|
21 | + return ' new ' . get_called_class() . '() '; |
|
22 | + } |
|
23 | 23 | |
24 | - public static function __set_state(array $properties) |
|
25 | - { |
|
26 | - $class = get_called_class(); |
|
27 | - $instance = new $class (); |
|
28 | - $vars = get_object_vars($instance); |
|
29 | - foreach ($properties as $property => $value) { |
|
30 | - if (property_exists($instance, $property)) { |
|
31 | - // see if the property is accessible |
|
32 | - if (array_key_exists($property, $vars)) { |
|
33 | - $instance->{$property} = $value; |
|
34 | - } else { |
|
35 | - $method = 'set' . ucfirst($property); |
|
36 | - if (method_exists($instance, $method)) { |
|
37 | - call_user_func(array( |
|
38 | - $instance, |
|
39 | - $method |
|
40 | - ), $value); |
|
41 | - } |
|
42 | - } |
|
43 | - } |
|
44 | - } |
|
45 | - return $instance; |
|
46 | - } |
|
24 | + public static function __set_state(array $properties) |
|
25 | + { |
|
26 | + $class = get_called_class(); |
|
27 | + $instance = new $class (); |
|
28 | + $vars = get_object_vars($instance); |
|
29 | + foreach ($properties as $property => $value) { |
|
30 | + if (property_exists($instance, $property)) { |
|
31 | + // see if the property is accessible |
|
32 | + if (array_key_exists($property, $vars)) { |
|
33 | + $instance->{$property} = $value; |
|
34 | + } else { |
|
35 | + $method = 'set' . ucfirst($property); |
|
36 | + if (method_exists($instance, $method)) { |
|
37 | + call_user_func(array( |
|
38 | + $instance, |
|
39 | + $method |
|
40 | + ), $value); |
|
41 | + } |
|
42 | + } |
|
43 | + } |
|
44 | + } |
|
45 | + return $instance; |
|
46 | + } |
|
47 | 47 | |
48 | - public function __toArray() |
|
49 | - { |
|
50 | - $r = get_object_vars($this); |
|
51 | - $methods = get_class_methods($this); |
|
52 | - foreach ($methods as $m) { |
|
53 | - if (substr($m, 0, 3) == 'get') { |
|
54 | - $r [lcfirst(substr($m, 3))] = @$this->{$m} (); |
|
55 | - } |
|
56 | - } |
|
57 | - return $r; |
|
58 | - } |
|
48 | + public function __toArray() |
|
49 | + { |
|
50 | + $r = get_object_vars($this); |
|
51 | + $methods = get_class_methods($this); |
|
52 | + foreach ($methods as $m) { |
|
53 | + if (substr($m, 0, 3) == 'get') { |
|
54 | + $r [lcfirst(substr($m, 3))] = @$this->{$m} (); |
|
55 | + } |
|
56 | + } |
|
57 | + return $r; |
|
58 | + } |
|
59 | 59 | |
60 | 60 | } |
61 | 61 |
@@ -18,13 +18,13 @@ discard block |
||
18 | 18 | |
19 | 19 | public function __toString() |
20 | 20 | { |
21 | - return ' new ' . get_called_class() . '() '; |
|
21 | + return ' new '.get_called_class().'() '; |
|
22 | 22 | } |
23 | 23 | |
24 | 24 | public static function __set_state(array $properties) |
25 | 25 | { |
26 | 26 | $class = get_called_class(); |
27 | - $instance = new $class (); |
|
27 | + $instance = new $class(); |
|
28 | 28 | $vars = get_object_vars($instance); |
29 | 29 | foreach ($properties as $property => $value) { |
30 | 30 | if (property_exists($instance, $property)) { |
@@ -32,7 +32,7 @@ discard block |
||
32 | 32 | if (array_key_exists($property, $vars)) { |
33 | 33 | $instance->{$property} = $value; |
34 | 34 | } else { |
35 | - $method = 'set' . ucfirst($property); |
|
35 | + $method = 'set'.ucfirst($property); |
|
36 | 36 | if (method_exists($instance, $method)) { |
37 | 37 | call_user_func(array( |
38 | 38 | $instance, |
@@ -16,24 +16,24 @@ |
||
16 | 16 | interface iValueObject |
17 | 17 | { |
18 | 18 | |
19 | - /** |
|
20 | - * This static method is called for creating an instance of the class by |
|
21 | - * passing the initiation values as an array. |
|
22 | - * |
|
23 | - * @static |
|
24 | - * @abstract |
|
25 | - * |
|
26 | - * @param array $properties |
|
27 | - * |
|
28 | - * @return iValueObject |
|
29 | - */ |
|
30 | - public static function __set_state(array $properties); |
|
19 | + /** |
|
20 | + * This static method is called for creating an instance of the class by |
|
21 | + * passing the initiation values as an array. |
|
22 | + * |
|
23 | + * @static |
|
24 | + * @abstract |
|
25 | + * |
|
26 | + * @param array $properties |
|
27 | + * |
|
28 | + * @return iValueObject |
|
29 | + */ |
|
30 | + public static function __set_state(array $properties); |
|
31 | 31 | |
32 | - /** |
|
33 | - * This method provides a string representation for the instance |
|
34 | - * |
|
35 | - * @return string |
|
36 | - */ |
|
37 | - public function __toString(); |
|
32 | + /** |
|
33 | + * This method provides a string representation for the instance |
|
34 | + * |
|
35 | + * @return string |
|
36 | + */ |
|
37 | + public function __toString(); |
|
38 | 38 | } |
39 | 39 |
@@ -15,41 +15,41 @@ |
||
15 | 15 | */ |
16 | 16 | class ApiMethodInfo extends ValueObject |
17 | 17 | { |
18 | - /** |
|
19 | - * @var string target url |
|
20 | - */ |
|
21 | - public $url; |
|
22 | - /** |
|
23 | - * @var string |
|
24 | - */ |
|
25 | - public $className; |
|
26 | - /** |
|
27 | - * @var string |
|
28 | - */ |
|
29 | - public $methodName; |
|
30 | - /** |
|
31 | - * @var array parameters to be passed to the api method |
|
32 | - */ |
|
33 | - public $parameters = array(); |
|
34 | - /** |
|
35 | - * @var array information on parameters in the form of array(name => index) |
|
36 | - */ |
|
37 | - public $arguments = array(); |
|
38 | - /** |
|
39 | - * @var array default values for parameters if any |
|
40 | - * in the form of array(index => value) |
|
41 | - */ |
|
42 | - public $defaults = array(); |
|
43 | - /** |
|
44 | - * @var array key => value pair of method meta information |
|
45 | - */ |
|
46 | - public $metadata = array(); |
|
47 | - /** |
|
48 | - * @var int access level |
|
49 | - * 0 - @public - available for all |
|
50 | - * 1 - @hybrid - both public and protected (enhanced info for authorized) |
|
51 | - * 2 - @protected comment - only for authenticated users |
|
52 | - * 3 - protected method - only for authenticated users |
|
53 | - */ |
|
54 | - public $accessLevel = 0; |
|
18 | + /** |
|
19 | + * @var string target url |
|
20 | + */ |
|
21 | + public $url; |
|
22 | + /** |
|
23 | + * @var string |
|
24 | + */ |
|
25 | + public $className; |
|
26 | + /** |
|
27 | + * @var string |
|
28 | + */ |
|
29 | + public $methodName; |
|
30 | + /** |
|
31 | + * @var array parameters to be passed to the api method |
|
32 | + */ |
|
33 | + public $parameters = array(); |
|
34 | + /** |
|
35 | + * @var array information on parameters in the form of array(name => index) |
|
36 | + */ |
|
37 | + public $arguments = array(); |
|
38 | + /** |
|
39 | + * @var array default values for parameters if any |
|
40 | + * in the form of array(index => value) |
|
41 | + */ |
|
42 | + public $defaults = array(); |
|
43 | + /** |
|
44 | + * @var array key => value pair of method meta information |
|
45 | + */ |
|
46 | + public $metadata = array(); |
|
47 | + /** |
|
48 | + * @var int access level |
|
49 | + * 0 - @public - available for all |
|
50 | + * 1 - @hybrid - both public and protected (enhanced info for authorized) |
|
51 | + * 2 - @protected comment - only for authenticated users |
|
52 | + * 3 - protected method - only for authenticated users |
|
53 | + */ |
|
54 | + public $accessLevel = 0; |
|
55 | 55 | } |
56 | 56 | \ No newline at end of file |
@@ -22,706 +22,706 @@ |
||
22 | 22 | */ |
23 | 23 | class Validator implements iValidate |
24 | 24 | { |
25 | - public static $holdException = false; |
|
26 | - public static $exceptions = array(); |
|
25 | + public static $holdException = false; |
|
26 | + public static $exceptions = array(); |
|
27 | 27 | |
28 | - public static $preFilters = array( |
|
29 | - //'*' => 'some_global_filter', //applied to all parameters |
|
30 | - 'string' => 'trim', //apply filter function by type (string) |
|
31 | - //'string' => 'strip_tags', |
|
32 | - //'string' => 'htmlspecialchars', |
|
33 | - //'int' => 'abs', |
|
34 | - //'float' => 'abs', |
|
35 | - //'CustomClass' => 'MyFilterClass::custom', |
|
36 | - // please note that you wont get an instance |
|
37 | - // of CustomClass. you will get an array instead |
|
38 | - ); |
|
28 | + public static $preFilters = array( |
|
29 | + //'*' => 'some_global_filter', //applied to all parameters |
|
30 | + 'string' => 'trim', //apply filter function by type (string) |
|
31 | + //'string' => 'strip_tags', |
|
32 | + //'string' => 'htmlspecialchars', |
|
33 | + //'int' => 'abs', |
|
34 | + //'float' => 'abs', |
|
35 | + //'CustomClass' => 'MyFilterClass::custom', |
|
36 | + // please note that you wont get an instance |
|
37 | + // of CustomClass. you will get an array instead |
|
38 | + ); |
|
39 | 39 | |
40 | - /** |
|
41 | - * Validate alphabetic characters. |
|
42 | - * |
|
43 | - * Check that given value contains only alphabetic characters. |
|
44 | - * |
|
45 | - * @param $input |
|
46 | - * @param ValidationInfo $info |
|
47 | - * |
|
48 | - * @return string |
|
49 | - * |
|
50 | - * @throws Invalid |
|
51 | - */ |
|
52 | - public static function alpha($input, ValidationInfo $info = null) |
|
53 | - { |
|
54 | - if (ctype_alpha($input)) { |
|
55 | - return $input; |
|
56 | - } |
|
57 | - if ($info && $info->fix) { |
|
58 | - //remove non alpha characters |
|
59 | - return preg_replace("/[^a-z]/i", "", $input); |
|
60 | - } |
|
61 | - throw new Invalid('Expecting only alphabetic characters.'); |
|
62 | - } |
|
40 | + /** |
|
41 | + * Validate alphabetic characters. |
|
42 | + * |
|
43 | + * Check that given value contains only alphabetic characters. |
|
44 | + * |
|
45 | + * @param $input |
|
46 | + * @param ValidationInfo $info |
|
47 | + * |
|
48 | + * @return string |
|
49 | + * |
|
50 | + * @throws Invalid |
|
51 | + */ |
|
52 | + public static function alpha($input, ValidationInfo $info = null) |
|
53 | + { |
|
54 | + if (ctype_alpha($input)) { |
|
55 | + return $input; |
|
56 | + } |
|
57 | + if ($info && $info->fix) { |
|
58 | + //remove non alpha characters |
|
59 | + return preg_replace("/[^a-z]/i", "", $input); |
|
60 | + } |
|
61 | + throw new Invalid('Expecting only alphabetic characters.'); |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * Validate UUID strings. |
|
66 | - * |
|
67 | - * Check that given value contains only alpha numeric characters and the length is 36 chars. |
|
68 | - * |
|
69 | - * @param $input |
|
70 | - * @param ValidationInfo $info |
|
71 | - * |
|
72 | - * @return string |
|
73 | - * |
|
74 | - * @throws Invalid |
|
75 | - */ |
|
76 | - public static function uuid($input, ValidationInfo $info = null) |
|
77 | - { |
|
78 | - if (is_string($input) && preg_match( |
|
79 | - '/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/i', |
|
80 | - $input |
|
81 | - )) { |
|
82 | - return strtolower($input); |
|
83 | - } |
|
84 | - throw new Invalid('Expecting a Universally Unique IDentifier (UUID) string.'); |
|
85 | - } |
|
64 | + /** |
|
65 | + * Validate UUID strings. |
|
66 | + * |
|
67 | + * Check that given value contains only alpha numeric characters and the length is 36 chars. |
|
68 | + * |
|
69 | + * @param $input |
|
70 | + * @param ValidationInfo $info |
|
71 | + * |
|
72 | + * @return string |
|
73 | + * |
|
74 | + * @throws Invalid |
|
75 | + */ |
|
76 | + public static function uuid($input, ValidationInfo $info = null) |
|
77 | + { |
|
78 | + if (is_string($input) && preg_match( |
|
79 | + '/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/i', |
|
80 | + $input |
|
81 | + )) { |
|
82 | + return strtolower($input); |
|
83 | + } |
|
84 | + throw new Invalid('Expecting a Universally Unique IDentifier (UUID) string.'); |
|
85 | + } |
|
86 | 86 | |
87 | - /** |
|
88 | - * Validate alpha numeric characters. |
|
89 | - * |
|
90 | - * Check that given value contains only alpha numeric characters. |
|
91 | - * |
|
92 | - * @param $input |
|
93 | - * @param ValidationInfo $info |
|
94 | - * |
|
95 | - * @return string |
|
96 | - * |
|
97 | - * @throws Invalid |
|
98 | - */ |
|
99 | - public static function alphanumeric($input, ValidationInfo $info = null) |
|
100 | - { |
|
101 | - if (ctype_alnum($input)) { |
|
102 | - return $input; |
|
103 | - } |
|
104 | - if ($info && $info->fix) { |
|
105 | - //remove non alpha numeric and space characters |
|
106 | - return preg_replace("/[^a-z0-9 ]/i", "", $input); |
|
107 | - } |
|
108 | - throw new Invalid('Expecting only alpha numeric characters.'); |
|
109 | - } |
|
87 | + /** |
|
88 | + * Validate alpha numeric characters. |
|
89 | + * |
|
90 | + * Check that given value contains only alpha numeric characters. |
|
91 | + * |
|
92 | + * @param $input |
|
93 | + * @param ValidationInfo $info |
|
94 | + * |
|
95 | + * @return string |
|
96 | + * |
|
97 | + * @throws Invalid |
|
98 | + */ |
|
99 | + public static function alphanumeric($input, ValidationInfo $info = null) |
|
100 | + { |
|
101 | + if (ctype_alnum($input)) { |
|
102 | + return $input; |
|
103 | + } |
|
104 | + if ($info && $info->fix) { |
|
105 | + //remove non alpha numeric and space characters |
|
106 | + return preg_replace("/[^a-z0-9 ]/i", "", $input); |
|
107 | + } |
|
108 | + throw new Invalid('Expecting only alpha numeric characters.'); |
|
109 | + } |
|
110 | 110 | |
111 | - /** |
|
112 | - * Validate printable characters. |
|
113 | - * |
|
114 | - * Check that given value contains only printable characters. |
|
115 | - * |
|
116 | - * @param $input |
|
117 | - * @param ValidationInfo $info |
|
118 | - * |
|
119 | - * @return string |
|
120 | - * |
|
121 | - * @throws Invalid |
|
122 | - */ |
|
123 | - public static function printable($input, ValidationInfo $info = null) |
|
124 | - { |
|
125 | - if (ctype_print($input)) { |
|
126 | - return $input; |
|
127 | - } |
|
128 | - if ($info && $info->fix) { |
|
129 | - //remove non printable characters |
|
130 | - return preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $input); |
|
131 | - } |
|
132 | - throw new Invalid('Expecting only printable characters.'); |
|
133 | - } |
|
111 | + /** |
|
112 | + * Validate printable characters. |
|
113 | + * |
|
114 | + * Check that given value contains only printable characters. |
|
115 | + * |
|
116 | + * @param $input |
|
117 | + * @param ValidationInfo $info |
|
118 | + * |
|
119 | + * @return string |
|
120 | + * |
|
121 | + * @throws Invalid |
|
122 | + */ |
|
123 | + public static function printable($input, ValidationInfo $info = null) |
|
124 | + { |
|
125 | + if (ctype_print($input)) { |
|
126 | + return $input; |
|
127 | + } |
|
128 | + if ($info && $info->fix) { |
|
129 | + //remove non printable characters |
|
130 | + return preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $input); |
|
131 | + } |
|
132 | + throw new Invalid('Expecting only printable characters.'); |
|
133 | + } |
|
134 | 134 | |
135 | - /** |
|
136 | - * Validate hexadecimal digits. |
|
137 | - * |
|
138 | - * Check that given value contains only hexadecimal digits. |
|
139 | - * |
|
140 | - * @param $input |
|
141 | - * @param ValidationInfo $info |
|
142 | - * |
|
143 | - * @return string |
|
144 | - * |
|
145 | - * @throws Invalid |
|
146 | - */ |
|
147 | - public static function hex($input, ValidationInfo $info = null) |
|
148 | - { |
|
149 | - if (ctype_xdigit($input)) { |
|
150 | - return $input; |
|
151 | - } |
|
152 | - throw new Invalid('Expecting only hexadecimal digits.'); |
|
153 | - } |
|
135 | + /** |
|
136 | + * Validate hexadecimal digits. |
|
137 | + * |
|
138 | + * Check that given value contains only hexadecimal digits. |
|
139 | + * |
|
140 | + * @param $input |
|
141 | + * @param ValidationInfo $info |
|
142 | + * |
|
143 | + * @return string |
|
144 | + * |
|
145 | + * @throws Invalid |
|
146 | + */ |
|
147 | + public static function hex($input, ValidationInfo $info = null) |
|
148 | + { |
|
149 | + if (ctype_xdigit($input)) { |
|
150 | + return $input; |
|
151 | + } |
|
152 | + throw new Invalid('Expecting only hexadecimal digits.'); |
|
153 | + } |
|
154 | 154 | |
155 | - /** |
|
156 | - * Color specified as hexadecimals |
|
157 | - * |
|
158 | - * Check that given value contains only color. |
|
159 | - * |
|
160 | - * @param $input |
|
161 | - * @param ValidationInfo|null $info |
|
162 | - * |
|
163 | - * @return string |
|
164 | - * @throws Invalid |
|
165 | - */ |
|
166 | - public static function color($input, ValidationInfo $info = null) |
|
167 | - { |
|
168 | - if (preg_match('/^#[a-f0-9]{6}$/i', $input)) { |
|
169 | - return $input; |
|
170 | - } |
|
171 | - throw new Invalid('Expecting color as hexadecimal digits.'); |
|
172 | - } |
|
155 | + /** |
|
156 | + * Color specified as hexadecimals |
|
157 | + * |
|
158 | + * Check that given value contains only color. |
|
159 | + * |
|
160 | + * @param $input |
|
161 | + * @param ValidationInfo|null $info |
|
162 | + * |
|
163 | + * @return string |
|
164 | + * @throws Invalid |
|
165 | + */ |
|
166 | + public static function color($input, ValidationInfo $info = null) |
|
167 | + { |
|
168 | + if (preg_match('/^#[a-f0-9]{6}$/i', $input)) { |
|
169 | + return $input; |
|
170 | + } |
|
171 | + throw new Invalid('Expecting color as hexadecimal digits.'); |
|
172 | + } |
|
173 | 173 | |
174 | - /** |
|
175 | - * Validate Telephone number |
|
176 | - * |
|
177 | - * Check if the given value is numeric with or without a `+` prefix |
|
178 | - * |
|
179 | - * @param $input |
|
180 | - * @param ValidationInfo $info |
|
181 | - * |
|
182 | - * @return string |
|
183 | - * |
|
184 | - * @throws Invalid |
|
185 | - */ |
|
186 | - public static function tel($input, ValidationInfo $info = null) |
|
187 | - { |
|
188 | - if (is_numeric($input) && '-' != substr($input, 0, 1)) { |
|
189 | - return $input; |
|
190 | - } |
|
191 | - throw new Invalid('Expecting phone number, a numeric value ' . |
|
192 | - 'with optional `+` prefix'); |
|
193 | - } |
|
174 | + /** |
|
175 | + * Validate Telephone number |
|
176 | + * |
|
177 | + * Check if the given value is numeric with or without a `+` prefix |
|
178 | + * |
|
179 | + * @param $input |
|
180 | + * @param ValidationInfo $info |
|
181 | + * |
|
182 | + * @return string |
|
183 | + * |
|
184 | + * @throws Invalid |
|
185 | + */ |
|
186 | + public static function tel($input, ValidationInfo $info = null) |
|
187 | + { |
|
188 | + if (is_numeric($input) && '-' != substr($input, 0, 1)) { |
|
189 | + return $input; |
|
190 | + } |
|
191 | + throw new Invalid('Expecting phone number, a numeric value ' . |
|
192 | + 'with optional `+` prefix'); |
|
193 | + } |
|
194 | 194 | |
195 | - /** |
|
196 | - * Validate Email |
|
197 | - * |
|
198 | - * Check if the given string is a valid email |
|
199 | - * |
|
200 | - * @param String $input |
|
201 | - * @param ValidationInfo $info |
|
202 | - * |
|
203 | - * @return string |
|
204 | - * @throws Invalid |
|
205 | - */ |
|
206 | - public static function email($input, ValidationInfo $info = null) |
|
207 | - { |
|
208 | - $r = filter_var($input, FILTER_VALIDATE_EMAIL); |
|
209 | - if ($r) { |
|
210 | - return $r; |
|
211 | - } elseif ($info && $info->fix) { |
|
212 | - $r = filter_var($input, FILTER_SANITIZE_EMAIL); |
|
213 | - return static::email($r); |
|
214 | - } |
|
215 | - throw new Invalid('Expecting email in `[email protected]` format'); |
|
216 | - } |
|
195 | + /** |
|
196 | + * Validate Email |
|
197 | + * |
|
198 | + * Check if the given string is a valid email |
|
199 | + * |
|
200 | + * @param String $input |
|
201 | + * @param ValidationInfo $info |
|
202 | + * |
|
203 | + * @return string |
|
204 | + * @throws Invalid |
|
205 | + */ |
|
206 | + public static function email($input, ValidationInfo $info = null) |
|
207 | + { |
|
208 | + $r = filter_var($input, FILTER_VALIDATE_EMAIL); |
|
209 | + if ($r) { |
|
210 | + return $r; |
|
211 | + } elseif ($info && $info->fix) { |
|
212 | + $r = filter_var($input, FILTER_SANITIZE_EMAIL); |
|
213 | + return static::email($r); |
|
214 | + } |
|
215 | + throw new Invalid('Expecting email in `[email protected]` format'); |
|
216 | + } |
|
217 | 217 | |
218 | - /** |
|
219 | - * Validate IP Address |
|
220 | - * |
|
221 | - * Check if the given string is a valid ip address |
|
222 | - * |
|
223 | - * @param String $input |
|
224 | - * @param ValidationInfo $info |
|
225 | - * |
|
226 | - * @return string |
|
227 | - * @throws Invalid |
|
228 | - */ |
|
229 | - public static function ip($input, ValidationInfo $info = null) |
|
230 | - { |
|
231 | - $r = filter_var($input, FILTER_VALIDATE_IP); |
|
232 | - if ($r) { |
|
233 | - return $r; |
|
234 | - } |
|
218 | + /** |
|
219 | + * Validate IP Address |
|
220 | + * |
|
221 | + * Check if the given string is a valid ip address |
|
222 | + * |
|
223 | + * @param String $input |
|
224 | + * @param ValidationInfo $info |
|
225 | + * |
|
226 | + * @return string |
|
227 | + * @throws Invalid |
|
228 | + */ |
|
229 | + public static function ip($input, ValidationInfo $info = null) |
|
230 | + { |
|
231 | + $r = filter_var($input, FILTER_VALIDATE_IP); |
|
232 | + if ($r) { |
|
233 | + return $r; |
|
234 | + } |
|
235 | 235 | |
236 | - throw new Invalid('Expecting IP address in IPV6 or IPV4 format'); |
|
237 | - } |
|
236 | + throw new Invalid('Expecting IP address in IPV6 or IPV4 format'); |
|
237 | + } |
|
238 | 238 | |
239 | - /** |
|
240 | - * Validate Url |
|
241 | - * |
|
242 | - * Check if the given string is a valid url |
|
243 | - * |
|
244 | - * @param String $input |
|
245 | - * @param ValidationInfo $info |
|
246 | - * |
|
247 | - * @return string |
|
248 | - * @throws Invalid |
|
249 | - */ |
|
250 | - public static function url($input, ValidationInfo $info = null) |
|
251 | - { |
|
252 | - $r = filter_var($input, FILTER_VALIDATE_URL); |
|
253 | - if ($r) { |
|
254 | - return $r; |
|
255 | - } elseif ($info && $info->fix) { |
|
256 | - $r = filter_var($input, FILTER_SANITIZE_URL); |
|
257 | - return static::url($r); |
|
258 | - } |
|
259 | - throw new Invalid('Expecting url in `http://example.com` format'); |
|
260 | - } |
|
239 | + /** |
|
240 | + * Validate Url |
|
241 | + * |
|
242 | + * Check if the given string is a valid url |
|
243 | + * |
|
244 | + * @param String $input |
|
245 | + * @param ValidationInfo $info |
|
246 | + * |
|
247 | + * @return string |
|
248 | + * @throws Invalid |
|
249 | + */ |
|
250 | + public static function url($input, ValidationInfo $info = null) |
|
251 | + { |
|
252 | + $r = filter_var($input, FILTER_VALIDATE_URL); |
|
253 | + if ($r) { |
|
254 | + return $r; |
|
255 | + } elseif ($info && $info->fix) { |
|
256 | + $r = filter_var($input, FILTER_SANITIZE_URL); |
|
257 | + return static::url($r); |
|
258 | + } |
|
259 | + throw new Invalid('Expecting url in `http://example.com` format'); |
|
260 | + } |
|
261 | 261 | |
262 | - /** |
|
263 | - * MySQL Date |
|
264 | - * |
|
265 | - * Check if the given string is a valid date in YYYY-MM-DD format |
|
266 | - * |
|
267 | - * @param String $input |
|
268 | - * @param ValidationInfo $info |
|
269 | - * |
|
270 | - * @return string |
|
271 | - * @throws Invalid |
|
272 | - */ |
|
273 | - public static function date($input, ValidationInfo $info = null) |
|
274 | - { |
|
275 | - if ( |
|
276 | - preg_match( |
|
277 | - '#^(?P<year>\d{2}|\d{4})-(?P<month>\d{1,2})-(?P<day>\d{1,2})$#', |
|
278 | - $input, |
|
279 | - $date |
|
280 | - ) |
|
281 | - && checkdate($date['month'], $date['day'], $date['year']) |
|
282 | - ) { |
|
283 | - return $input; |
|
284 | - } |
|
285 | - throw new Invalid( |
|
286 | - 'Expecting date in `YYYY-MM-DD` format, such as `' |
|
287 | - . date("Y-m-d") . '`' |
|
288 | - ); |
|
289 | - } |
|
262 | + /** |
|
263 | + * MySQL Date |
|
264 | + * |
|
265 | + * Check if the given string is a valid date in YYYY-MM-DD format |
|
266 | + * |
|
267 | + * @param String $input |
|
268 | + * @param ValidationInfo $info |
|
269 | + * |
|
270 | + * @return string |
|
271 | + * @throws Invalid |
|
272 | + */ |
|
273 | + public static function date($input, ValidationInfo $info = null) |
|
274 | + { |
|
275 | + if ( |
|
276 | + preg_match( |
|
277 | + '#^(?P<year>\d{2}|\d{4})-(?P<month>\d{1,2})-(?P<day>\d{1,2})$#', |
|
278 | + $input, |
|
279 | + $date |
|
280 | + ) |
|
281 | + && checkdate($date['month'], $date['day'], $date['year']) |
|
282 | + ) { |
|
283 | + return $input; |
|
284 | + } |
|
285 | + throw new Invalid( |
|
286 | + 'Expecting date in `YYYY-MM-DD` format, such as `' |
|
287 | + . date("Y-m-d") . '`' |
|
288 | + ); |
|
289 | + } |
|
290 | 290 | |
291 | - /** |
|
292 | - * MySQL DateTime |
|
293 | - * |
|
294 | - * Check if the given string is a valid date and time in YYY-MM-DD HH:MM:SS format |
|
295 | - * |
|
296 | - * @param String $input |
|
297 | - * @param ValidationInfo $info |
|
298 | - * |
|
299 | - * @return string |
|
300 | - * @throws Invalid |
|
301 | - */ |
|
302 | - public static function datetime($input, ValidationInfo $info = null) |
|
303 | - { |
|
304 | - if ( |
|
305 | - preg_match('/^(?P<year>19\d\d|20\d\d)\-(?P<month>0[1-9]|1[0-2])\-' . |
|
306 | - '(?P<day>0\d|[1-2]\d|3[0-1]) (?P<h>0\d|1\d|2[0-3]' . |
|
307 | - ')\:(?P<i>[0-5][0-9])\:(?P<s>[0-5][0-9])$/', |
|
308 | - $input, $date) |
|
309 | - && checkdate($date['month'], $date['day'], $date['year']) |
|
310 | - ) { |
|
311 | - return $input; |
|
312 | - } |
|
313 | - throw new Invalid( |
|
314 | - 'Expecting date and time in `YYYY-MM-DD HH:MM:SS` format, such as `' |
|
315 | - . date("Y-m-d H:i:s") . '`' |
|
316 | - ); |
|
317 | - } |
|
291 | + /** |
|
292 | + * MySQL DateTime |
|
293 | + * |
|
294 | + * Check if the given string is a valid date and time in YYY-MM-DD HH:MM:SS format |
|
295 | + * |
|
296 | + * @param String $input |
|
297 | + * @param ValidationInfo $info |
|
298 | + * |
|
299 | + * @return string |
|
300 | + * @throws Invalid |
|
301 | + */ |
|
302 | + public static function datetime($input, ValidationInfo $info = null) |
|
303 | + { |
|
304 | + if ( |
|
305 | + preg_match('/^(?P<year>19\d\d|20\d\d)\-(?P<month>0[1-9]|1[0-2])\-' . |
|
306 | + '(?P<day>0\d|[1-2]\d|3[0-1]) (?P<h>0\d|1\d|2[0-3]' . |
|
307 | + ')\:(?P<i>[0-5][0-9])\:(?P<s>[0-5][0-9])$/', |
|
308 | + $input, $date) |
|
309 | + && checkdate($date['month'], $date['day'], $date['year']) |
|
310 | + ) { |
|
311 | + return $input; |
|
312 | + } |
|
313 | + throw new Invalid( |
|
314 | + 'Expecting date and time in `YYYY-MM-DD HH:MM:SS` format, such as `' |
|
315 | + . date("Y-m-d H:i:s") . '`' |
|
316 | + ); |
|
317 | + } |
|
318 | 318 | |
319 | - /** |
|
320 | - * Alias for Time |
|
321 | - * |
|
322 | - * Check if the given string is a valid time in HH:MM:SS format |
|
323 | - * |
|
324 | - * @param String $input |
|
325 | - * @param ValidationInfo $info |
|
326 | - * |
|
327 | - * @return string |
|
328 | - * @throws Invalid |
|
329 | - */ |
|
330 | - public static function time24($input, ValidationInfo $info = null) |
|
331 | - { |
|
332 | - return static::time($input, $info); |
|
333 | - } |
|
319 | + /** |
|
320 | + * Alias for Time |
|
321 | + * |
|
322 | + * Check if the given string is a valid time in HH:MM:SS format |
|
323 | + * |
|
324 | + * @param String $input |
|
325 | + * @param ValidationInfo $info |
|
326 | + * |
|
327 | + * @return string |
|
328 | + * @throws Invalid |
|
329 | + */ |
|
330 | + public static function time24($input, ValidationInfo $info = null) |
|
331 | + { |
|
332 | + return static::time($input, $info); |
|
333 | + } |
|
334 | 334 | |
335 | - /** |
|
336 | - * Time |
|
337 | - * |
|
338 | - * Check if the given string is a valid time in HH:MM:SS format |
|
339 | - * |
|
340 | - * @param String $input |
|
341 | - * @param ValidationInfo $info |
|
342 | - * |
|
343 | - * @return string |
|
344 | - * @throws Invalid |
|
345 | - */ |
|
346 | - public static function time($input, ValidationInfo $info = null) |
|
347 | - { |
|
348 | - if (preg_match('/^([01]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/', $input)) { |
|
349 | - return $input; |
|
350 | - } |
|
351 | - throw new Invalid( |
|
352 | - 'Expecting time in `HH:MM:SS` format, such as `' |
|
353 | - . date("H:i:s") . '`' |
|
354 | - ); |
|
355 | - } |
|
335 | + /** |
|
336 | + * Time |
|
337 | + * |
|
338 | + * Check if the given string is a valid time in HH:MM:SS format |
|
339 | + * |
|
340 | + * @param String $input |
|
341 | + * @param ValidationInfo $info |
|
342 | + * |
|
343 | + * @return string |
|
344 | + * @throws Invalid |
|
345 | + */ |
|
346 | + public static function time($input, ValidationInfo $info = null) |
|
347 | + { |
|
348 | + if (preg_match('/^([01]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/', $input)) { |
|
349 | + return $input; |
|
350 | + } |
|
351 | + throw new Invalid( |
|
352 | + 'Expecting time in `HH:MM:SS` format, such as `' |
|
353 | + . date("H:i:s") . '`' |
|
354 | + ); |
|
355 | + } |
|
356 | 356 | |
357 | - /** |
|
358 | - * Time in 12 hour format |
|
359 | - * |
|
360 | - * Check if the given string is a valid time 12 hour format |
|
361 | - * |
|
362 | - * @param String $input |
|
363 | - * @param ValidationInfo $info |
|
364 | - * |
|
365 | - * @return string |
|
366 | - * @throws Invalid |
|
367 | - */ |
|
368 | - public static function time12($input, ValidationInfo $info = null) |
|
369 | - { |
|
370 | - if (preg_match( |
|
371 | - '/^([1-9]|1[0-2]|0[1-9]){1}(:[0-5][0-9])?\s?([aApP][mM]{1})?$/', |
|
372 | - $input) |
|
373 | - ) { |
|
374 | - return $input; |
|
375 | - } |
|
376 | - throw new Invalid( |
|
377 | - 'Expecting time in 12 hour format, such as `08:00AM` and `10:05:11`' |
|
378 | - ); |
|
379 | - } |
|
357 | + /** |
|
358 | + * Time in 12 hour format |
|
359 | + * |
|
360 | + * Check if the given string is a valid time 12 hour format |
|
361 | + * |
|
362 | + * @param String $input |
|
363 | + * @param ValidationInfo $info |
|
364 | + * |
|
365 | + * @return string |
|
366 | + * @throws Invalid |
|
367 | + */ |
|
368 | + public static function time12($input, ValidationInfo $info = null) |
|
369 | + { |
|
370 | + if (preg_match( |
|
371 | + '/^([1-9]|1[0-2]|0[1-9]){1}(:[0-5][0-9])?\s?([aApP][mM]{1})?$/', |
|
372 | + $input) |
|
373 | + ) { |
|
374 | + return $input; |
|
375 | + } |
|
376 | + throw new Invalid( |
|
377 | + 'Expecting time in 12 hour format, such as `08:00AM` and `10:05:11`' |
|
378 | + ); |
|
379 | + } |
|
380 | 380 | |
381 | - /** |
|
382 | - * Unix Timestamp |
|
383 | - * |
|
384 | - * Check if the given value is a valid timestamp |
|
385 | - * |
|
386 | - * @param String $input |
|
387 | - * @param ValidationInfo $info |
|
388 | - * |
|
389 | - * @return int |
|
390 | - * @throws Invalid |
|
391 | - */ |
|
392 | - public static function timestamp($input, ValidationInfo $info = null) |
|
393 | - { |
|
394 | - if ((string)(int)$input == $input |
|
395 | - && ($input <= PHP_INT_MAX) |
|
396 | - && ($input >= ~PHP_INT_MAX) |
|
397 | - ) { |
|
398 | - return (int)$input; |
|
399 | - } |
|
400 | - throw new Invalid('Expecting unix timestamp, such as ' . time()); |
|
401 | - } |
|
381 | + /** |
|
382 | + * Unix Timestamp |
|
383 | + * |
|
384 | + * Check if the given value is a valid timestamp |
|
385 | + * |
|
386 | + * @param String $input |
|
387 | + * @param ValidationInfo $info |
|
388 | + * |
|
389 | + * @return int |
|
390 | + * @throws Invalid |
|
391 | + */ |
|
392 | + public static function timestamp($input, ValidationInfo $info = null) |
|
393 | + { |
|
394 | + if ((string)(int)$input == $input |
|
395 | + && ($input <= PHP_INT_MAX) |
|
396 | + && ($input >= ~PHP_INT_MAX) |
|
397 | + ) { |
|
398 | + return (int)$input; |
|
399 | + } |
|
400 | + throw new Invalid('Expecting unix timestamp, such as ' . time()); |
|
401 | + } |
|
402 | 402 | |
403 | - /** |
|
404 | - * Validate the given input |
|
405 | - * |
|
406 | - * Validates the input and attempts to fix it when fix is requested |
|
407 | - * |
|
408 | - * @param mixed $input |
|
409 | - * @param ValidationInfo $info |
|
410 | - * @param null $full |
|
411 | - * |
|
412 | - * @throws \Exception |
|
413 | - * @return array|bool|float|int|mixed|null|number|string |
|
414 | - */ |
|
415 | - public static function validate($input, ValidationInfo $info, $full = null) |
|
416 | - { |
|
417 | - $html = Scope::get('Restler')->responseFormat instanceof HtmlFormat; |
|
418 | - $name = $html ? "<strong>$info->label</strong>" : "`$info->name`"; |
|
419 | - if ( |
|
420 | - isset(static::$preFilters['*']) && |
|
421 | - is_scalar($input) && |
|
422 | - is_callable($func = static::$preFilters['*']) |
|
423 | - ) { |
|
424 | - $input = $func($input); |
|
425 | - } |
|
426 | - if ( |
|
427 | - isset(static::$preFilters[$info->type]) && |
|
428 | - (is_scalar($input) || !empty($info->children)) && |
|
429 | - is_callable($func = static::$preFilters[$info->type]) |
|
430 | - ) { |
|
431 | - $input = $func($input); |
|
432 | - } |
|
433 | - try { |
|
434 | - if (is_null($input)) { |
|
435 | - if ($info->required) { |
|
436 | - throw new RestException (400, |
|
437 | - "$name is required."); |
|
438 | - } |
|
439 | - return null; |
|
440 | - } |
|
441 | - $error = isset ($info->message) |
|
442 | - ? $info->message |
|
443 | - : "Invalid value specified for $name"; |
|
403 | + /** |
|
404 | + * Validate the given input |
|
405 | + * |
|
406 | + * Validates the input and attempts to fix it when fix is requested |
|
407 | + * |
|
408 | + * @param mixed $input |
|
409 | + * @param ValidationInfo $info |
|
410 | + * @param null $full |
|
411 | + * |
|
412 | + * @throws \Exception |
|
413 | + * @return array|bool|float|int|mixed|null|number|string |
|
414 | + */ |
|
415 | + public static function validate($input, ValidationInfo $info, $full = null) |
|
416 | + { |
|
417 | + $html = Scope::get('Restler')->responseFormat instanceof HtmlFormat; |
|
418 | + $name = $html ? "<strong>$info->label</strong>" : "`$info->name`"; |
|
419 | + if ( |
|
420 | + isset(static::$preFilters['*']) && |
|
421 | + is_scalar($input) && |
|
422 | + is_callable($func = static::$preFilters['*']) |
|
423 | + ) { |
|
424 | + $input = $func($input); |
|
425 | + } |
|
426 | + if ( |
|
427 | + isset(static::$preFilters[$info->type]) && |
|
428 | + (is_scalar($input) || !empty($info->children)) && |
|
429 | + is_callable($func = static::$preFilters[$info->type]) |
|
430 | + ) { |
|
431 | + $input = $func($input); |
|
432 | + } |
|
433 | + try { |
|
434 | + if (is_null($input)) { |
|
435 | + if ($info->required) { |
|
436 | + throw new RestException (400, |
|
437 | + "$name is required."); |
|
438 | + } |
|
439 | + return null; |
|
440 | + } |
|
441 | + $error = isset ($info->message) |
|
442 | + ? $info->message |
|
443 | + : "Invalid value specified for $name"; |
|
444 | 444 | |
445 | - //if a validation method is specified |
|
446 | - if (!empty($info->method)) { |
|
447 | - $method = $info->method; |
|
448 | - $info->method = ''; |
|
449 | - $r = self::validate($input, $info); |
|
450 | - return $info->apiClassInstance->{$method} ($r); |
|
451 | - } |
|
445 | + //if a validation method is specified |
|
446 | + if (!empty($info->method)) { |
|
447 | + $method = $info->method; |
|
448 | + $info->method = ''; |
|
449 | + $r = self::validate($input, $info); |
|
450 | + return $info->apiClassInstance->{$method} ($r); |
|
451 | + } |
|
452 | 452 | |
453 | - // when type is an array check if it passes for any type |
|
454 | - if (is_array($info->type)) { |
|
455 | - //trace("types are ".print_r($info->type, true)); |
|
456 | - $types = $info->type; |
|
457 | - foreach ($types as $type) { |
|
458 | - $info->type = $type; |
|
459 | - try { |
|
460 | - $r = self::validate($input, $info); |
|
461 | - if ($r !== false) { |
|
462 | - return $r; |
|
463 | - } |
|
464 | - } catch (RestException $e) { |
|
465 | - // just continue |
|
466 | - } |
|
467 | - } |
|
468 | - throw new RestException (400, $error); |
|
469 | - } |
|
453 | + // when type is an array check if it passes for any type |
|
454 | + if (is_array($info->type)) { |
|
455 | + //trace("types are ".print_r($info->type, true)); |
|
456 | + $types = $info->type; |
|
457 | + foreach ($types as $type) { |
|
458 | + $info->type = $type; |
|
459 | + try { |
|
460 | + $r = self::validate($input, $info); |
|
461 | + if ($r !== false) { |
|
462 | + return $r; |
|
463 | + } |
|
464 | + } catch (RestException $e) { |
|
465 | + // just continue |
|
466 | + } |
|
467 | + } |
|
468 | + throw new RestException (400, $error); |
|
469 | + } |
|
470 | 470 | |
471 | - //patterns are supported only for non numeric types |
|
472 | - if (isset ($info->pattern) |
|
473 | - && $info->type != 'int' |
|
474 | - && $info->type != 'float' |
|
475 | - && $info->type != 'number' |
|
476 | - ) { |
|
477 | - if (!preg_match($info->pattern, $input)) { |
|
478 | - throw new RestException (400, $error); |
|
479 | - } |
|
480 | - } |
|
471 | + //patterns are supported only for non numeric types |
|
472 | + if (isset ($info->pattern) |
|
473 | + && $info->type != 'int' |
|
474 | + && $info->type != 'float' |
|
475 | + && $info->type != 'number' |
|
476 | + ) { |
|
477 | + if (!preg_match($info->pattern, $input)) { |
|
478 | + throw new RestException (400, $error); |
|
479 | + } |
|
480 | + } |
|
481 | 481 | |
482 | - if (isset ($info->choice)) { |
|
483 | - if (!$info->required && empty($input)) { |
|
484 | - //since its optional, and empty let it pass. |
|
485 | - $input = null; |
|
486 | - } elseif (is_array($input)) { |
|
487 | - foreach ($input as $i) { |
|
488 | - if (!in_array($i, $info->choice)) { |
|
489 | - $error .= ". Expected one of (" . implode(',', $info->choice) . ")."; |
|
490 | - throw new RestException (400, $error); |
|
491 | - } |
|
492 | - } |
|
493 | - } elseif (!in_array($input, $info->choice)) { |
|
494 | - $error .= ". Expected one of (" . implode(',', $info->choice) . ")."; |
|
495 | - throw new RestException (400, $error); |
|
496 | - } |
|
497 | - } |
|
482 | + if (isset ($info->choice)) { |
|
483 | + if (!$info->required && empty($input)) { |
|
484 | + //since its optional, and empty let it pass. |
|
485 | + $input = null; |
|
486 | + } elseif (is_array($input)) { |
|
487 | + foreach ($input as $i) { |
|
488 | + if (!in_array($i, $info->choice)) { |
|
489 | + $error .= ". Expected one of (" . implode(',', $info->choice) . ")."; |
|
490 | + throw new RestException (400, $error); |
|
491 | + } |
|
492 | + } |
|
493 | + } elseif (!in_array($input, $info->choice)) { |
|
494 | + $error .= ". Expected one of (" . implode(',', $info->choice) . ")."; |
|
495 | + throw new RestException (400, $error); |
|
496 | + } |
|
497 | + } |
|
498 | 498 | |
499 | - if (method_exists($class = get_called_class(), $info->type) && $info->type != 'validate') { |
|
500 | - if (!$info->required && empty($input)) { |
|
501 | - //optional parameter with a empty value assume null |
|
502 | - return null; |
|
503 | - } |
|
504 | - try { |
|
505 | - return call_user_func("$class::$info->type", $input, $info); |
|
506 | - } catch (Invalid $e) { |
|
507 | - throw new RestException(400, $error . '. ' . $e->getMessage()); |
|
508 | - } |
|
509 | - } |
|
499 | + if (method_exists($class = get_called_class(), $info->type) && $info->type != 'validate') { |
|
500 | + if (!$info->required && empty($input)) { |
|
501 | + //optional parameter with a empty value assume null |
|
502 | + return null; |
|
503 | + } |
|
504 | + try { |
|
505 | + return call_user_func("$class::$info->type", $input, $info); |
|
506 | + } catch (Invalid $e) { |
|
507 | + throw new RestException(400, $error . '. ' . $e->getMessage()); |
|
508 | + } |
|
509 | + } |
|
510 | 510 | |
511 | - switch ($info->type) { |
|
512 | - case 'int' : |
|
513 | - case 'float' : |
|
514 | - case 'number' : |
|
515 | - if (!is_numeric($input)) { |
|
516 | - $error .= '. Expecting ' |
|
517 | - . ($info->type == 'int' ? 'integer' : 'numeric') |
|
518 | - . ' value'; |
|
519 | - break; |
|
520 | - } |
|
521 | - if ($info->type == 'int' && (int)$input != $input) { |
|
522 | - if ($info->fix) { |
|
523 | - $r = (int)$input; |
|
524 | - } else { |
|
525 | - $error .= '. Expecting integer value'; |
|
526 | - break; |
|
527 | - } |
|
528 | - } else { |
|
529 | - $r = $info->numericValue($input); |
|
530 | - } |
|
531 | - if (isset ($info->min) && $r < $info->min) { |
|
532 | - if ($info->fix) { |
|
533 | - $r = $info->min; |
|
534 | - } else { |
|
535 | - $error .= ". Minimum required value is $info->min."; |
|
536 | - break; |
|
537 | - } |
|
538 | - } |
|
539 | - if (isset ($info->max) && $r > $info->max) { |
|
540 | - if ($info->fix) { |
|
541 | - $r = $info->max; |
|
542 | - } else { |
|
543 | - $error .= ". Maximum allowed value is $info->max."; |
|
544 | - break; |
|
545 | - } |
|
546 | - } |
|
547 | - return $r; |
|
511 | + switch ($info->type) { |
|
512 | + case 'int' : |
|
513 | + case 'float' : |
|
514 | + case 'number' : |
|
515 | + if (!is_numeric($input)) { |
|
516 | + $error .= '. Expecting ' |
|
517 | + . ($info->type == 'int' ? 'integer' : 'numeric') |
|
518 | + . ' value'; |
|
519 | + break; |
|
520 | + } |
|
521 | + if ($info->type == 'int' && (int)$input != $input) { |
|
522 | + if ($info->fix) { |
|
523 | + $r = (int)$input; |
|
524 | + } else { |
|
525 | + $error .= '. Expecting integer value'; |
|
526 | + break; |
|
527 | + } |
|
528 | + } else { |
|
529 | + $r = $info->numericValue($input); |
|
530 | + } |
|
531 | + if (isset ($info->min) && $r < $info->min) { |
|
532 | + if ($info->fix) { |
|
533 | + $r = $info->min; |
|
534 | + } else { |
|
535 | + $error .= ". Minimum required value is $info->min."; |
|
536 | + break; |
|
537 | + } |
|
538 | + } |
|
539 | + if (isset ($info->max) && $r > $info->max) { |
|
540 | + if ($info->fix) { |
|
541 | + $r = $info->max; |
|
542 | + } else { |
|
543 | + $error .= ". Maximum allowed value is $info->max."; |
|
544 | + break; |
|
545 | + } |
|
546 | + } |
|
547 | + return $r; |
|
548 | 548 | |
549 | - case 'string' : |
|
550 | - case 'password' : //password fields with string |
|
551 | - case 'search' : //search field with string |
|
552 | - if (is_bool($input)) $input = $input ? 'true' : 'false'; |
|
553 | - if (!is_string($input)) { |
|
554 | - $error .= '. Expecting alpha numeric value'; |
|
555 | - break; |
|
556 | - } |
|
557 | - if ($info->required && $input === '') { |
|
558 | - $error = "$name is required."; |
|
559 | - break; |
|
560 | - } |
|
561 | - $r = strlen($input); |
|
562 | - if (isset ($info->min) && $r < $info->min) { |
|
563 | - if ($info->fix) { |
|
564 | - $input = str_pad($input, $info->min, $input); |
|
565 | - } else { |
|
566 | - $char = $info->min > 1 ? 'characters' : 'character'; |
|
567 | - $error .= ". Minimum $info->min $char required."; |
|
568 | - break; |
|
569 | - } |
|
570 | - } |
|
571 | - if (isset ($info->max) && $r > $info->max) { |
|
572 | - if ($info->fix) { |
|
573 | - $input = substr($input, 0, $info->max); |
|
574 | - } else { |
|
575 | - $char = $info->max > 1 ? 'characters' : 'character'; |
|
576 | - $error .= ". Maximum $info->max $char allowed."; |
|
577 | - break; |
|
578 | - } |
|
579 | - } |
|
580 | - return $input; |
|
549 | + case 'string' : |
|
550 | + case 'password' : //password fields with string |
|
551 | + case 'search' : //search field with string |
|
552 | + if (is_bool($input)) $input = $input ? 'true' : 'false'; |
|
553 | + if (!is_string($input)) { |
|
554 | + $error .= '. Expecting alpha numeric value'; |
|
555 | + break; |
|
556 | + } |
|
557 | + if ($info->required && $input === '') { |
|
558 | + $error = "$name is required."; |
|
559 | + break; |
|
560 | + } |
|
561 | + $r = strlen($input); |
|
562 | + if (isset ($info->min) && $r < $info->min) { |
|
563 | + if ($info->fix) { |
|
564 | + $input = str_pad($input, $info->min, $input); |
|
565 | + } else { |
|
566 | + $char = $info->min > 1 ? 'characters' : 'character'; |
|
567 | + $error .= ". Minimum $info->min $char required."; |
|
568 | + break; |
|
569 | + } |
|
570 | + } |
|
571 | + if (isset ($info->max) && $r > $info->max) { |
|
572 | + if ($info->fix) { |
|
573 | + $input = substr($input, 0, $info->max); |
|
574 | + } else { |
|
575 | + $char = $info->max > 1 ? 'characters' : 'character'; |
|
576 | + $error .= ". Maximum $info->max $char allowed."; |
|
577 | + break; |
|
578 | + } |
|
579 | + } |
|
580 | + return $input; |
|
581 | 581 | |
582 | - case 'bool': |
|
583 | - case 'boolean': |
|
584 | - if (is_bool($input)) { |
|
585 | - return $input; |
|
586 | - } |
|
587 | - if (is_numeric($input)) { |
|
588 | - if ($input == 1) { |
|
589 | - return true; |
|
590 | - } |
|
591 | - if ($input == 0) { |
|
592 | - return false; |
|
593 | - } |
|
594 | - } elseif (is_string($input)) { |
|
595 | - switch (strtolower($input)) { |
|
596 | - case 'true': |
|
597 | - return true; |
|
598 | - case 'false': |
|
599 | - return false; |
|
600 | - } |
|
601 | - } |
|
602 | - if ($info->fix) { |
|
603 | - return $input ? true : false; |
|
604 | - } |
|
605 | - $error .= '. Expecting boolean value'; |
|
606 | - break; |
|
607 | - case 'array': |
|
608 | - if ($info->fix && is_string($input)) { |
|
609 | - $input = explode(CommentParser::$arrayDelimiter, $input); |
|
610 | - } |
|
611 | - if (is_array($input)) { |
|
612 | - $contentType = |
|
613 | - Util::nestedValue($info, 'contentType') ?: null; |
|
614 | - if ($info->fix) { |
|
615 | - if ($contentType == 'indexed') { |
|
616 | - $input = $info->filterArray($input, true); |
|
617 | - } elseif ($contentType == 'associative') { |
|
618 | - $input = $info->filterArray($input, false); |
|
619 | - } |
|
620 | - } elseif ( |
|
621 | - $contentType == 'indexed' && |
|
622 | - array_values($input) != $input |
|
623 | - ) { |
|
624 | - $error .= '. Expecting a list of items but an item is given'; |
|
625 | - break; |
|
626 | - } elseif ( |
|
627 | - $contentType == 'associative' && |
|
628 | - array_values($input) == $input && |
|
629 | - count($input) |
|
630 | - ) { |
|
631 | - $error .= '. Expecting an item but a list is given'; |
|
632 | - break; |
|
633 | - } |
|
634 | - $r = count($input); |
|
635 | - if (isset ($info->min) && $r < $info->min) { |
|
636 | - $item = $info->max > 1 ? 'items' : 'item'; |
|
637 | - $error .= ". Minimum $info->min $item required."; |
|
638 | - break; |
|
639 | - } |
|
640 | - if (isset ($info->max) && $r > $info->max) { |
|
641 | - if ($info->fix) { |
|
642 | - $input = array_slice($input, 0, $info->max); |
|
643 | - } else { |
|
644 | - $item = $info->max > 1 ? 'items' : 'item'; |
|
645 | - $error .= ". Maximum $info->max $item allowed."; |
|
646 | - break; |
|
647 | - } |
|
648 | - } |
|
649 | - if ( |
|
650 | - isset($contentType) && |
|
651 | - $contentType != 'associative' && |
|
652 | - $contentType != 'indexed' |
|
653 | - ) { |
|
654 | - $name = $info->name; |
|
655 | - $info->type = $contentType; |
|
656 | - unset($info->contentType); |
|
657 | - unset($info->min); |
|
658 | - unset($info->max); |
|
659 | - foreach ($input as $key => $chinput) { |
|
660 | - $info->name = "{$name}[$key]"; |
|
661 | - $input[$key] = static::validate($chinput, $info); |
|
662 | - } |
|
663 | - } |
|
664 | - return $input; |
|
665 | - } elseif (isset($contentType)) { |
|
666 | - $error .= '. Expecting items of type ' . |
|
667 | - ($html ? "<strong>$contentType</strong>" : "`$contentType`"); |
|
668 | - break; |
|
669 | - } |
|
670 | - break; |
|
671 | - case 'mixed': |
|
672 | - case 'unknown_type': |
|
673 | - case 'unknown': |
|
674 | - case null: //treat as unknown |
|
675 | - return $input; |
|
676 | - default : |
|
677 | - if (!is_array($input)) { |
|
678 | - break; |
|
679 | - } |
|
680 | - //do type conversion |
|
681 | - if (class_exists($info->type)) { |
|
682 | - $input = $info->filterArray($input, false); |
|
683 | - $implements = class_implements($info->type); |
|
684 | - if ( |
|
685 | - is_array($implements) && |
|
686 | - in_array('Luracast\\Restler\\Data\\iValueObject', $implements) |
|
687 | - ) { |
|
688 | - return call_user_func( |
|
689 | - "{$info->type}::__set_state", $input |
|
690 | - ); |
|
691 | - } |
|
692 | - $class = $info->type; |
|
693 | - $instance = new $class(); |
|
694 | - if (is_array($info->children)) { |
|
695 | - if ( |
|
696 | - empty($input) || |
|
697 | - !is_array($input) || |
|
698 | - $input === array_values($input) |
|
699 | - ) { |
|
700 | - $error .= '. Expecting an item of type ' . |
|
701 | - ($html ? "<strong>$info->type</strong>" : "`$info->type`"); |
|
702 | - break; |
|
703 | - } |
|
704 | - foreach ($info->children as $key => $value) { |
|
705 | - $cv = new ValidationInfo($value); |
|
706 | - $cv->name = "{$info->name}[$key]"; |
|
707 | - if (array_key_exists($key, $input) || $cv->required) { |
|
708 | - $instance->{$key} = static::validate( |
|
709 | - Util::nestedValue($input, $key), |
|
710 | - $cv |
|
711 | - ); |
|
712 | - } |
|
713 | - } |
|
714 | - } |
|
715 | - return $instance; |
|
716 | - } |
|
717 | - } |
|
718 | - throw new RestException (400, $error); |
|
719 | - } catch (\Exception $e) { |
|
720 | - static::$exceptions[$info->name] = $e; |
|
721 | - if (static::$holdException) { |
|
722 | - return null; |
|
723 | - } |
|
724 | - throw $e; |
|
725 | - } |
|
726 | - } |
|
582 | + case 'bool': |
|
583 | + case 'boolean': |
|
584 | + if (is_bool($input)) { |
|
585 | + return $input; |
|
586 | + } |
|
587 | + if (is_numeric($input)) { |
|
588 | + if ($input == 1) { |
|
589 | + return true; |
|
590 | + } |
|
591 | + if ($input == 0) { |
|
592 | + return false; |
|
593 | + } |
|
594 | + } elseif (is_string($input)) { |
|
595 | + switch (strtolower($input)) { |
|
596 | + case 'true': |
|
597 | + return true; |
|
598 | + case 'false': |
|
599 | + return false; |
|
600 | + } |
|
601 | + } |
|
602 | + if ($info->fix) { |
|
603 | + return $input ? true : false; |
|
604 | + } |
|
605 | + $error .= '. Expecting boolean value'; |
|
606 | + break; |
|
607 | + case 'array': |
|
608 | + if ($info->fix && is_string($input)) { |
|
609 | + $input = explode(CommentParser::$arrayDelimiter, $input); |
|
610 | + } |
|
611 | + if (is_array($input)) { |
|
612 | + $contentType = |
|
613 | + Util::nestedValue($info, 'contentType') ?: null; |
|
614 | + if ($info->fix) { |
|
615 | + if ($contentType == 'indexed') { |
|
616 | + $input = $info->filterArray($input, true); |
|
617 | + } elseif ($contentType == 'associative') { |
|
618 | + $input = $info->filterArray($input, false); |
|
619 | + } |
|
620 | + } elseif ( |
|
621 | + $contentType == 'indexed' && |
|
622 | + array_values($input) != $input |
|
623 | + ) { |
|
624 | + $error .= '. Expecting a list of items but an item is given'; |
|
625 | + break; |
|
626 | + } elseif ( |
|
627 | + $contentType == 'associative' && |
|
628 | + array_values($input) == $input && |
|
629 | + count($input) |
|
630 | + ) { |
|
631 | + $error .= '. Expecting an item but a list is given'; |
|
632 | + break; |
|
633 | + } |
|
634 | + $r = count($input); |
|
635 | + if (isset ($info->min) && $r < $info->min) { |
|
636 | + $item = $info->max > 1 ? 'items' : 'item'; |
|
637 | + $error .= ". Minimum $info->min $item required."; |
|
638 | + break; |
|
639 | + } |
|
640 | + if (isset ($info->max) && $r > $info->max) { |
|
641 | + if ($info->fix) { |
|
642 | + $input = array_slice($input, 0, $info->max); |
|
643 | + } else { |
|
644 | + $item = $info->max > 1 ? 'items' : 'item'; |
|
645 | + $error .= ". Maximum $info->max $item allowed."; |
|
646 | + break; |
|
647 | + } |
|
648 | + } |
|
649 | + if ( |
|
650 | + isset($contentType) && |
|
651 | + $contentType != 'associative' && |
|
652 | + $contentType != 'indexed' |
|
653 | + ) { |
|
654 | + $name = $info->name; |
|
655 | + $info->type = $contentType; |
|
656 | + unset($info->contentType); |
|
657 | + unset($info->min); |
|
658 | + unset($info->max); |
|
659 | + foreach ($input as $key => $chinput) { |
|
660 | + $info->name = "{$name}[$key]"; |
|
661 | + $input[$key] = static::validate($chinput, $info); |
|
662 | + } |
|
663 | + } |
|
664 | + return $input; |
|
665 | + } elseif (isset($contentType)) { |
|
666 | + $error .= '. Expecting items of type ' . |
|
667 | + ($html ? "<strong>$contentType</strong>" : "`$contentType`"); |
|
668 | + break; |
|
669 | + } |
|
670 | + break; |
|
671 | + case 'mixed': |
|
672 | + case 'unknown_type': |
|
673 | + case 'unknown': |
|
674 | + case null: //treat as unknown |
|
675 | + return $input; |
|
676 | + default : |
|
677 | + if (!is_array($input)) { |
|
678 | + break; |
|
679 | + } |
|
680 | + //do type conversion |
|
681 | + if (class_exists($info->type)) { |
|
682 | + $input = $info->filterArray($input, false); |
|
683 | + $implements = class_implements($info->type); |
|
684 | + if ( |
|
685 | + is_array($implements) && |
|
686 | + in_array('Luracast\\Restler\\Data\\iValueObject', $implements) |
|
687 | + ) { |
|
688 | + return call_user_func( |
|
689 | + "{$info->type}::__set_state", $input |
|
690 | + ); |
|
691 | + } |
|
692 | + $class = $info->type; |
|
693 | + $instance = new $class(); |
|
694 | + if (is_array($info->children)) { |
|
695 | + if ( |
|
696 | + empty($input) || |
|
697 | + !is_array($input) || |
|
698 | + $input === array_values($input) |
|
699 | + ) { |
|
700 | + $error .= '. Expecting an item of type ' . |
|
701 | + ($html ? "<strong>$info->type</strong>" : "`$info->type`"); |
|
702 | + break; |
|
703 | + } |
|
704 | + foreach ($info->children as $key => $value) { |
|
705 | + $cv = new ValidationInfo($value); |
|
706 | + $cv->name = "{$info->name}[$key]"; |
|
707 | + if (array_key_exists($key, $input) || $cv->required) { |
|
708 | + $instance->{$key} = static::validate( |
|
709 | + Util::nestedValue($input, $key), |
|
710 | + $cv |
|
711 | + ); |
|
712 | + } |
|
713 | + } |
|
714 | + } |
|
715 | + return $instance; |
|
716 | + } |
|
717 | + } |
|
718 | + throw new RestException (400, $error); |
|
719 | + } catch (\Exception $e) { |
|
720 | + static::$exceptions[$info->name] = $e; |
|
721 | + if (static::$holdException) { |
|
722 | + return null; |
|
723 | + } |
|
724 | + throw $e; |
|
725 | + } |
|
726 | + } |
|
727 | 727 | } |
@@ -188,7 +188,7 @@ discard block |
||
188 | 188 | if (is_numeric($input) && '-' != substr($input, 0, 1)) { |
189 | 189 | return $input; |
190 | 190 | } |
191 | - throw new Invalid('Expecting phone number, a numeric value ' . |
|
191 | + throw new Invalid('Expecting phone number, a numeric value '. |
|
192 | 192 | 'with optional `+` prefix'); |
193 | 193 | } |
194 | 194 | |
@@ -284,7 +284,7 @@ discard block |
||
284 | 284 | } |
285 | 285 | throw new Invalid( |
286 | 286 | 'Expecting date in `YYYY-MM-DD` format, such as `' |
287 | - . date("Y-m-d") . '`' |
|
287 | + . date("Y-m-d").'`' |
|
288 | 288 | ); |
289 | 289 | } |
290 | 290 | |
@@ -302,8 +302,8 @@ discard block |
||
302 | 302 | public static function datetime($input, ValidationInfo $info = null) |
303 | 303 | { |
304 | 304 | if ( |
305 | - preg_match('/^(?P<year>19\d\d|20\d\d)\-(?P<month>0[1-9]|1[0-2])\-' . |
|
306 | - '(?P<day>0\d|[1-2]\d|3[0-1]) (?P<h>0\d|1\d|2[0-3]' . |
|
305 | + preg_match('/^(?P<year>19\d\d|20\d\d)\-(?P<month>0[1-9]|1[0-2])\-'. |
|
306 | + '(?P<day>0\d|[1-2]\d|3[0-1]) (?P<h>0\d|1\d|2[0-3]'. |
|
307 | 307 | ')\:(?P<i>[0-5][0-9])\:(?P<s>[0-5][0-9])$/', |
308 | 308 | $input, $date) |
309 | 309 | && checkdate($date['month'], $date['day'], $date['year']) |
@@ -312,7 +312,7 @@ discard block |
||
312 | 312 | } |
313 | 313 | throw new Invalid( |
314 | 314 | 'Expecting date and time in `YYYY-MM-DD HH:MM:SS` format, such as `' |
315 | - . date("Y-m-d H:i:s") . '`' |
|
315 | + . date("Y-m-d H:i:s").'`' |
|
316 | 316 | ); |
317 | 317 | } |
318 | 318 | |
@@ -350,7 +350,7 @@ discard block |
||
350 | 350 | } |
351 | 351 | throw new Invalid( |
352 | 352 | 'Expecting time in `HH:MM:SS` format, such as `' |
353 | - . date("H:i:s") . '`' |
|
353 | + . date("H:i:s").'`' |
|
354 | 354 | ); |
355 | 355 | } |
356 | 356 | |
@@ -391,13 +391,13 @@ discard block |
||
391 | 391 | */ |
392 | 392 | public static function timestamp($input, ValidationInfo $info = null) |
393 | 393 | { |
394 | - if ((string)(int)$input == $input |
|
394 | + if ((string) (int) $input == $input |
|
395 | 395 | && ($input <= PHP_INT_MAX) |
396 | 396 | && ($input >= ~PHP_INT_MAX) |
397 | 397 | ) { |
398 | - return (int)$input; |
|
398 | + return (int) $input; |
|
399 | 399 | } |
400 | - throw new Invalid('Expecting unix timestamp, such as ' . time()); |
|
400 | + throw new Invalid('Expecting unix timestamp, such as '.time()); |
|
401 | 401 | } |
402 | 402 | |
403 | 403 | /** |
@@ -433,7 +433,7 @@ discard block |
||
433 | 433 | try { |
434 | 434 | if (is_null($input)) { |
435 | 435 | if ($info->required) { |
436 | - throw new RestException (400, |
|
436 | + throw new RestException(400, |
|
437 | 437 | "$name is required."); |
438 | 438 | } |
439 | 439 | return null; |
@@ -465,7 +465,7 @@ discard block |
||
465 | 465 | // just continue |
466 | 466 | } |
467 | 467 | } |
468 | - throw new RestException (400, $error); |
|
468 | + throw new RestException(400, $error); |
|
469 | 469 | } |
470 | 470 | |
471 | 471 | //patterns are supported only for non numeric types |
@@ -475,7 +475,7 @@ discard block |
||
475 | 475 | && $info->type != 'number' |
476 | 476 | ) { |
477 | 477 | if (!preg_match($info->pattern, $input)) { |
478 | - throw new RestException (400, $error); |
|
478 | + throw new RestException(400, $error); |
|
479 | 479 | } |
480 | 480 | } |
481 | 481 | |
@@ -486,13 +486,13 @@ discard block |
||
486 | 486 | } elseif (is_array($input)) { |
487 | 487 | foreach ($input as $i) { |
488 | 488 | if (!in_array($i, $info->choice)) { |
489 | - $error .= ". Expected one of (" . implode(',', $info->choice) . ")."; |
|
490 | - throw new RestException (400, $error); |
|
489 | + $error .= ". Expected one of (".implode(',', $info->choice).")."; |
|
490 | + throw new RestException(400, $error); |
|
491 | 491 | } |
492 | 492 | } |
493 | 493 | } elseif (!in_array($input, $info->choice)) { |
494 | - $error .= ". Expected one of (" . implode(',', $info->choice) . ")."; |
|
495 | - throw new RestException (400, $error); |
|
494 | + $error .= ". Expected one of (".implode(',', $info->choice).")."; |
|
495 | + throw new RestException(400, $error); |
|
496 | 496 | } |
497 | 497 | } |
498 | 498 | |
@@ -504,7 +504,7 @@ discard block |
||
504 | 504 | try { |
505 | 505 | return call_user_func("$class::$info->type", $input, $info); |
506 | 506 | } catch (Invalid $e) { |
507 | - throw new RestException(400, $error . '. ' . $e->getMessage()); |
|
507 | + throw new RestException(400, $error.'. '.$e->getMessage()); |
|
508 | 508 | } |
509 | 509 | } |
510 | 510 | |
@@ -518,9 +518,9 @@ discard block |
||
518 | 518 | . ' value'; |
519 | 519 | break; |
520 | 520 | } |
521 | - if ($info->type == 'int' && (int)$input != $input) { |
|
521 | + if ($info->type == 'int' && (int) $input != $input) { |
|
522 | 522 | if ($info->fix) { |
523 | - $r = (int)$input; |
|
523 | + $r = (int) $input; |
|
524 | 524 | } else { |
525 | 525 | $error .= '. Expecting integer value'; |
526 | 526 | break; |
@@ -663,7 +663,7 @@ discard block |
||
663 | 663 | } |
664 | 664 | return $input; |
665 | 665 | } elseif (isset($contentType)) { |
666 | - $error .= '. Expecting items of type ' . |
|
666 | + $error .= '. Expecting items of type '. |
|
667 | 667 | ($html ? "<strong>$contentType</strong>" : "`$contentType`"); |
668 | 668 | break; |
669 | 669 | } |
@@ -697,7 +697,7 @@ discard block |
||
697 | 697 | !is_array($input) || |
698 | 698 | $input === array_values($input) |
699 | 699 | ) { |
700 | - $error .= '. Expecting an item of type ' . |
|
700 | + $error .= '. Expecting an item of type '. |
|
701 | 701 | ($html ? "<strong>$info->type</strong>" : "`$info->type`"); |
702 | 702 | break; |
703 | 703 | } |
@@ -715,7 +715,7 @@ discard block |
||
715 | 715 | return $instance; |
716 | 716 | } |
717 | 717 | } |
718 | - throw new RestException (400, $error); |
|
718 | + throw new RestException(400, $error); |
|
719 | 719 | } catch (\Exception $e) { |
720 | 720 | static::$exceptions[$info->name] = $e; |
721 | 721 | if (static::$holdException) { |
@@ -549,7 +549,9 @@ |
||
549 | 549 | case 'string' : |
550 | 550 | case 'password' : //password fields with string |
551 | 551 | case 'search' : //search field with string |
552 | - if (is_bool($input)) $input = $input ? 'true' : 'false'; |
|
552 | + if (is_bool($input)) { |
|
553 | + $input = $input ? 'true' : 'false'; |
|
554 | + } |
|
553 | 555 | if (!is_string($input)) { |
554 | 556 | $error .= '. Expecting alpha numeric value'; |
555 | 557 | break; |
@@ -19,255 +19,255 @@ |
||
19 | 19 | */ |
20 | 20 | class ValidationInfo implements iValueObject |
21 | 21 | { |
22 | - /** |
|
23 | - * @var mixed given value for the parameter |
|
24 | - */ |
|
25 | - public $value; |
|
26 | - /** |
|
27 | - * @var string proper name for given parameter |
|
28 | - */ |
|
29 | - public $label; |
|
30 | - /** |
|
31 | - * @var string html element that can be used to represent the parameter for |
|
32 | - * input |
|
33 | - */ |
|
34 | - public $field; |
|
35 | - /** |
|
36 | - * @var mixed default value for the parameter |
|
37 | - */ |
|
38 | - public $default; |
|
39 | - /** |
|
40 | - * Name of the variable being validated |
|
41 | - * |
|
42 | - * @var string variable name |
|
43 | - */ |
|
44 | - public $name; |
|
22 | + /** |
|
23 | + * @var mixed given value for the parameter |
|
24 | + */ |
|
25 | + public $value; |
|
26 | + /** |
|
27 | + * @var string proper name for given parameter |
|
28 | + */ |
|
29 | + public $label; |
|
30 | + /** |
|
31 | + * @var string html element that can be used to represent the parameter for |
|
32 | + * input |
|
33 | + */ |
|
34 | + public $field; |
|
35 | + /** |
|
36 | + * @var mixed default value for the parameter |
|
37 | + */ |
|
38 | + public $default; |
|
39 | + /** |
|
40 | + * Name of the variable being validated |
|
41 | + * |
|
42 | + * @var string variable name |
|
43 | + */ |
|
44 | + public $name; |
|
45 | 45 | |
46 | - /** |
|
47 | - * @var bool is it required or not |
|
48 | - */ |
|
49 | - public $required; |
|
46 | + /** |
|
47 | + * @var bool is it required or not |
|
48 | + */ |
|
49 | + public $required; |
|
50 | 50 | |
51 | - /** |
|
52 | - * @var string body or header or query where this parameter is coming from |
|
53 | - * in the http request |
|
54 | - */ |
|
55 | - public $from; |
|
51 | + /** |
|
52 | + * @var string body or header or query where this parameter is coming from |
|
53 | + * in the http request |
|
54 | + */ |
|
55 | + public $from; |
|
56 | 56 | |
57 | - /** |
|
58 | - * Data type of the variable being validated. |
|
59 | - * It will be mostly string |
|
60 | - * |
|
61 | - * @var string|array multiple types are specified it will be of |
|
62 | - * type array otherwise it will be a string |
|
63 | - */ |
|
64 | - public $type; |
|
57 | + /** |
|
58 | + * Data type of the variable being validated. |
|
59 | + * It will be mostly string |
|
60 | + * |
|
61 | + * @var string|array multiple types are specified it will be of |
|
62 | + * type array otherwise it will be a string |
|
63 | + */ |
|
64 | + public $type; |
|
65 | 65 | |
66 | - /** |
|
67 | - * When the type is array, this field is used to define the type of the |
|
68 | - * contents of the array |
|
69 | - * |
|
70 | - * @var string|null when all the items in an array are of certain type, we |
|
71 | - * can set this property. It will be null if the items can be of any type |
|
72 | - */ |
|
73 | - public $contentType; |
|
66 | + /** |
|
67 | + * When the type is array, this field is used to define the type of the |
|
68 | + * contents of the array |
|
69 | + * |
|
70 | + * @var string|null when all the items in an array are of certain type, we |
|
71 | + * can set this property. It will be null if the items can be of any type |
|
72 | + */ |
|
73 | + public $contentType; |
|
74 | 74 | |
75 | - /** |
|
76 | - * Should we attempt to fix the value? |
|
77 | - * When set to false validation class should throw |
|
78 | - * an exception or return false for the validate call. |
|
79 | - * When set to true it will attempt to fix the value if possible |
|
80 | - * or throw an exception or return false when it cant be fixed. |
|
81 | - * |
|
82 | - * @var boolean true or false |
|
83 | - */ |
|
84 | - public $fix = false; |
|
75 | + /** |
|
76 | + * Should we attempt to fix the value? |
|
77 | + * When set to false validation class should throw |
|
78 | + * an exception or return false for the validate call. |
|
79 | + * When set to true it will attempt to fix the value if possible |
|
80 | + * or throw an exception or return false when it cant be fixed. |
|
81 | + * |
|
82 | + * @var boolean true or false |
|
83 | + */ |
|
84 | + public $fix = false; |
|
85 | 85 | |
86 | - /** |
|
87 | - * @var array of children to be validated |
|
88 | - */ |
|
89 | - public $children = null; |
|
86 | + /** |
|
87 | + * @var array of children to be validated |
|
88 | + */ |
|
89 | + public $children = null; |
|
90 | 90 | |
91 | - // ================================================================== |
|
92 | - // |
|
93 | - // VALUE RANGE |
|
94 | - // |
|
95 | - // ------------------------------------------------------------------ |
|
96 | - /** |
|
97 | - * Given value should match one of the values in the array |
|
98 | - * |
|
99 | - * @var array of choices to match to |
|
100 | - */ |
|
101 | - public $choice; |
|
102 | - /** |
|
103 | - * If the type is string it will set the lower limit for length |
|
104 | - * else will specify the lower limit for the value |
|
105 | - * |
|
106 | - * @var number minimum value |
|
107 | - */ |
|
108 | - public $min; |
|
109 | - /** |
|
110 | - * If the type is string it will set the upper limit limit for length |
|
111 | - * else will specify the upper limit for the value |
|
112 | - * |
|
113 | - * @var number maximum value |
|
114 | - */ |
|
115 | - public $max; |
|
91 | + // ================================================================== |
|
92 | + // |
|
93 | + // VALUE RANGE |
|
94 | + // |
|
95 | + // ------------------------------------------------------------------ |
|
96 | + /** |
|
97 | + * Given value should match one of the values in the array |
|
98 | + * |
|
99 | + * @var array of choices to match to |
|
100 | + */ |
|
101 | + public $choice; |
|
102 | + /** |
|
103 | + * If the type is string it will set the lower limit for length |
|
104 | + * else will specify the lower limit for the value |
|
105 | + * |
|
106 | + * @var number minimum value |
|
107 | + */ |
|
108 | + public $min; |
|
109 | + /** |
|
110 | + * If the type is string it will set the upper limit limit for length |
|
111 | + * else will specify the upper limit for the value |
|
112 | + * |
|
113 | + * @var number maximum value |
|
114 | + */ |
|
115 | + public $max; |
|
116 | 116 | |
117 | - // ================================================================== |
|
118 | - // |
|
119 | - // REGEX VALIDATION |
|
120 | - // |
|
121 | - // ------------------------------------------------------------------ |
|
122 | - /** |
|
123 | - * RegEx pattern to match the value |
|
124 | - * |
|
125 | - * @var string regular expression |
|
126 | - */ |
|
127 | - public $pattern; |
|
117 | + // ================================================================== |
|
118 | + // |
|
119 | + // REGEX VALIDATION |
|
120 | + // |
|
121 | + // ------------------------------------------------------------------ |
|
122 | + /** |
|
123 | + * RegEx pattern to match the value |
|
124 | + * |
|
125 | + * @var string regular expression |
|
126 | + */ |
|
127 | + public $pattern; |
|
128 | 128 | |
129 | - // ================================================================== |
|
130 | - // |
|
131 | - // CUSTOM VALIDATION |
|
132 | - // |
|
133 | - // ------------------------------------------------------------------ |
|
134 | - /** |
|
135 | - * Rules specified for the parameter in the php doc comment. |
|
136 | - * It is passed to the validation method as the second parameter |
|
137 | - * |
|
138 | - * @var array custom rule set |
|
139 | - */ |
|
140 | - public $rules; |
|
129 | + // ================================================================== |
|
130 | + // |
|
131 | + // CUSTOM VALIDATION |
|
132 | + // |
|
133 | + // ------------------------------------------------------------------ |
|
134 | + /** |
|
135 | + * Rules specified for the parameter in the php doc comment. |
|
136 | + * It is passed to the validation method as the second parameter |
|
137 | + * |
|
138 | + * @var array custom rule set |
|
139 | + */ |
|
140 | + public $rules; |
|
141 | 141 | |
142 | - /** |
|
143 | - * Specifying a custom error message will override the standard error |
|
144 | - * message return by the validator class |
|
145 | - * |
|
146 | - * @var string custom error response |
|
147 | - */ |
|
148 | - public $message; |
|
142 | + /** |
|
143 | + * Specifying a custom error message will override the standard error |
|
144 | + * message return by the validator class |
|
145 | + * |
|
146 | + * @var string custom error response |
|
147 | + */ |
|
148 | + public $message; |
|
149 | 149 | |
150 | - // ================================================================== |
|
151 | - // |
|
152 | - // METHODS |
|
153 | - // |
|
154 | - // ------------------------------------------------------------------ |
|
150 | + // ================================================================== |
|
151 | + // |
|
152 | + // METHODS |
|
153 | + // |
|
154 | + // ------------------------------------------------------------------ |
|
155 | 155 | |
156 | - /** |
|
157 | - * Name of the method to be used for validation. |
|
158 | - * It will be receiving two parameters $input, $rules (array) |
|
159 | - * |
|
160 | - * @var string validation method name |
|
161 | - */ |
|
162 | - public $method; |
|
156 | + /** |
|
157 | + * Name of the method to be used for validation. |
|
158 | + * It will be receiving two parameters $input, $rules (array) |
|
159 | + * |
|
160 | + * @var string validation method name |
|
161 | + */ |
|
162 | + public $method; |
|
163 | 163 | |
164 | - /** |
|
165 | - * Instance of the API class currently being called. It will be null most of |
|
166 | - * the time. Only when method is defined it will contain an instance. |
|
167 | - * This behavior is for lazy loading of the API class |
|
168 | - * |
|
169 | - * @var null|object will be null or api class instance |
|
170 | - */ |
|
171 | - public $apiClassInstance = null; |
|
164 | + /** |
|
165 | + * Instance of the API class currently being called. It will be null most of |
|
166 | + * the time. Only when method is defined it will contain an instance. |
|
167 | + * This behavior is for lazy loading of the API class |
|
168 | + * |
|
169 | + * @var null|object will be null or api class instance |
|
170 | + */ |
|
171 | + public $apiClassInstance = null; |
|
172 | 172 | |
173 | - public static function numericValue($value) |
|
174 | - { |
|
175 | - return ( int )$value == $value |
|
176 | - ? ( int )$value |
|
177 | - : floatval($value); |
|
178 | - } |
|
173 | + public static function numericValue($value) |
|
174 | + { |
|
175 | + return ( int )$value == $value |
|
176 | + ? ( int )$value |
|
177 | + : floatval($value); |
|
178 | + } |
|
179 | 179 | |
180 | - public static function arrayValue($value) |
|
181 | - { |
|
182 | - return is_array($value) ? $value : array( |
|
183 | - $value |
|
184 | - ); |
|
185 | - } |
|
180 | + public static function arrayValue($value) |
|
181 | + { |
|
182 | + return is_array($value) ? $value : array( |
|
183 | + $value |
|
184 | + ); |
|
185 | + } |
|
186 | 186 | |
187 | - public static function stringValue($value, $glue = ',') |
|
188 | - { |
|
189 | - return is_array($value) |
|
190 | - ? implode($glue, $value) |
|
191 | - : ( string )$value; |
|
192 | - } |
|
187 | + public static function stringValue($value, $glue = ',') |
|
188 | + { |
|
189 | + return is_array($value) |
|
190 | + ? implode($glue, $value) |
|
191 | + : ( string )$value; |
|
192 | + } |
|
193 | 193 | |
194 | - public static function booleanValue($value) |
|
195 | - { |
|
196 | - return is_bool($value) |
|
197 | - ? $value |
|
198 | - : $value !== 'false'; |
|
199 | - } |
|
194 | + public static function booleanValue($value) |
|
195 | + { |
|
196 | + return is_bool($value) |
|
197 | + ? $value |
|
198 | + : $value !== 'false'; |
|
199 | + } |
|
200 | 200 | |
201 | - public static function filterArray(array $data, $keepNumericKeys) |
|
202 | - { |
|
203 | - $r = array(); |
|
204 | - foreach ($data as $key => $value) { |
|
205 | - if (is_numeric($key)) { |
|
206 | - if ($keepNumericKeys) { |
|
207 | - $r[$key] = $value; |
|
208 | - } |
|
209 | - } elseif (!$keepNumericKeys) { |
|
210 | - $r[$key] = $value; |
|
211 | - } |
|
212 | - } |
|
213 | - return $r; |
|
214 | - } |
|
201 | + public static function filterArray(array $data, $keepNumericKeys) |
|
202 | + { |
|
203 | + $r = array(); |
|
204 | + foreach ($data as $key => $value) { |
|
205 | + if (is_numeric($key)) { |
|
206 | + if ($keepNumericKeys) { |
|
207 | + $r[$key] = $value; |
|
208 | + } |
|
209 | + } elseif (!$keepNumericKeys) { |
|
210 | + $r[$key] = $value; |
|
211 | + } |
|
212 | + } |
|
213 | + return $r; |
|
214 | + } |
|
215 | 215 | |
216 | - public function __toString() |
|
217 | - { |
|
218 | - return ' new ValidationInfo() '; |
|
219 | - } |
|
216 | + public function __toString() |
|
217 | + { |
|
218 | + return ' new ValidationInfo() '; |
|
219 | + } |
|
220 | 220 | |
221 | - private function getProperty(array &$from, $property) |
|
222 | - { |
|
223 | - $p = Util::nestedValue($from, $property); |
|
224 | - unset($from[$property]); |
|
225 | - $p2 = Util::nestedValue( |
|
226 | - $from, CommentParser::$embeddedDataName, $property |
|
227 | - ); |
|
228 | - unset($from[CommentParser::$embeddedDataName][$property]); |
|
221 | + private function getProperty(array &$from, $property) |
|
222 | + { |
|
223 | + $p = Util::nestedValue($from, $property); |
|
224 | + unset($from[$property]); |
|
225 | + $p2 = Util::nestedValue( |
|
226 | + $from, CommentParser::$embeddedDataName, $property |
|
227 | + ); |
|
228 | + unset($from[CommentParser::$embeddedDataName][$property]); |
|
229 | 229 | |
230 | - if ($property == 'type' && $p == 'array' && $p2) { |
|
231 | - $this->contentType = $p2; |
|
232 | - return $p; |
|
233 | - } |
|
234 | - $r = is_null($p2) ? (is_null($p) ? null : $p) : $p2; |
|
235 | - if (!is_null($r)) { |
|
236 | - if ($property == 'min' || $property == 'max') { |
|
237 | - return static::numericValue($r); |
|
238 | - } elseif ($property == 'required' || $property == 'fix') { |
|
239 | - return static::booleanValue($r); |
|
240 | - } elseif ($property == 'choice') { |
|
241 | - return static::arrayValue($r); |
|
242 | - } elseif ($property == 'pattern') { |
|
243 | - return static::stringValue($r); |
|
244 | - } |
|
245 | - } |
|
246 | - return $r; |
|
247 | - } |
|
230 | + if ($property == 'type' && $p == 'array' && $p2) { |
|
231 | + $this->contentType = $p2; |
|
232 | + return $p; |
|
233 | + } |
|
234 | + $r = is_null($p2) ? (is_null($p) ? null : $p) : $p2; |
|
235 | + if (!is_null($r)) { |
|
236 | + if ($property == 'min' || $property == 'max') { |
|
237 | + return static::numericValue($r); |
|
238 | + } elseif ($property == 'required' || $property == 'fix') { |
|
239 | + return static::booleanValue($r); |
|
240 | + } elseif ($property == 'choice') { |
|
241 | + return static::arrayValue($r); |
|
242 | + } elseif ($property == 'pattern') { |
|
243 | + return static::stringValue($r); |
|
244 | + } |
|
245 | + } |
|
246 | + return $r; |
|
247 | + } |
|
248 | 248 | |
249 | - public function __construct(array $info) |
|
250 | - { |
|
251 | - $properties = get_object_vars($this); |
|
252 | - unset($properties['contentType']); |
|
253 | - foreach ($properties as $property => $value) { |
|
254 | - $this->{$property} = $this->getProperty($info, $property); |
|
255 | - } |
|
256 | - $inner = Util::nestedValue($info, 'properties'); |
|
257 | - $this->rules = !empty($inner) ? $inner + $info : $info; |
|
258 | - unset($this->rules['properties']); |
|
259 | - if (is_string($this->type) && $this->type == 'integer') { |
|
260 | - $this->type = 'int'; |
|
261 | - } |
|
262 | - } |
|
249 | + public function __construct(array $info) |
|
250 | + { |
|
251 | + $properties = get_object_vars($this); |
|
252 | + unset($properties['contentType']); |
|
253 | + foreach ($properties as $property => $value) { |
|
254 | + $this->{$property} = $this->getProperty($info, $property); |
|
255 | + } |
|
256 | + $inner = Util::nestedValue($info, 'properties'); |
|
257 | + $this->rules = !empty($inner) ? $inner + $info : $info; |
|
258 | + unset($this->rules['properties']); |
|
259 | + if (is_string($this->type) && $this->type == 'integer') { |
|
260 | + $this->type = 'int'; |
|
261 | + } |
|
262 | + } |
|
263 | 263 | |
264 | - /** |
|
265 | - * Magic Method used for creating instance at run time |
|
266 | - */ |
|
267 | - public static function __set_state(array $info) |
|
268 | - { |
|
269 | - $o = new self ($info); |
|
270 | - return $o; |
|
271 | - } |
|
264 | + /** |
|
265 | + * Magic Method used for creating instance at run time |
|
266 | + */ |
|
267 | + public static function __set_state(array $info) |
|
268 | + { |
|
269 | + $o = new self ($info); |
|
270 | + return $o; |
|
271 | + } |
|
272 | 272 | } |
273 | 273 |
@@ -172,8 +172,8 @@ discard block |
||
172 | 172 | |
173 | 173 | public static function numericValue($value) |
174 | 174 | { |
175 | - return ( int )$value == $value |
|
176 | - ? ( int )$value |
|
175 | + return (int) $value == $value |
|
176 | + ? (int) $value |
|
177 | 177 | : floatval($value); |
178 | 178 | } |
179 | 179 | |
@@ -188,7 +188,7 @@ discard block |
||
188 | 188 | { |
189 | 189 | return is_array($value) |
190 | 190 | ? implode($glue, $value) |
191 | - : ( string )$value; |
|
191 | + : (string) $value; |
|
192 | 192 | } |
193 | 193 | |
194 | 194 | public static function booleanValue($value) |
@@ -266,7 +266,7 @@ discard block |
||
266 | 266 | */ |
267 | 267 | public static function __set_state(array $info) |
268 | 268 | { |
269 | - $o = new self ($info); |
|
269 | + $o = new self($info); |
|
270 | 270 | return $o; |
271 | 271 | } |
272 | 272 | } |
@@ -15,58 +15,58 @@ |
||
15 | 15 | */ |
16 | 16 | class Compose implements iCompose |
17 | 17 | { |
18 | - /** |
|
19 | - * @var bool When restler is not running in production mode, this value will |
|
20 | - * be checked to include the debug information on error response |
|
21 | - */ |
|
22 | - public static $includeDebugInfo = true; |
|
23 | - /** |
|
24 | - * Current Restler instance |
|
25 | - * Injected at runtime |
|
26 | - * |
|
27 | - * @var Restler |
|
28 | - */ |
|
29 | - public $restler; |
|
18 | + /** |
|
19 | + * @var bool When restler is not running in production mode, this value will |
|
20 | + * be checked to include the debug information on error response |
|
21 | + */ |
|
22 | + public static $includeDebugInfo = true; |
|
23 | + /** |
|
24 | + * Current Restler instance |
|
25 | + * Injected at runtime |
|
26 | + * |
|
27 | + * @var Restler |
|
28 | + */ |
|
29 | + public $restler; |
|
30 | 30 | |
31 | - /** |
|
32 | - * Result of an api call is passed to this method |
|
33 | - * to create a standard structure for the data |
|
34 | - * |
|
35 | - * @param mixed $result can be a primitive or array or object |
|
36 | - * |
|
37 | - * @return mixed |
|
38 | - */ |
|
39 | - public function response($result) |
|
40 | - { |
|
41 | - //TODO: check Defaults::language and change result accordingly |
|
42 | - return $result; |
|
43 | - } |
|
31 | + /** |
|
32 | + * Result of an api call is passed to this method |
|
33 | + * to create a standard structure for the data |
|
34 | + * |
|
35 | + * @param mixed $result can be a primitive or array or object |
|
36 | + * |
|
37 | + * @return mixed |
|
38 | + */ |
|
39 | + public function response($result) |
|
40 | + { |
|
41 | + //TODO: check Defaults::language and change result accordingly |
|
42 | + return $result; |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * When the api call results in RestException this method |
|
47 | - * will be called to return the error message |
|
48 | - * |
|
49 | - * @param RestException $exception exception that has reasons for failure |
|
50 | - * |
|
51 | - * @return array |
|
52 | - */ |
|
53 | - public function message(RestException $exception) |
|
54 | - { |
|
55 | - //TODO: check Defaults::language and change result accordingly |
|
56 | - $r = array( |
|
57 | - 'error' => array( |
|
58 | - 'code' => $exception->getCode(), |
|
59 | - 'message' => $exception->getErrorMessage(), |
|
60 | - ) + $exception->getDetails() |
|
61 | - ); |
|
62 | - if (!Scope::get('Restler')->getProductionMode() && self::$includeDebugInfo) { |
|
63 | - $r += array( |
|
64 | - 'debug' => array( |
|
65 | - 'source' => $exception->getSource(), |
|
66 | - 'stages' => $exception->getStages(), |
|
67 | - ) |
|
68 | - ); |
|
69 | - } |
|
70 | - return $r; |
|
71 | - } |
|
45 | + /** |
|
46 | + * When the api call results in RestException this method |
|
47 | + * will be called to return the error message |
|
48 | + * |
|
49 | + * @param RestException $exception exception that has reasons for failure |
|
50 | + * |
|
51 | + * @return array |
|
52 | + */ |
|
53 | + public function message(RestException $exception) |
|
54 | + { |
|
55 | + //TODO: check Defaults::language and change result accordingly |
|
56 | + $r = array( |
|
57 | + 'error' => array( |
|
58 | + 'code' => $exception->getCode(), |
|
59 | + 'message' => $exception->getErrorMessage(), |
|
60 | + ) + $exception->getDetails() |
|
61 | + ); |
|
62 | + if (!Scope::get('Restler')->getProductionMode() && self::$includeDebugInfo) { |
|
63 | + $r += array( |
|
64 | + 'debug' => array( |
|
65 | + 'source' => $exception->getSource(), |
|
66 | + 'stages' => $exception->getStages(), |
|
67 | + ) |
|
68 | + ); |
|
69 | + } |
|
70 | + return $r; |
|
71 | + } |
|
72 | 72 | } |
73 | 73 | \ No newline at end of file |