appserver-io /
collections
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * \AppserverIo\Collections\CollectionUtils |
||
| 5 | * |
||
| 6 | * NOTICE OF LICENSE |
||
| 7 | * |
||
| 8 | * This source file is subject to the Open Software License (OSL 3.0) |
||
| 9 | * that is available through the world-wide-web at this URL: |
||
| 10 | * http://opensource.org/licenses/osl-3.0.php |
||
| 11 | * |
||
| 12 | * PHP version 5 |
||
| 13 | * |
||
| 14 | * @author Tim Wagner <[email protected]> |
||
| 15 | * @copyright 2015 TechDivision GmbH <[email protected]> |
||
| 16 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
||
| 17 | * @link https://github.com/appserver-io/collections |
||
| 18 | * @link http://www.appserver.io |
||
| 19 | */ |
||
| 20 | |||
| 21 | namespace AppserverIo\Collections; |
||
| 22 | |||
| 23 | use AppserverIo\Lang\Object; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * This class implements static methods that can be used |
||
| 27 | * to work with Collections. |
||
| 28 | * |
||
| 29 | * @author Tim Wagner <[email protected]> |
||
| 30 | * @copyright 2015 TechDivision GmbH <[email protected]> |
||
| 31 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
||
| 32 | * @link https://github.com/appserver-io/collections |
||
| 33 | * @link http://www.appserver.io |
||
| 34 | */ |
||
| 35 | class CollectionUtils extends Object |
||
| 36 | { |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Standardconstructor that marks this class as util class. |
||
| 40 | */ |
||
| 41 | protected function __construct() |
||
| 42 | { |
||
| 43 | /* Marks class as utility */ |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * This method iterates over the passed IndexedCollection and invokes the evaluate |
||
| 48 | * method of the although passed Predicate on every object of the IndexedCollection. |
||
| 49 | * If the evaluate method returns false, the object is removed from the passed |
||
| 50 | * IndexedCollection. |
||
| 51 | * |
||
| 52 | * @param \AppserverIo\Collections\CollectionInterface $collection Holds the IndexedCollection that should be filtered |
||
| 53 | * @param \AppserverIo\Collections\PredicateInterface $predicate The Predicate that should be used for evaluation purposes |
||
| 54 | * @param integer $iterations Holds the size of successful iterations, after that the filter should run |
||
| 55 | * |
||
| 56 | * @return void |
||
| 57 | */ |
||
| 58 | 1 | public static function filter(CollectionInterface $collection, PredicateInterface $predicate, $iterations = 0) |
|
| 59 | { |
||
| 60 | // initialize the ArrayList that should be returned |
||
| 61 | 1 | $return = array(); |
|
| 62 | // initialize the counter for the iterations |
||
| 63 | 1 | $iteration = 0; |
|
| 64 | // iterate over the ArrayList and invoke the evaluate() |
||
| 65 | // method of the Predicate on every object of the ArrayList |
||
| 66 | 1 | foreach ($collection as $key => $object) { |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 67 | // if the Predicate returns true, then adding the object |
||
| 68 | // to the new ArrayList |
||
| 69 | 1 | if ($predicate->evaluate($object)) { |
|
| 70 | 1 | $return[$key] = $object; |
|
| 71 | // rise the iterator |
||
| 72 | 1 | $iteration ++; |
|
| 73 | // if the iterator is equal to the actual iteration number |
||
| 74 | // then stop |
||
| 75 | 1 | if ($iteration == $iterations) { |
|
| 76 | break; |
||
| 77 | } |
||
| 78 | 1 | } |
|
| 79 | 1 | } |
|
| 80 | // clear all elements and add the filtered |
||
| 81 | 1 | $collection->clear(); |
|
| 82 | 1 | $collection->addAll($return); |
|
| 83 | 1 | } |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Finds the first element in the given collection which matches |
||
| 87 | * the given predicate. |
||
| 88 | * |
||
| 89 | * If no element of the collection matches the predicate, null is |
||
| 90 | * returned. |
||
| 91 | * |
||
| 92 | * @param \AppserverIo\Collections\CollectionInterface $collection The collection to search |
||
| 93 | * @param \AppserverIo\Collections\PredicateInterface $predicate The predicate to use |
||
| 94 | * |
||
| 95 | * @return object Returns the first element of the collection which matches the predicate or null if none could be found |
||
| 96 | */ |
||
| 97 | public static function find(CollectionInterface $collection, PredicateInterface $predicate) |
||
| 98 | { |
||
| 99 | // iterate over the IndexedCollection and invoke the evaluate() |
||
| 100 | // method of the Predicate on every object of the IndexedCollection |
||
| 101 | foreach ($collection as $object) { |
||
|
0 ignored issues
–
show
|
|||
| 102 | // if the Predicate returns true, if the predicate returns true |
||
| 103 | if ($predicate->evaluate($object)) { |
||
| 104 | return $object; |
||
| 105 | } |
||
| 106 | } |
||
| 107 | // return nothing if no object was found |
||
| 108 | return; |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * This method iterates over the passed Collection and invokes the |
||
| 113 | * evaluate method of the although passed Predicate on every object |
||
| 114 | * of the Collection. |
||
| 115 | * If the evaluate method returns true the method |
||
| 116 | * returns true also. |
||
| 117 | * |
||
| 118 | * @param \AppserverIo\Collections\CollectionInterface $collection Holds the IndexedCollection that should be filtered |
||
| 119 | * @param \AppserverIo\Collections\PredicateInterface $predicate The Predicate that should be used for evaluation purposes |
||
| 120 | * |
||
| 121 | * @return boolean TRUE if the evaluate method of the Predicate returns TRUE |
||
| 122 | */ |
||
| 123 | public static function exists(CollectionInterface $collection, PredicateInterface $predicate) |
||
| 124 | { |
||
| 125 | // iterate over the Collection and invoke the evaluate() |
||
| 126 | // method of the Predicate on every object of the Collection |
||
| 127 | foreach ($collection as $object) { |
||
|
0 ignored issues
–
show
|
|||
| 128 | // if the Predicate returns true, if the predicate returns true |
||
| 129 | if ($predicate->evaluate($object)) { |
||
| 130 | return true; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | // return false if element is not found |
||
| 134 | return false; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * This method iterates over the passed Collection and invokes |
||
| 139 | * the evaluate method of the although passed Predicate on every object |
||
| 140 | * of the Collection. |
||
| 141 | * If the evaluate method returns |
||
| 142 | * true the method returns the key of the object. |
||
| 143 | * |
||
| 144 | * @param \AppserverIo\Collections\CollectionInterface $collection Holds the Collection that should be filtered |
||
| 145 | * @param \AppserverIo\Collections\PredicateInterface $predicate The Predicate that should be used for evaluation purposes |
||
| 146 | * |
||
| 147 | * @return mixed Holds the key of the first object with it's evaluate() method returning TRUE |
||
| 148 | */ |
||
| 149 | public static function findKey(CollectionInterface $collection, PredicateInterface $predicate) |
||
| 150 | { |
||
| 151 | // iterate over the IndexedCollection and invoke the evaluate() |
||
| 152 | // method of the Predicate on every object of the Collection |
||
| 153 | foreach ($collection as $key => $object) { |
||
|
0 ignored issues
–
show
|
|||
| 154 | // if the Predicate returns true, if the predicate returns true |
||
| 155 | if ($predicate->evaluate($object)) { |
||
| 156 | return $key; |
||
| 157 | } |
||
| 158 | } |
||
| 159 | // return nothing if no object was evaluated by the predicate |
||
| 160 | return; |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Returns a new Collection containing a - b. |
||
| 165 | * The cardinality of each |
||
| 166 | * element e in the returned Collection will be the cardinality of e |
||
| 167 | * in a minus the cardinality of e in b, or zero, whichever is greater. |
||
| 168 | * |
||
| 169 | * @param \AppserverIo\Collections\CollectionInterface $a The Collection to subtract from, must not be null |
||
| 170 | * @param \AppserverIo\Collections\CollectionInterface $b The Collection to subtract, must not be null |
||
| 171 | * |
||
| 172 | * @return void |
||
| 173 | */ |
||
| 174 | public static function subtract(CollectionInterface $a, CollectionInterface $b) |
||
| 175 | { |
||
| 176 | // initialize the array with the value to return that should be returned |
||
| 177 | $return = array(); |
||
| 178 | // iterate over the Collection and check if the object exists in |
||
| 179 | // the second Collection |
||
| 180 | foreach ($a as $key => $element) { |
||
|
0 ignored issues
–
show
|
|||
| 181 | // if the object does not exist in the second Collection add |
||
| 182 | // it to the return array |
||
| 183 | if ($b->exists($key)) { |
||
| 184 | $return[$key] = $element; |
||
| 185 | } |
||
| 186 | } |
||
| 187 | // clear all elements and add the subtracted |
||
| 188 | $a->clear(); |
||
| 189 | $a->addAll($return); |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * This method sorts the passed collection depending |
||
| 194 | * on the comparator. |
||
| 195 | * |
||
| 196 | * @param \AppserverIo\Collections\CollectionInterface $collection Holds the Collection that should be sorted |
||
| 197 | * @param \AppserverIo\Collections\ComparatorInterface $comperator The Comparator that should be used for compare purposes |
||
| 198 | * |
||
| 199 | * @return void |
||
| 200 | */ |
||
| 201 | 2 | public static function sort(CollectionInterface $collection, ComparatorInterface $comperator) |
|
| 202 | { |
||
| 203 | // initialize the ArrayList that should be returned |
||
| 204 | // sort the ArrayList |
||
| 205 | 2 | $return = CollectionUtils::arraySort($collection->toArray(), 0, $collection->size(), $comperator); |
|
| 206 | // clear all elements and add the sorted |
||
| 207 | 2 | $collection->clear(); |
|
| 208 | 2 | $collection->addAll($return); |
|
| 209 | 2 | } |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Sorts the passed array. |
||
| 213 | * |
||
| 214 | * @param array $src The Array to be sorted |
||
| 215 | * @param integer $low The offset we start sorting |
||
| 216 | * @param integer $high The number of elements to be sorted |
||
| 217 | * @param \AppserverIo\Collections\ComparatorInterface $comperator The comperator used for sorting |
||
| 218 | * |
||
| 219 | * @return array The sorted array |
||
| 220 | */ |
||
| 221 | 2 | protected static function arraySort($src, $low, $high, ComparatorInterface $comperator) |
|
| 222 | { |
||
| 223 | // sort the array |
||
| 224 | 2 | for ($i = $low; $i < $high; $i ++) { |
|
| 225 | 2 | for ($j = $i; ($j > $low) && ($comperator->compare($src[$j - 1], $src[$j]) > 0); $j --) { |
|
| 226 | 2 | $src = CollectionUtils::swap($src, $j, $j - 1); |
|
| 227 | 2 | } |
|
| 228 | 2 | } |
|
| 229 | // return the sorted array |
||
| 230 | 2 | return $src; |
|
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Swaps x[a] with x[b]. |
||
| 235 | * |
||
| 236 | * @param array $x The array with the values to be swapped |
||
| 237 | * @param integer $a The position of the element to be swapped |
||
| 238 | * @param integer $b The position of the element to swap $a with |
||
| 239 | * |
||
| 240 | * @return array |
||
| 241 | */ |
||
| 242 | 2 | protected static function swap($x, $a, $b) |
|
| 243 | { |
||
| 244 | 2 | $t = $x[$a]; |
|
| 245 | 2 | $x[$a] = $x[$b]; |
|
| 246 | 2 | $x[$b] = $t; |
|
| 247 | 2 | return $x; |
|
| 248 | } |
||
| 249 | } |
||
| 250 |