| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Jmw\Collection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * This class provides a skeletal implementation of the Collection interface,  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * to minimize the effort required to implement this interface. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * To implement an unmodifiable collection, the programmer needs only to extend this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * class and provide implementations for the iterator and size methods, and use the ImmutableCollectionTrait trait | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * (The iterator returned by the iterator method must implement hasNext and next.) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @author john | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | abstract class CollectionAbstract implements CollectionInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | {	 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 	 * Adds all of the elements in the specified collection to this collection (optional operation). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	 * @param CollectionInterface $collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	 * @return boolean $changed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	*/ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 21 |  | View Code Duplication | 	public function addAll(CollectionInterface $collection) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 		$iterator = $collection->iterator(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 		$changed = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 		while($iterator->hasNext()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 			$changed = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 			$this->add($iterator->next()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 		return $changed;		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	 * Returns true if this collection contains the specified element. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	 * @return boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	*/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	public function contains($element) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 		$iterator = $this->iterator(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 		while($iterator->hasNext()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 			if($element === $iterator->next()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 			{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 				return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 		return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	 * Returns true if this collection contains all of the elements in the specified collection. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	 * @param Collection $collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 	 * @return boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	*/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	public function containsAll(CollectionInterface $collection) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 		$iterator = $collection->iterator(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 		while($iterator->hasNext()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 			if(!$this->contains($iterator->next())) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 			{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 				return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	 * Compares the specified object with this collection for equality. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	 * @param unknown $object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	 * @return boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	*/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 	public function equals($object) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 		if(!$object instanceof CollectionInterface) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 			return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 		return $this->hashCode() === $object->hashCode(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	 * Returns true if this collection contains no elements. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	 * @return boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	*/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 	public function isEmpty() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 		return $this->size() === 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	 * Retains only the elements in this collection that are contained in the specified collection (optional operation). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	 * @param CollectionInterface $collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 	 * @return boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 	*/ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 101 |  | View Code Duplication | 	public function retainAll(CollectionInterface $collection) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 		$iterator = $this->iterator(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 		$changed = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 		while($iterator->hasNext()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 			if(!$collection->contains($iterator->next())) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 			{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 				$iterator->remove(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 				$changed = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 		return $changed; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 	 * Returns a json string representing all the elements in this collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 	public function toJson() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 		$json = json_encode($this->toArray()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 		if($error = json_last_error()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 			throw new \Exception('Error encoding to json: ' . $error); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 		return $json; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 	 * Ensures that this collection contains the specified element (optional operation). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 	 * @param multitype $element | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 	 * @return boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 	public abstract function add($element); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 	 * Removes all of the elements from this collection (optional operation). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 	 * @return void | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 141 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  | 	public abstract function clear(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 	 * Returns an iterator over the elements in this collection. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 	 * @return IteratorInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 	public abstract function iterator(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 	 * Removes a single instance of the specified element from this collection, if it is present (optional operation). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 	 * @param multitype $element | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 	 * @return boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 	*/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 	public abstract function remove($element); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 	 * Removes all of this collection's elements that are also contained in the specified collection (optional operation). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 	 * @param CollectionInterface $collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 	 * @return boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 	*/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 	public abstract function removeAll(CollectionInterface $collection); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 	 * Returns the number of elements in this collection. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 	 * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  | 	*/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | 	public abstract function size(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | 	 * Returns an array containing all of the elements in this collection. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  | 	 * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  | 	*/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  | 	public abstract function toArray(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  | 	 * Returns the hash code value for this collection. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  | 	 */ | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 180 |  |  | 	public abstract function hashCode(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 181 |  |  | } |