Parameters.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Stdlib;
  10. use ArrayObject as PhpArrayObject;
  11. class Parameters extends PhpArrayObject implements ParametersInterface
  12. {
  13. /**
  14. * Constructor
  15. *
  16. * Enforces that we have an array, and enforces parameter access to array
  17. * elements.
  18. *
  19. * @param array $values
  20. */
  21. public function __construct(array $values = null)
  22. {
  23. if (null === $values) {
  24. $values = array();
  25. }
  26. parent::__construct($values, ArrayObject::ARRAY_AS_PROPS);
  27. }
  28. /**
  29. * Populate from native PHP array
  30. *
  31. * @param array $values
  32. * @return void
  33. */
  34. public function fromArray(array $values)
  35. {
  36. $this->exchangeArray($values);
  37. }
  38. /**
  39. * Populate from query string
  40. *
  41. * @param string $string
  42. * @return void
  43. */
  44. public function fromString($string)
  45. {
  46. $array = array();
  47. parse_str($string, $array);
  48. $this->fromArray($array);
  49. }
  50. /**
  51. * Serialize to native PHP array
  52. *
  53. * @return array
  54. */
  55. public function toArray()
  56. {
  57. return $this->getArrayCopy();
  58. }
  59. /**
  60. * Serialize to query string
  61. *
  62. * @return string
  63. */
  64. public function toString()
  65. {
  66. return http_build_query($this);
  67. }
  68. /**
  69. * Retrieve by key
  70. *
  71. * Returns null if the key does not exist.
  72. *
  73. * @param string $name
  74. * @return mixed
  75. */
  76. public function offsetGet($name)
  77. {
  78. if ($this->offsetExists($name)) {
  79. return parent::offsetGet($name);
  80. }
  81. return null;
  82. }
  83. /**
  84. * @param string $name
  85. * @param mixed $default optional default value
  86. * @return mixed
  87. */
  88. public function get($name, $default = null)
  89. {
  90. if ($this->offsetExists($name)) {
  91. return parent::offsetGet($name);
  92. }
  93. return $default;
  94. }
  95. /**
  96. * @param string $name
  97. * @param mixed $value
  98. * @return Parameters
  99. */
  100. public function set($name, $value)
  101. {
  102. $this[$name] = $value;
  103. return $this;
  104. }
  105. }