alexmanno /
trucker
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.
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Trucker\Responses; |
||
| 4 | |||
| 5 | /** |
||
| 6 | * Collection class returned from CollectionFinder when |
||
| 7 | * a colleciton of results is requested. |
||
| 8 | * |
||
| 9 | * @author Alessandro Manno <[email protected]> |
||
| 10 | */ |
||
| 11 | class Collection implements \Iterator |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * Var to hold the actual source array collection. |
||
| 15 | * |
||
| 16 | * @var array |
||
| 17 | */ |
||
| 18 | private $collection; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Associative array of metadata related to the |
||
| 22 | * collection. |
||
| 23 | * |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | public $metaData = []; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Constructor for the collection. |
||
| 30 | * |
||
| 31 | * @param array $givenArray array of objects |
||
| 32 | */ |
||
| 33 | 14 | public function __construct($givenArray) |
|
| 34 | { |
||
| 35 | 14 | $this->collection = $givenArray; |
|
| 36 | 14 | } |
|
| 37 | |||
| 38 | /** |
||
| 39 | * Function to conform with Iterator interface. |
||
| 40 | * |
||
| 41 | * @see Iterator |
||
| 42 | * |
||
| 43 | * @return \Trucker\Resource\Model |
||
| 44 | */ |
||
| 45 | 1 | public function rewind() |
|
| 46 | { |
||
| 47 | 1 | return reset($this->collection); |
|
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Function to conform with Iterator interface. |
||
| 52 | * |
||
| 53 | * @see Iterator |
||
| 54 | * |
||
| 55 | * @return \Trucker\Resource\Model |
||
| 56 | */ |
||
| 57 | 1 | public function current() |
|
| 58 | { |
||
| 59 | 1 | return current($this->collection); |
|
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Function to conform with Iterator interface. |
||
| 64 | * |
||
| 65 | * @see Iterator |
||
| 66 | * |
||
| 67 | * @return \Trucker\Resource\Model |
||
| 68 | */ |
||
| 69 | 1 | public function key() |
|
| 70 | { |
||
| 71 | 1 | return key($this->collection); |
|
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Function to conform with Iterator interface. |
||
| 76 | * |
||
| 77 | * @see Iterator |
||
| 78 | * |
||
| 79 | * @return \Trucker\Resource\Model |
||
| 80 | */ |
||
| 81 | 3 | public function next() |
|
| 82 | { |
||
| 83 | 3 | return next($this->collection); |
|
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Function to conform with Iterator interface. |
||
| 88 | * |
||
| 89 | * @see Iterator |
||
| 90 | * |
||
| 91 | * @return bool |
||
| 92 | */ |
||
| 93 | 1 | public function valid() |
|
| 94 | { |
||
| 95 | 1 | return null !== key($this->collection); |
|
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Function to return the size of the collection. |
||
| 100 | * |
||
| 101 | * @return int size of collection |
||
| 102 | */ |
||
| 103 | 5 | public function size() |
|
| 104 | { |
||
| 105 | 5 | return count($this->collection); |
|
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Function to return the first item of the collection. |
||
| 110 | * |
||
| 111 | * @return \Trucker\Resource\Model |
||
| 112 | */ |
||
| 113 | 5 | public function first() |
|
| 114 | { |
||
| 115 | 5 | return empty($this->collection) ? null : $this->collection[0]; |
|
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Function to return the last item of the collection. |
||
| 120 | * |
||
| 121 | * @return \Trucker\Resource\Model |
||
| 122 | */ |
||
| 123 | 1 | public function last() |
|
| 124 | { |
||
| 125 | 1 | return empty($this->collection) ? null : $this->collection[count($this->collection) - 1]; |
|
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Function to convert the collection to an array using |
||
| 130 | * each collection elements attributes. |
||
| 131 | * |
||
| 132 | * @param string $collectionKey |
||
| 133 | * @param string $metaKey |
||
| 134 | * |
||
| 135 | * @return array |
||
| 136 | */ |
||
| 137 | 3 | public function toArray($collectionKey = null, $metaKey = 'meta') |
|
| 138 | { |
||
| 139 | 3 | $entities = []; |
|
| 140 | 3 | foreach ($this->collection as $entity) { |
|
| 141 | 3 | $entities[] = $entity->attributes(); |
|
| 142 | } |
||
| 143 | |||
| 144 | 3 | $col = $entities; |
|
| 145 | 3 | if ($collectionKey) { |
|
|
0 ignored issues
–
show
The expression
$collectionKey of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.
In PHP, under loose comparison (like For '' == false // true
'' == null // true
'ab' == false // false
'ab' == null // false
// It is often better to use strict comparison
'' === false // false
'' === null // false
Loading history...
|
|||
| 146 | 3 | $col = [$collectionKey => $entities]; |
|
| 147 | } |
||
| 148 | |||
| 149 | 3 | $met = []; |
|
| 150 | 3 | if ($this->metaData) { |
|
|
0 ignored issues
–
show
The expression
$this->metaData of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent. Consider making the comparison explicit by using Loading history...
|
|||
| 151 | 2 | $met = [$metaKey => $this->metaData]; |
|
| 152 | } |
||
| 153 | |||
| 154 | 3 | return array_merge($col, $met); |
|
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Function to convert the collection to json using |
||
| 159 | * each collection elements attributes as an array then |
||
| 160 | * encoding the array to json. |
||
| 161 | * |
||
| 162 | * @param string $collectionKey |
||
| 163 | * @param string $metaKey |
||
| 164 | * |
||
| 165 | * @return array |
||
| 166 | */ |
||
| 167 | 1 | public function toJson($collectionKey = null, $metaKey = 'meta') |
|
| 168 | { |
||
| 169 | 1 | return json_encode($this->toArray($collectionKey, $metaKey)); |
|
|
0 ignored issues
–
show
|
|||
| 170 | } |
||
| 171 | }//end class |
||
| 172 |