ValidationInfo.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. namespace Luracast\Restler\Data;
  3. use Luracast\Restler\CommentParser;
  4. use Luracast\Restler\Util;
  5. /**
  6. * ValueObject for validation information. An instance is created and
  7. * populated by Restler to pass it to iValidate implementing classes for
  8. * validation
  9. *
  10. * @category Framework
  11. * @package Restler
  12. * @author R.Arul Kumaran <arul@luracast.com>
  13. * @copyright 2010 Luracast
  14. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  15. * @link http://luracast.com/products/restler/
  16. *
  17. */
  18. class ValidationInfo implements iValueObject
  19. {
  20. /**
  21. * @var mixed given value for the parameter
  22. */
  23. public $value;
  24. /**
  25. * @var string proper name for given parameter
  26. */
  27. public $label;
  28. /**
  29. * @var string html element that can be used to represent the parameter for
  30. * input
  31. */
  32. public $field;
  33. /**
  34. * @var mixed default value for the parameter
  35. */
  36. public $default;
  37. /**
  38. * Name of the variable being validated
  39. *
  40. * @var string variable name
  41. */
  42. public $name;
  43. /**
  44. * @var bool is it required or not
  45. */
  46. public $required;
  47. /**
  48. * @var string body or header or query where this parameter is coming from
  49. * in the http request
  50. */
  51. public $from;
  52. /**
  53. * Data type of the variable being validated.
  54. * It will be mostly string
  55. *
  56. * @var string|array multiple types are specified it will be of
  57. * type array otherwise it will be a string
  58. */
  59. public $type;
  60. /**
  61. * When the type is array, this field is used to define the type of the
  62. * contents of the array
  63. *
  64. * @var string|null when all the items in an array are of certain type, we
  65. * can set this property. It will be null if the items can be of any type
  66. */
  67. public $contentType;
  68. /**
  69. * Should we attempt to fix the value?
  70. * When set to false validation class should throw
  71. * an exception or return false for the validate call.
  72. * When set to true it will attempt to fix the value if possible
  73. * or throw an exception or return false when it cant be fixed.
  74. *
  75. * @var boolean true or false
  76. */
  77. public $fix = false;
  78. /**
  79. * @var array of children to be validated
  80. */
  81. public $children = null;
  82. // ==================================================================
  83. //
  84. // VALUE RANGE
  85. //
  86. // ------------------------------------------------------------------
  87. /**
  88. * Given value should match one of the values in the array
  89. *
  90. * @var array of choices to match to
  91. */
  92. public $choice;
  93. /**
  94. * If the type is string it will set the lower limit for length
  95. * else will specify the lower limit for the value
  96. *
  97. * @var number minimum value
  98. */
  99. public $min;
  100. /**
  101. * If the type is string it will set the upper limit limit for length
  102. * else will specify the upper limit for the value
  103. *
  104. * @var number maximum value
  105. */
  106. public $max;
  107. // ==================================================================
  108. //
  109. // REGEX VALIDATION
  110. //
  111. // ------------------------------------------------------------------
  112. /**
  113. * RegEx pattern to match the value
  114. *
  115. * @var string regular expression
  116. */
  117. public $pattern;
  118. // ==================================================================
  119. //
  120. // CUSTOM VALIDATION
  121. //
  122. // ------------------------------------------------------------------
  123. /**
  124. * Rules specified for the parameter in the php doc comment.
  125. * It is passed to the validation method as the second parameter
  126. *
  127. * @var array custom rule set
  128. */
  129. public $rules;
  130. /**
  131. * Specifying a custom error message will override the standard error
  132. * message return by the validator class
  133. *
  134. * @var string custom error response
  135. */
  136. public $message;
  137. // ==================================================================
  138. //
  139. // METHODS
  140. //
  141. // ------------------------------------------------------------------
  142. /**
  143. * Name of the method to be used for validation.
  144. * It will be receiving two parameters $input, $rules (array)
  145. *
  146. * @var string validation method name
  147. */
  148. public $method;
  149. /**
  150. * Instance of the API class currently being called. It will be null most of
  151. * the time. Only when method is defined it will contain an instance.
  152. * This behavior is for lazy loading of the API class
  153. *
  154. * @var null|object will be null or api class instance
  155. */
  156. public $apiClassInstance = null;
  157. public static function numericValue($value)
  158. {
  159. return ( int )$value == $value
  160. ? ( int )$value
  161. : floatval($value);
  162. }
  163. public static function arrayValue($value)
  164. {
  165. return is_array($value) ? $value : array(
  166. $value
  167. );
  168. }
  169. public static function stringValue($value, $glue = ',')
  170. {
  171. return is_array($value)
  172. ? implode($glue, $value)
  173. : ( string )$value;
  174. }
  175. public static function booleanValue($value)
  176. {
  177. return is_bool($value)
  178. ? $value
  179. : $value !== 'false';
  180. }
  181. public static function filterArray(array $data, $keepNumericKeys)
  182. {
  183. $r = array();
  184. foreach ($data as $key => $value) {
  185. if (is_numeric($key)) {
  186. if ($keepNumericKeys) {
  187. $r[$key] = $value;
  188. }
  189. } elseif (!$keepNumericKeys) {
  190. $r[$key] = $value;
  191. }
  192. }
  193. return $r;
  194. }
  195. public function __toString()
  196. {
  197. return ' new ValidationInfo() ';
  198. }
  199. private function getProperty(array &$from, $property)
  200. {
  201. $p = Util::nestedValue($from, $property);
  202. unset($from[$property]);
  203. $p2 = Util::nestedValue(
  204. $from, CommentParser::$embeddedDataName, $property
  205. );
  206. unset($from[CommentParser::$embeddedDataName][$property]);
  207. if ($property == 'type' && $p == 'array' && $p2) {
  208. $this->contentType = $p2;
  209. return $p;
  210. }
  211. $r = is_null($p2) ? (is_null($p) ? null : $p) : $p2;
  212. if (!is_null($r)) {
  213. if ($property == 'min' || $property == 'max') {
  214. return static::numericValue($r);
  215. } elseif ($property == 'required' || $property == 'fix') {
  216. return static::booleanValue($r);
  217. } elseif ($property == 'choice') {
  218. return static::arrayValue($r);
  219. } elseif ($property == 'pattern') {
  220. return static::stringValue($r);
  221. }
  222. }
  223. return $r;
  224. }
  225. public function __construct(array $info)
  226. {
  227. $properties = get_object_vars($this);
  228. unset($properties['contentType']);
  229. foreach ($properties as $property => $value) {
  230. $this->{$property} = $this->getProperty($info, $property);
  231. }
  232. $inner = Util::nestedValue($info, 'properties');
  233. $this->rules = !empty($inner) ? $inner + $info : $info;
  234. unset($this->rules['properties']);
  235. if (is_string($this->type) && $this->type == 'integer') {
  236. $this->type = 'int';
  237. }
  238. }
  239. /**
  240. * Magic Method used for creating instance at run time
  241. */
  242. public static function __set_state(array $info)
  243. {
  244. $o = new self ($info);
  245. return $o;
  246. }
  247. }