PageRenderTime 62ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/Input/Input.php

https://gitlab.com/dleonov/my-framework-two
PHP | 419 lines | 184 code | 43 blank | 192 comment | 20 complexity | 81b10b32dd27560d6aee5ac757192033 MD5 | raw file
  1. <?php
  2. /**
  3. * Input Base Class
  4. *
  5. * This is an abstracted input class used to manage retrieving data from the application environment.
  6. *
  7. * @since 11.1
  8. *
  9. * @property-read Input $get
  10. * @property-read Input $post
  11. * @property-read Input $request
  12. * @property-read Input $server
  13. * @property-read InputFiles $files
  14. * @property-read InputCookie $cookie
  15. *
  16. * @method integer getInt() getInt($name, $default = null) Get a signed integer.
  17. * @method integer getUint() getUint($name, $default = null) Get an unsigned integer.
  18. * @method float getFloat() getFloat($name, $default = null) Get a floating-point number.
  19. * @method boolean getBool() getBool($name, $default = null) Get a boolean.
  20. * @method string getWord() getWord($name, $default = null)
  21. * @method string getAlnum() getAlnum($name, $default = null)
  22. * @method string getCmd() getCmd($name, $default = null)
  23. * @method string getBase64() getBase64($name, $default = null)
  24. * @method string getString() getString($name, $default = null)
  25. * @method string getHtml() getHtml($name, $default = null)
  26. * @method string getPath() getPath($name, $default = null)
  27. * @method string getUsername() getUsername($name, $default = null)
  28. */
  29. class Input implements Serializable, Countable
  30. {
  31. /**
  32. * Options array for the Input instance.
  33. *
  34. * @var array
  35. * @since 11.1
  36. */
  37. protected $options = array();
  38. /**
  39. * Filter object to use.
  40. *
  41. * @var FilterInput
  42. * @since 11.1
  43. */
  44. protected $filter = null;
  45. /**
  46. * Input data.
  47. *
  48. * @var array
  49. * @since 11.1
  50. */
  51. protected $data = array();
  52. /**
  53. * Input objects
  54. *
  55. * @var array
  56. * @since 11.1
  57. */
  58. protected $inputs = array();
  59. /**
  60. * Constructor.
  61. *
  62. * @param array $source Source data (Optional, default is $_REQUEST)
  63. * @param array $options Array of configuration parameters (Optional)
  64. *
  65. * @since 11.1
  66. */
  67. public function __construct($source = null, array $options = array())
  68. {
  69. if (isset($options['filter']))
  70. {
  71. $this->filter = $options['filter'];
  72. }
  73. else
  74. {
  75. $this->filter = FilterInput::getInstance();
  76. }
  77. if (is_null($source))
  78. {
  79. $this->data = &$_REQUEST;
  80. }
  81. else
  82. {
  83. $this->data = $source;
  84. }
  85. // Set the options for the class.
  86. $this->options = $options;
  87. }
  88. /**
  89. * Magic method to get an input object
  90. *
  91. * @param mixed $name Name of the input object to retrieve.
  92. *
  93. * @return Input The request input object
  94. *
  95. * @since 11.1
  96. */
  97. public function __get($name)
  98. {
  99. if (isset($this->inputs[$name]))
  100. {
  101. return $this->inputs[$name];
  102. }
  103. $className = 'Input' . ucfirst($name);
  104. if (class_exists($className))
  105. {
  106. $this->inputs[$name] = new $className(null, $this->options);
  107. return $this->inputs[$name];
  108. }
  109. $superGlobal = '_' . strtoupper($name);
  110. if (isset($GLOBALS[$superGlobal]))
  111. {
  112. $this->inputs[$name] = new Input($GLOBALS[$superGlobal], $this->options);
  113. return $this->inputs[$name];
  114. }
  115. // TODO throw an exception
  116. }
  117. /**
  118. * Get the number of variables.
  119. *
  120. * @return integer The number of variables in the input.
  121. *
  122. * @since 12.2
  123. * @see Countable::count()
  124. */
  125. public function count()
  126. {
  127. return count($this->data);
  128. }
  129. /**
  130. * Gets a value from the input data.
  131. *
  132. * @param string $name Name of the value to get.
  133. * @param mixed $default Default value to return if variable does not exist.
  134. * @param string $filter Filter to apply to the value.
  135. *
  136. * @return mixed The filtered input value.
  137. *
  138. * @since 11.1
  139. */
  140. public function getArgument($name, $default = null, $filter = 'string')
  141. {
  142. if (isset($this->data[$name]))
  143. {
  144. return $this->filter->clean($this->data[$name], $filter);
  145. }
  146. return $default;
  147. }
  148. /**
  149. * Gets an array of values from the request.
  150. *
  151. * @param array $vars Associative array of keys and filter types to apply.
  152. * If empty and datasource is null, all the input data will be returned
  153. * but filtered using the filter given by the parameter defaultFilter in
  154. * FilterInput::clean.
  155. * @param mixed $datasource Array to retrieve data from, or null.
  156. * @param string $defaultFilter Default filter used in FilterInput::clean if vars is empty and
  157. * datasource is null. If 'unknown', the default case is used in
  158. * FilterInput::clean.
  159. *
  160. * @return mixed The filtered input data.
  161. *
  162. * @since 11.1
  163. */
  164. public function getArray(array $vars = array(), $datasource = null, $defaultFilter = 'unknown')
  165. {
  166. return $this->getArrayRecursive($vars, $datasource, $defaultFilter, false);
  167. }
  168. /**
  169. * Gets an array of values from the request.
  170. *
  171. * @param array $vars Associative array of keys and filter types to apply.
  172. * If empty and datasource is null, all the input data will be returned
  173. * but filtered using the filter given by the parameter defaultFilter in
  174. * FilterInput::clean.
  175. * @param mixed $datasource Array to retrieve data from, or null.
  176. * @param string $defaultFilter Default filter used in FilterInput::clean if vars is empty and
  177. * datasource is null. If 'unknown', the default case is used in
  178. * FilterInput::clean.
  179. * @param bool $recursion Flag to indicate a recursive function call.
  180. *
  181. * @return mixed The filtered input data.
  182. *
  183. * @since 3.4.2
  184. */
  185. protected function getArrayRecursive(array $vars = array(), $datasource = null, $defaultFilter = 'unknown', $recursion = false)
  186. {
  187. if (empty($vars) && is_null($datasource))
  188. {
  189. $vars = $this->data;
  190. }
  191. else
  192. {
  193. if (!$recursion)
  194. {
  195. $defaultFilter = null;
  196. }
  197. }
  198. $results = array();
  199. foreach ($vars as $k => $v)
  200. {
  201. if (is_array($v))
  202. {
  203. if (is_null($datasource))
  204. {
  205. $results[$k] = $this->getArrayRecursive($v, $this->get($k, null, 'array'), $defaultFilter, true);
  206. }
  207. else
  208. {
  209. $results[$k] = $this->getArrayRecursive($v, $datasource[$k], $defaultFilter, true);
  210. }
  211. }
  212. else
  213. {
  214. $filter = isset($defaultFilter) ? $defaultFilter : $v;
  215. if (is_null($datasource))
  216. {
  217. $results[$k] = $this->get($k, null, $filter);
  218. }
  219. elseif (isset($datasource[$k]))
  220. {
  221. $results[$k] = $this->filter->clean($datasource[$k], $filter);
  222. }
  223. else
  224. {
  225. $results[$k] = $this->filter->clean(null, $filter);
  226. }
  227. }
  228. }
  229. return $results;
  230. }
  231. /**
  232. * Sets a value
  233. *
  234. * @param string $name Name of the value to set.
  235. * @param mixed $value Value to assign to the input.
  236. *
  237. * @return void
  238. *
  239. * @since 11.1
  240. */
  241. public function set($name, $value)
  242. {
  243. $this->data[$name] = $value;
  244. }
  245. /**
  246. * Define a value. The value will only be set if there's no value for the name or if it is null.
  247. *
  248. * @param string $name Name of the value to define.
  249. * @param mixed $value Value to assign to the input.
  250. *
  251. * @return void
  252. *
  253. * @since 12.1
  254. */
  255. public function def($name, $value)
  256. {
  257. if (isset($this->data[$name]))
  258. {
  259. return;
  260. }
  261. $this->data[$name] = $value;
  262. }
  263. /**
  264. * Magic method to get filtered input data.
  265. *
  266. * @param string $name Name of the filter type prefixed with 'get'.
  267. * @param array $arguments [0] The name of the variable [1] The default value.
  268. *
  269. * @return mixed The filtered input value.
  270. *
  271. * @since 11.1
  272. */
  273. public function __call($name, $arguments)
  274. {
  275. if (substr($name, 0, 3) == 'get')
  276. {
  277. $filter = substr($name, 3);
  278. $default = null;
  279. if (isset($arguments[1]))
  280. {
  281. $default = $arguments[1];
  282. }
  283. return $this->get($arguments[0], $default, $filter);
  284. }
  285. }
  286. /**
  287. * Gets the request method.
  288. *
  289. * @return string The request method.
  290. *
  291. * @since 11.1
  292. */
  293. public function getMethod()
  294. {
  295. // Get method if exist
  296. $method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : '';
  297. $method = strtoupper($method);
  298. return $method;
  299. }
  300. /**
  301. * Method to serialize the input.
  302. *
  303. * @return string The serialized input.
  304. *
  305. * @since 12.1
  306. */
  307. public function serialize()
  308. {
  309. // Load all of the inputs.
  310. $this->loadAllInputs();
  311. // Remove $_ENV and $_SERVER from the inputs.
  312. $inputs = $this->inputs;
  313. unset($inputs['env']);
  314. unset($inputs['server']);
  315. // Serialize the options, data, and inputs.
  316. return serialize(array($this->options, $this->data, $inputs));
  317. }
  318. /**
  319. * Method to unserialize the input.
  320. *
  321. * @param string $input The serialized input.
  322. *
  323. * @return Input The input object.
  324. *
  325. * @since 12.1
  326. */
  327. public function unserialize($input)
  328. {
  329. // Unserialize the options, data, and inputs.
  330. list($this->options, $this->data, $this->inputs) = unserialize($input);
  331. // Load the filter.
  332. if (isset($this->options['filter']))
  333. {
  334. $this->filter = $this->options['filter'];
  335. }
  336. else
  337. {
  338. $this->filter = FilterInput::getInstance();
  339. }
  340. }
  341. /**
  342. * Method to load all of the global inputs.
  343. *
  344. * @return void
  345. *
  346. * @since 12.1
  347. */
  348. protected function loadAllInputs()
  349. {
  350. static $loaded = false;
  351. if (!$loaded)
  352. {
  353. // Load up all the globals.
  354. foreach ($GLOBALS as $global => $data)
  355. {
  356. // Check if the global starts with an underscore.
  357. if (strpos($global, '_') === 0)
  358. {
  359. // Convert global name to input name.
  360. $global = strtolower($global);
  361. $global = substr($global, 1);
  362. // Get the input.
  363. $this->$global;
  364. }
  365. }
  366. $loaded = true;
  367. }
  368. }
  369. public static function Get() {
  370. if (!self::$_Instance) {
  371. self::$_Instance = new self();
  372. }
  373. return self::$_Instance;
  374. }
  375. private static $_Instance = null;
  376. }