Param.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * Pimf
  4. *
  5. * @copyright Copyright (c) Gjero Krsteski (http://krsteski.de)
  6. * @license http://krsteski.de/new-bsd-license New BSD License
  7. */
  8. namespace Pimf;
  9. /**
  10. * @package Pimf
  11. * @author Gjero Krsteski <[email protected]>
  12. */
  13. class Param
  14. {
  15. /**
  16. * @var \ArrayObject|null
  17. */
  18. protected $data = null;
  19. /**
  20. * @param array $data
  21. */
  22. public function __construct(array $data = array())
  23. {
  24. $this->data = new \ArrayObject($data, \ArrayObject::STD_PROP_LIST + \ArrayObject::ARRAY_AS_PROPS);
  25. }
  26. /**
  27. * @return array
  28. */
  29. public function getAll()
  30. {
  31. return (array)$this->data->getArrayCopy();
  32. }
  33. /**
  34. * @param string $index
  35. * @param null|string $defaultValue
  36. * @param bool $filtered If you trust foreign input introduced to your PHP code - set to FALSE!
  37. *
  38. * @return string
  39. */
  40. public function get($index, $defaultValue = null, $filtered = true)
  41. {
  42. if ($this->data->offsetExists($index)) {
  43. if ($filtered === true) {
  44. // pretty high-level filtering here...
  45. return self::filter($this->data->offsetGet($index));
  46. }
  47. return $this->data->offsetGet($index);
  48. }
  49. return $defaultValue;
  50. }
  51. /**
  52. * Never ever (ever) trust foreign input introduced to your PHP code!
  53. *
  54. * @param array|string $rawData
  55. *
  56. * @return array|bool|string
  57. */
  58. public static function filter($rawData)
  59. {
  60. return is_array($rawData)
  61. ? array_map(
  62. function ($value) {
  63. return \Pimf\Util\String\Clean::xss($value);
  64. }, $rawData
  65. )
  66. : \Pimf\Util\String\Clean::xss($rawData);
  67. }
  68. }