1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (https://nette.org)
5: *
6: * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
7: *
8: * For the full copyright and license information, please view
9: * the file license.txt that was distributed with this source code.
10: */
11:
12: namespace Nette\Collections;
13:
14: use Nette;
15:
16:
17:
18: /**
19: * Provides the base class for a collection that contains no duplicate elements.
20: *
21: * @author David Grudl
22: */
23: class Set extends Collection implements ISet
24: {
25:
26:
27: /**
28: * Appends the specified element to the end of this collection.
29: * @param mixed
30: * @return bool true if this collection changed as a result of the call
31: * @throws \InvalidArgumentException, \NotSupportedException
32: */
33: public function append($item)
34: {
35: $this->beforeAdd($item);
36:
37: if (is_object($item)) {
38: $key = spl_object_hash($item);
39: if (parent::offsetExists($key)) {
40: return FALSE;
41: }
42: parent::offsetSet($key, $item);
43: return TRUE;
44:
45: } else {
46: $key = $this->search($item);
47: if ($key === FALSE) {
48: parent::offsetSet(NULL, $item);
49: return TRUE;
50: }
51: return FALSE;
52: }
53: }
54:
55:
56:
57: /**
58: * Returns the index of the first occurrence of the specified element,
59: * or FALSE if this collection does not contain this element.
60: * @param mixed
61: * @return int|FALSE
62: * @throws \InvalidArgumentException
63: */
64: protected function search($item)
65: {
66: if (is_object($item)) {
67: $key = spl_object_hash($item);
68: return parent::offsetExists($key) ? $key : FALSE;
69:
70: } else {
71: return array_search($item, $this->getArrayCopy(), TRUE);
72: }
73: }
74:
75:
76:
77: /********************* ArrayObject cooperation ****************d*g**/
78:
79:
80:
81: /**
82: * Not supported (only appending).
83: */
84: public function offsetSet($key, $item)
85: {
86: if ($key === NULL) {
87: $this->append($item);
88: } else {
89: throw new \NotSupportedException;
90: }
91: }
92:
93:
94:
95: /**
96: * Not supported.
97: */
98: public function offsetGet($key)
99: {
100: throw new \NotSupportedException;
101: }
102:
103:
104:
105: /**
106: * Not supported.
107: */
108: public function offsetExists($key)
109: {
110: throw new \NotSupportedException;
111: }
112:
113:
114:
115: /**
116: * Not supported.
117: */
118: public function offsetUnset($key)
119: {
120: throw new \NotSupportedException;
121: }
122:
123: }
124: