44
55namespace Infinityloop \Utils ;
66
7- abstract class ObjectSet implements \Iterator, \ArrayAccess, \Countable
7+ abstract class ObjectSet extends \ Infinityloop \ Utils \BaseSet
88{
9- use \Nette \SmartObject;
10-
11- protected const INNER_CLASS = self ::class;
12-
13- protected array $ array = [];
14-
15- public function __construct (array $ data )
9+ public function __construct (array $ data = [])
1610 {
1711 foreach ($ data as $ object ) {
1812 $ this ->offsetSet (null , $ object );
1913 }
2014 }
2115
22- public function toArray () : array
16+ public function key () : int
2317 {
24- return $ this ->array ;
18+ return \key ( $ this ->array ) ;
2519 }
2620
27- public function merge ( self $ objectSet , bool $ allowReplace = false ) : self
21+ protected function mergeImpl ( BaseSet $ objectSet , bool $ allowReplace = false ) : self
2822 {
29- if (!$ objectSet instanceof static) {
30- throw new \Exception ('I can only merge ObjectSets of same type ' );
31- }
32-
3323 foreach ($ objectSet as $ offset => $ object ) {
3424 $ this ->offsetSet ($ allowReplace
3525 ? $ offset
@@ -39,95 +29,20 @@ public function merge(self $objectSet, bool $allowReplace = false) : self
3929 return $ this ;
4030 }
4131
42- public function current () : object
43- {
44- return \current ($ this ->array );
45- }
46-
47- public function next () : void
48- {
49- \next ($ this ->array );
50- }
51-
52- /** @return int|string */
53- public function key ()
32+ protected function offsetSetImpl ($ offset , object $ object ) : void
5433 {
55- return \key ($ this ->array );
56- }
57-
58- public function valid () : bool
59- {
60- return \key ($ this ->array ) !== null ;
61- }
62-
63- public function rewind () : void
64- {
65- \reset ($ this ->array );
66- }
67-
68- public function count () : int
69- {
70- return \count ($ this ->array );
71- }
72-
73- public function offsetExists ($ offset ) : bool
74- {
75- return \array_key_exists ($ offset , $ this ->array );
76- }
77-
78- public function offsetGet ($ offset ) : object
79- {
80- if (!$ this ->offsetExists ($ offset )) {
81- throw new \Exception ('Item doesnt exist. ' );
82- }
83-
84- return $ this ->array [$ offset ];
85- }
86-
87- public function offsetSet ($ offset , $ object ) : void
88- {
89- if (!\is_a ($ object , static ::INNER_CLASS )) {
90- throw new \Exception ('Invalid input. ' );
91- }
92-
93- $ key = $ this ->getKey ($ object );
94-
9534 if ($ offset === null ) {
96- if ($ key === null ) {
97- $ this ->array [] = $ object ;
98-
99- return ;
100- }
101-
102- if ($ this ->offsetExists ($ key )) {
103- throw new \Exception ('Duplicated item. Set using explicit key if you wish to replace. ' );
104- }
105-
106- $ this ->array [$ key ] = $ object ;
35+ $ this ->array [] = $ object ;
10736
10837 return ;
10938 }
11039
111- if (( $ key === null && \is_int ($ offset )) || ( \is_string ( $ key ) && \is_string ( $ offset ) && $ key === $ offset )) {
40+ if (\is_int ($ offset )) {
11241 $ this ->array [$ offset ] = $ object ;
11342
11443 return ;
11544 }
11645
11746 throw new \Exception ('Invalid offset for given object. ' );
11847 }
119-
120- public function offsetUnset ($ offset ) : void
121- {
122- if (!$ this ->offsetExists ($ offset )) {
123- throw new \Exception ('Item already doesnt exist. ' );
124- }
125-
126- unset($ this ->array [$ offset ]);
127- }
128-
129- protected function getKey (object $ object ) : ?string
130- {
131- return null ;
132- }
13348}
0 commit comments