/library/Zend/Db/Adapter/ParameterContainer.php

https://github.com/Rovak/zf2 · PHP · 313 lines · 145 code · 32 blank · 136 comment · 15 complexity · 101dfb5e77de7b7b9854f60e3a1d1144 MD5 · raw file

  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @package Zend_Db
  9. */
  10. namespace Zend\Db\Adapter;
  11. /**
  12. * @category Zend
  13. * @package Zend_Db
  14. * @subpackage Adapter
  15. */
  16. class ParameterContainer implements \Iterator, \ArrayAccess, \Countable
  17. {
  18. const TYPE_AUTO = 'auto';
  19. const TYPE_NULL = 'null';
  20. const TYPE_DOUBLE = 'double';
  21. const TYPE_INTEGER = 'integer';
  22. const TYPE_STRING = 'string';
  23. const TYPE_LOB = 'lob';
  24. /**
  25. * Data
  26. *
  27. * @var array
  28. */
  29. protected $data = array();
  30. /**
  31. * @var array
  32. */
  33. protected $positions = array();
  34. /**
  35. * Errata
  36. *
  37. * @var array
  38. */
  39. protected $errata = array();
  40. /**
  41. * Constructor
  42. *
  43. * @param array $data
  44. */
  45. public function __construct(array $data = array())
  46. {
  47. if ($data) {
  48. $this->setFromArray($data);
  49. }
  50. }
  51. /**
  52. * Offset exists
  53. *
  54. * @param string $name
  55. * @return boolean
  56. */
  57. public function offsetExists($name)
  58. {
  59. return (isset($this->data[$name]));
  60. }
  61. /**
  62. * Offset get
  63. *
  64. * @param string $name
  65. * @return mixed
  66. */
  67. public function offsetGet($name)
  68. {
  69. return (isset($this->data[$name])) ? $this->data[$name] : null;
  70. }
  71. /**
  72. * @param $name
  73. * @param $from
  74. */
  75. public function offsetSetReference($name, $from)
  76. {
  77. $this->data[$name] =& $this->data[$from];
  78. }
  79. /**
  80. * Offset set
  81. *
  82. * @param string|integer $name
  83. * @param mixed $value
  84. * @param mixed $errata
  85. */
  86. public function offsetSet($name, $value, $errata = null)
  87. {
  88. $this->data[$name] = $value;
  89. $names = array_keys($this->data);
  90. $this->positions[array_search($name, $names)] = $name;
  91. if ($errata) {
  92. $this->offsetSetErrata($name, $errata);
  93. }
  94. }
  95. /**
  96. * Offset unset
  97. *
  98. * @param string $name
  99. * @return ParameterContainer
  100. */
  101. public function offsetUnset($name)
  102. {
  103. if (is_int($name)) {
  104. $name = $this->positions[$name];
  105. }
  106. unset($this->data[$name]);
  107. return $this;
  108. }
  109. /**
  110. * Set from array
  111. *
  112. * @param array $data
  113. * @return ParameterContainer
  114. */
  115. public function setFromArray(Array $data)
  116. {
  117. foreach ($data as $n => $v) {
  118. $this->offsetSet($n, $v);
  119. }
  120. return $this;
  121. }
  122. /**
  123. * Offset set errata
  124. *
  125. * @param string|integer $name
  126. * @param mixed $errata
  127. */
  128. public function offsetSetErrata($name, $errata)
  129. {
  130. if (is_int($name)) {
  131. $name = $this->positions[$name];
  132. }
  133. $this->errata[$name] = $errata;
  134. }
  135. /**
  136. * Offset get errata
  137. *
  138. * @param string|integer $name
  139. * @return mixed
  140. */
  141. public function offsetGetErrata($name)
  142. {
  143. if (is_int($name)) {
  144. $name = $this->positions[$name];
  145. }
  146. if (!array_key_exists($name, $this->data)) {
  147. throw new Exception\InvalidArgumentException('Data does not exist for this name/position');
  148. }
  149. return $this->errata[$name];
  150. }
  151. /**
  152. * Offset has errata
  153. *
  154. * @param string|integer $name
  155. * @return boolean
  156. */
  157. public function offsetHasErrata($name)
  158. {
  159. if (is_int($name)) {
  160. $name = $this->positions[$name];
  161. }
  162. return (isset($this->errata[$name]));
  163. }
  164. /**
  165. * Offset unset errata
  166. *
  167. * @param string|integer $name
  168. */
  169. public function offsetUnsetErrata($name)
  170. {
  171. if (is_int($name)) {
  172. $name = $this->positions[$name];
  173. }
  174. if (!array_key_exists($name, $this->errata)) {
  175. throw new Exception\InvalidArgumentException('Data does not exist for this name/position');
  176. }
  177. $this->errata[$name] = null;
  178. }
  179. /**
  180. * Get errata iterator
  181. *
  182. * @return \ArrayIterator
  183. */
  184. public function getErrataIterator()
  185. {
  186. return new \ArrayIterator($this->errata);
  187. }
  188. /**
  189. * getNamedArray
  190. *
  191. * @return array
  192. */
  193. public function getNamedArray()
  194. {
  195. return $this->data;
  196. }
  197. /**
  198. * getNamedArray
  199. *
  200. * @return array
  201. */
  202. public function getPositionalArray()
  203. {
  204. return array_values($this->data);
  205. }
  206. /**
  207. * count
  208. *
  209. * @return integer
  210. */
  211. public function count()
  212. {
  213. return count($this->data);
  214. }
  215. /**
  216. * Current
  217. *
  218. * @return mixed
  219. */
  220. public function current()
  221. {
  222. return current($this->data);
  223. }
  224. /**
  225. * Next
  226. *
  227. * @return mixed
  228. */
  229. public function next()
  230. {
  231. return next($this->data);
  232. }
  233. /**
  234. * Key
  235. *
  236. * @return mixed
  237. */
  238. public function key()
  239. {
  240. return key($this->data);
  241. }
  242. /**
  243. * Valid
  244. *
  245. * @return boolean
  246. */
  247. public function valid()
  248. {
  249. return (current($this->data) !== false);
  250. }
  251. /**
  252. * Rewind
  253. */
  254. public function rewind()
  255. {
  256. reset($this->data);
  257. }
  258. /**
  259. * @param array $array
  260. * @return ParameterContainer
  261. */
  262. public function merge($parameters)
  263. {
  264. if (!is_array($parameters) && !$parameters instanceof ParameterContainer) {
  265. throw new Exception\InvalidArgumentException('$parameters must be an array or an instance of ParameterContainer');
  266. }
  267. if (count($parameters) == 0) {
  268. return;
  269. }
  270. if ($parameters instanceof ParameterContainer) {
  271. $parameters = $parameters->getNamedArray();
  272. }
  273. foreach ($parameters as $key => $value) {
  274. if (is_int($key)) {
  275. $key = null;
  276. }
  277. $this->offsetSet($key, $value);
  278. }
  279. return $this;
  280. }
  281. }