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