Param.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace Elastica;
  3. use Elastica\Exception\InvalidException;
  4. /**
  5. * Class to handle params.
  6. *
  7. * This function can be used to handle params for queries, filter
  8. *
  9. * @author Nicolas Ruflin <spam@ruflin.com>
  10. */
  11. class Param implements ArrayableInterface, \Countable
  12. {
  13. /**
  14. * Params.
  15. *
  16. * @var array
  17. */
  18. protected $_params = [];
  19. /**
  20. * Raw Params.
  21. *
  22. * @var array
  23. */
  24. protected $_rawParams = [];
  25. /**
  26. * Converts the params to an array. A default implementation exist to create
  27. * the an array out of the class name (last part of the class name)
  28. * and the params.
  29. *
  30. * @return array Filter array
  31. */
  32. public function toArray()
  33. {
  34. $data = [$this->_getBaseName() => $this->getParams()];
  35. if (!empty($this->_rawParams)) {
  36. $data = array_merge($data, $this->_rawParams);
  37. }
  38. return $this->_convertArrayable($data);
  39. }
  40. /**
  41. * Cast objects to arrays.
  42. *
  43. * @param array $array
  44. *
  45. * @return array
  46. */
  47. protected function _convertArrayable(array $array)
  48. {
  49. $arr = [];
  50. foreach ($array as $key => $value) {
  51. if ($value instanceof ArrayableInterface) {
  52. $arr[$value instanceof NameableInterface ? $value->getName() : $key] = $value->toArray();
  53. } elseif (is_array($value)) {
  54. $arr[$key] = $this->_convertArrayable($value);
  55. } else {
  56. $arr[$key] = $value;
  57. }
  58. }
  59. return $arr;
  60. }
  61. /**
  62. * Param's name
  63. * Picks the last part of the class name and makes it snake_case
  64. * You can override this method if you want to change the name.
  65. *
  66. * @return string name
  67. */
  68. protected function _getBaseName()
  69. {
  70. return Util::getParamName($this);
  71. }
  72. /**
  73. * Sets params not inside params array.
  74. *
  75. * @param string $key
  76. * @param mixed $value
  77. *
  78. * @return $this
  79. */
  80. protected function _setRawParam($key, $value)
  81. {
  82. $this->_rawParams[$key] = $value;
  83. return $this;
  84. }
  85. /**
  86. * Sets (overwrites) the value at the given key.
  87. *
  88. * @param string $key Key to set
  89. * @param mixed $value Key Value
  90. *
  91. * @return $this
  92. */
  93. public function setParam($key, $value)
  94. {
  95. $this->_params[$key] = $value;
  96. return $this;
  97. }
  98. /**
  99. * Sets (overwrites) all params of this object.
  100. *
  101. * @param array $params Parameter list
  102. *
  103. * @return $this
  104. */
  105. public function setParams(array $params)
  106. {
  107. $this->_params = $params;
  108. return $this;
  109. }
  110. /**
  111. * Adds a param to the list.
  112. *
  113. * This function can be used to add an array of params
  114. *
  115. * @param string $key Param key
  116. * @param mixed $value Value to set
  117. *
  118. * @return $this
  119. */
  120. public function addParam($key, $value)
  121. {
  122. $this->_params[$key][] = $value;
  123. return $this;
  124. }
  125. /**
  126. * Returns a specific param.
  127. *
  128. * @param string $key Key to return
  129. *
  130. * @throws \Elastica\Exception\InvalidException If requested key is not set
  131. *
  132. * @return mixed Key value
  133. */
  134. public function getParam($key)
  135. {
  136. if (!$this->hasParam($key)) {
  137. throw new InvalidException('Param '.$key.' does not exist');
  138. }
  139. return $this->_params[$key];
  140. }
  141. /**
  142. * Test if a param is set.
  143. *
  144. * @param string $key Key to test
  145. *
  146. * @return bool True if the param is set, false otherwise
  147. */
  148. public function hasParam($key)
  149. {
  150. return isset($this->_params[$key]);
  151. }
  152. /**
  153. * Returns the params array.
  154. *
  155. * @return array Params
  156. */
  157. public function getParams()
  158. {
  159. return $this->_params;
  160. }
  161. /**
  162. * {@inheritdoc}
  163. *
  164. * @return int
  165. */
  166. public function count()
  167. {
  168. return count($this->_params);
  169. }
  170. }