/hphp/system/php/spl/datastructures/SplObjectStorage.php

https://gitlab.com/Blueprint-Marketing/hhvm · PHP · 382 lines · 114 code · 27 blank · 241 comment · 10 complexity · 0505743e7fedf699c9af612c0d4e62a2 MD5 · raw file

  1. <?php
  2. // This doc comment block generated by idl/sysdoc.php
  3. /**
  4. * ( excerpt from http://docs.hhvm.com/manual/en/class.splobjectstorage.php )
  5. *
  6. * The SplObjectStorage class provides a map from objects to data or, by
  7. * ignoring data, an object set. This dual purpose can be useful in many
  8. * cases involving the need to uniquely identify objects.
  9. *
  10. */
  11. class SplObjectStorage
  12. implements \HH\Iterator, Countable, Serializable, ArrayAccess {
  13. private $__storage = array();
  14. private $__key = 0;
  15. // This doc comment block generated by idl/sysdoc.php
  16. /**
  17. * ( excerpt from http://docs.hhvm.com/manual/en/splobjectstorage.rewind.php )
  18. *
  19. * Rewind the iterator to the first storage element.
  20. *
  21. * @return mixed No value is returned.
  22. */
  23. public function rewind() {
  24. reset($this->__storage);
  25. $this->__key = 0;
  26. }
  27. // This doc comment block generated by idl/sysdoc.php
  28. /**
  29. * ( excerpt from http://docs.hhvm.com/manual/en/splobjectstorage.valid.php )
  30. *
  31. * Returns if the current iterator entry is valid.
  32. *
  33. * @return mixed Returns TRUE if the iterator entry is valid, FALSE
  34. * otherwise.
  35. */
  36. public function valid() {
  37. return key($this->__storage) !== NULL;
  38. }
  39. // This doc comment block generated by idl/sysdoc.php
  40. /**
  41. * ( excerpt from http://docs.hhvm.com/manual/en/splobjectstorage.key.php )
  42. *
  43. * Returns the index at which the iterator currently is.
  44. *
  45. * @return mixed The index corresponding to the position of the
  46. * iterator.
  47. */
  48. public function key() {
  49. return $this->__key;
  50. }
  51. // This doc comment block generated by idl/sysdoc.php
  52. /**
  53. * ( excerpt from http://docs.hhvm.com/manual/en/splobjectstorage.current.php )
  54. *
  55. * Returns the current storage entry.
  56. *
  57. * @return mixed The object at the current iterator position.
  58. */
  59. public function current() {
  60. return current($this->__storage)['obj'];
  61. }
  62. // This doc comment block generated by idl/sysdoc.php
  63. /**
  64. * ( excerpt from http://docs.hhvm.com/manual/en/splobjectstorage.next.php )
  65. *
  66. * Moves the iterator to the next object in the storage.
  67. *
  68. * @return mixed No value is returned.
  69. */
  70. public function next() {
  71. next($this->__storage);
  72. $this->__key++;
  73. }
  74. // This doc comment block generated by idl/sysdoc.php
  75. /**
  76. * ( excerpt from http://docs.hhvm.com/manual/en/splobjectstorage.count.php )
  77. *
  78. * Counts the number of objects in the storage.
  79. *
  80. * @return mixed The number of objects in the storage.
  81. */
  82. public function count() {
  83. return count($this->__storage);
  84. }
  85. // This doc comment block generated by idl/sysdoc.php
  86. /**
  87. * ( excerpt from http://docs.hhvm.com/manual/en/splobjectstorage.contains.php )
  88. *
  89. * Checks if the storage contains the object provided.
  90. *
  91. * @obj mixed The object to look for.
  92. *
  93. * @return mixed Returns TRUE if the object is in the storage, FALSE
  94. * otherwise.
  95. */
  96. public function contains($obj) {
  97. if (gettype($obj) === 'object') {
  98. return isset($this->__storage[$this->getHashAndValidate($obj)]);
  99. }
  100. return false;
  101. }
  102. // This doc comment block generated by idl/sysdoc.php
  103. /**
  104. * ( excerpt from http://docs.hhvm.com/manual/en/splobjectstorage.attach.php )
  105. *
  106. * Adds an object inside the storage, and optionally associate it to some
  107. * data.
  108. *
  109. * @obj mixed The object to add.
  110. * @data mixed The data to associate with the object.
  111. *
  112. * @return mixed No value is returned.
  113. */
  114. public function attach($obj, $data = null) {
  115. if (gettype($obj) === 'object') {
  116. $this->__storage[$this->getHashAndValidate($obj)] = array(
  117. 'obj' => $obj, 'inf' => $data
  118. );
  119. }
  120. }
  121. // This doc comment block generated by idl/sysdoc.php
  122. /**
  123. * ( excerpt from http://docs.hhvm.com/manual/en/splobjectstorage.detach.php )
  124. *
  125. * Removes the object from the storage.
  126. *
  127. * @obj mixed The object to remove.
  128. *
  129. * @return mixed No value is returned.
  130. */
  131. public function detach($obj) {
  132. if (gettype($obj) === 'object') {
  133. unset($this->__storage[$this->getHashAndValidate($obj)]);
  134. }
  135. }
  136. // This doc comment block generated by idl/sysdoc.php
  137. /**
  138. * ( excerpt from
  139. * http://docs.hhvm.com/manual/en/splobjectstorage.offsetexists.php )
  140. *
  141. * Checks whether an object exists in the storage.
  142. *
  143. * SplObjectStorage::offsetExists() is an alias of
  144. * SplObjectStorage::contains().
  145. *
  146. * @object mixed The object to look for.
  147. *
  148. * @return mixed Returns TRUE if the object exists in the storage,
  149. * and FALSE otherwise.
  150. */
  151. public function offsetExists($object) {
  152. return $this->contains($object);
  153. }
  154. // This doc comment block generated by idl/sysdoc.php
  155. /**
  156. * ( excerpt from http://docs.hhvm.com/manual/en/splobjectstorage.offsetget.php )
  157. *
  158. * Returns the data associated with an object in the storage.
  159. *
  160. * @object mixed The object to look for.
  161. *
  162. * @return mixed The data previously associated with the object in
  163. * the storage.
  164. */
  165. public function offsetGet($object) {
  166. if (gettype($object) === 'object') {
  167. if (!$this->contains($object)) {
  168. throw new UnexpectedValueException('Object not found');
  169. }
  170. return $this->__storage[$this->getHashAndValidate($object)]['inf'];
  171. }
  172. }
  173. // This doc comment block generated by idl/sysdoc.php
  174. /**
  175. * ( excerpt from http://docs.hhvm.com/manual/en/splobjectstorage.offsetset.php )
  176. *
  177. * Associate data to an object in the storage.
  178. *
  179. * SplObjectStorage::offsetSet() is an alias of
  180. * SplObjectStorage::attach().
  181. *
  182. * @object mixed The object to associate data with.
  183. * @data mixed The data to associate with the object.
  184. *
  185. * @return mixed No value is returned.
  186. */
  187. public function offsetSet($object, $data = null) {
  188. return $this->attach($object, $data);
  189. }
  190. // This doc comment block generated by idl/sysdoc.php
  191. /**
  192. * ( excerpt from http://docs.hhvm.com/manual/en/splobjectstorage.offsetunset.php
  193. * )
  194. *
  195. * Removes an object from the storage.
  196. *
  197. * SplObjectStorage::offsetUnset() is an alias of
  198. * SplObjectStorage::detach().
  199. *
  200. * @object mixed The object to remove.
  201. *
  202. * @return mixed No value is returned.
  203. */
  204. public function offsetUnset($object) {
  205. return $this->detach($object);
  206. }
  207. // This doc comment block generated by idl/sysdoc.php
  208. /**
  209. * ( excerpt from http://docs.hhvm.com/manual/en/splobjectstorage.removeall.php )
  210. *
  211. * Removes objects contained in another storage from the current storage.
  212. *
  213. * @storage mixed The storage containing the elements to remove.
  214. *
  215. * @return mixed No value is returned.
  216. */
  217. public function removeAll($storage) {
  218. $cache = array();
  219. foreach ($storage as $obj) {
  220. $cache[] = $obj;
  221. }
  222. foreach ($cache as $obj) {
  223. $this->detach($obj);
  224. }
  225. }
  226. // This doc comment block generated by idl/sysdoc.php
  227. /**
  228. * ( excerpt from
  229. * http://docs.hhvm.com/manual/en/splobjectstorage.removeallexcept.php )
  230. *
  231. * Removes all objects except for those contained in another storage from
  232. * the current storage.
  233. *
  234. * @storage mixed The storage containing the elements to retain in the
  235. * current storage.
  236. *
  237. * @return mixed No value is returned.
  238. */
  239. public function removeAllExcept($storage) {
  240. $cache = array();
  241. foreach ($this->__storage as $object) {
  242. if (!$storage->contains($object['obj'])) {
  243. $cache[] = $object['obj'];
  244. }
  245. }
  246. foreach ($cache as $object) {
  247. $this->detach($object);
  248. }
  249. }
  250. // This doc comment block generated by idl/sysdoc.php
  251. /**
  252. * ( excerpt from http://docs.hhvm.com/manual/en/splobjectstorage.addall.php )
  253. *
  254. * Adds all objects-data pairs from a different storage in the current
  255. * storage.
  256. *
  257. * @storage mixed The storage you want to import.
  258. *
  259. * @return mixed No value is returned.
  260. */
  261. public function addAll($storage) {
  262. foreach ($storage as $object) {
  263. $this->attach($object);
  264. }
  265. }
  266. // This doc comment block generated by idl/sysdoc.php
  267. /**
  268. * ( excerpt from http://docs.hhvm.com/manual/en/splobjectstorage.gethash.php )
  269. *
  270. * This method calculates an identifier for the objects added to an
  271. * SplObjectStorage object.
  272. *
  273. * The implementation in SplObjectStorage returns the same value as
  274. * spl_object_hash().
  275. *
  276. * The storage object will never contain more than one object with the
  277. * same identifier. As such, it can be used to implement a set (a
  278. * collection of unique values) where the quality of an object being unique
  279. * is determined by the value returned by this function being unique.
  280. *
  281. * @object mixed The object whose identifier is to be calculated.
  282. *
  283. * @return mixed A string with the calculated identifier.
  284. */
  285. public function getHash($object) {
  286. return spl_object_hash($object);
  287. }
  288. private function getHashAndValidate($object) {
  289. $hash = $this->getHash($object);
  290. if (!is_string($hash)) {
  291. throw new RuntimeException('Hash needs to be a string');
  292. }
  293. return $hash;
  294. }
  295. // This doc comment block generated by idl/sysdoc.php
  296. /**
  297. * ( excerpt from http://docs.hhvm.com/manual/en/splobjectstorage.serialize.php )
  298. *
  299. * Returns a string representation of the storage.
  300. *
  301. * @return mixed A string representing the storage.
  302. */
  303. public function serialize() {
  304. return serialize($this->__storage);
  305. }
  306. // This doc comment block generated by idl/sysdoc.php
  307. /**
  308. * ( excerpt from http://docs.hhvm.com/manual/en/splobjectstorage.unserialize.php
  309. * )
  310. *
  311. * Unserializes storage entries and attach them to the current storage.
  312. *
  313. * @serialized mixed The serialized representation of a storage.
  314. *
  315. * @return mixed No value is returned.
  316. */
  317. public function unserialize($serialized) {
  318. $arr = @unserialize($serialized);
  319. // check for error while unserializing.
  320. // we need to differentiate serialized(false) and false returned because of
  321. // a bad string
  322. if ($arr === false && serialize(false) !== $serialized) {
  323. throw new UnexpectedValueException('Error while unserializing');
  324. }
  325. if (is_array($arr)) {
  326. $this->__storage = $arr;
  327. }
  328. }
  329. // This doc comment block generated by idl/sysdoc.php
  330. /**
  331. * ( excerpt from http://docs.hhvm.com/manual/en/splobjectstorage.setinfo.php )
  332. *
  333. * Associates data, or info, with the object currently pointed to by the
  334. * iterator.
  335. *
  336. * @data mixed The data to associate with the current iterator
  337. * entry.
  338. *
  339. * @return mixed No value is returned.
  340. */
  341. public function setInfo($data) {
  342. current($this->__storage)['inf'] = $data;;
  343. }
  344. /**
  345. * ( excerpt from http://docs.hhvm.com/manual/en/splobjectstorage.getinfo.php )
  346. *
  347. * Returns the data associated with the current iterator entry.
  348. *
  349. * @return mixed Returns the data, or info, associated with the
  350. * object pointed by the current iterator position.
  351. */
  352. public function getInfo() {
  353. return current($this->__storage)['inf'];
  354. }
  355. }