dominionenterprises /
tol-api-php
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 | namespace DominionEnterprises\Api; |
||
| 4 | use DominionEnterprises\Util; |
||
| 5 | |||
| 6 | /** |
||
| 7 | * Class for iterating index responses. Collections are readonly |
||
| 8 | */ |
||
| 9 | final class Collection implements \Iterator, \Countable |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * API Client |
||
| 13 | * |
||
| 14 | * @var Client |
||
| 15 | */ |
||
| 16 | private $_client; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * limit to give to API |
||
| 20 | * |
||
| 21 | * @var int |
||
| 22 | */ |
||
| 23 | private $_limit; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * offset to give to API |
||
| 27 | * |
||
| 28 | * @var int |
||
| 29 | */ |
||
| 30 | private $_offset; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * resource name for collection |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | private $_resource; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * array of filters to pass to API |
||
| 41 | * |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | private $_filters; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Total number of elements in the collection |
||
| 48 | * |
||
| 49 | * @var int |
||
| 50 | */ |
||
| 51 | private $_total; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * pointer in the paginated results |
||
| 55 | * |
||
| 56 | * @var int |
||
| 57 | */ |
||
| 58 | private $_position; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * A paginated set of elements from the API |
||
| 62 | * |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | private $_result; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Create a new collection |
||
| 69 | * |
||
| 70 | * @param Client $client client connection to the API |
||
| 71 | * @param string $resource name of API resource to request |
||
| 72 | * @param array $filters key value pair array of search filters |
||
| 73 | */ |
||
| 74 | public function __construct(Client $client, $resource, array $filters = []) |
||
| 75 | { |
||
| 76 | Util::throwIfNotType(['string' => [$resource]], true); |
||
| 77 | |||
| 78 | $this->_client = $client; |
||
| 79 | $this->_resource = $resource; |
||
| 80 | $this->_filters = $filters; |
||
| 81 | $this->rewind(); |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @see Countable::count() |
||
| 86 | * |
||
| 87 | * @return int |
||
| 88 | */ |
||
| 89 | public function count() |
||
| 90 | { |
||
| 91 | if ($this->_position === -1) { |
||
| 92 | $this->next(); |
||
| 93 | } |
||
| 94 | |||
| 95 | return $this->_total; |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @see Iterator::rewind() |
||
| 100 | * |
||
| 101 | * @return void |
||
| 102 | */ |
||
| 103 | public function rewind() |
||
| 104 | { |
||
| 105 | $this->_result = null; |
||
|
0 ignored issues
–
show
|
|||
| 106 | $this->_offset = 0; |
||
| 107 | $this->_total = 0; |
||
| 108 | $this->_limit = 0; |
||
| 109 | $this->_position = -1; |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @see Iterator::key() |
||
| 114 | * |
||
| 115 | * @return int |
||
| 116 | */ |
||
| 117 | View Code Duplication | public function key() |
|
| 118 | { |
||
| 119 | if ($this->_position === -1) { |
||
| 120 | $this->next(); |
||
| 121 | } |
||
| 122 | |||
| 123 | Util::ensure(false, empty($this->_result), '\OutOfBoundsException', ['Collection contains no elements']); |
||
| 124 | |||
| 125 | return $this->_offset + $this->_position; |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @see Iterator::valid() |
||
| 130 | * |
||
| 131 | * @return bool |
||
| 132 | */ |
||
| 133 | public function valid() |
||
| 134 | { |
||
| 135 | if ($this->_position === -1) { |
||
| 136 | $this->next(); |
||
| 137 | } |
||
| 138 | |||
| 139 | return $this->_offset + $this->_position < $this->_total; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @see Iterator::next() |
||
| 144 | * |
||
| 145 | * @return void |
||
| 146 | */ |
||
| 147 | public function next() |
||
| 148 | { |
||
| 149 | ++$this->_position; |
||
| 150 | |||
| 151 | if ($this->_position < $this->_limit) { |
||
| 152 | return; |
||
| 153 | } |
||
| 154 | |||
| 155 | $this->_offset += $this->_limit; |
||
| 156 | $this->_filters['offset'] = $this->_offset; |
||
| 157 | $indexResponse = $this->_client->index($this->_resource, $this->_filters); |
||
| 158 | |||
| 159 | $httpCode = $indexResponse->getHttpCode(); |
||
| 160 | Util::ensure(200, $httpCode, "Did not receive 200 from API. Instead received {$httpCode}"); |
||
| 161 | |||
| 162 | $response = $indexResponse->getResponse(); |
||
| 163 | $this->_limit = $response['pagination']['limit']; |
||
| 164 | $this->_total = $response['pagination']['total']; |
||
| 165 | $this->_result = $response['result']; |
||
| 166 | $this->_position = 0; |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @see Iterator::current() |
||
| 171 | * |
||
| 172 | * @return array |
||
| 173 | */ |
||
| 174 | View Code Duplication | public function current() |
|
| 175 | { |
||
| 176 | if ($this->_position === -1) { |
||
| 177 | $this->next(); |
||
| 178 | } |
||
| 179 | |||
| 180 | Util::ensure( |
||
| 181 | true, |
||
| 182 | array_key_exists($this->_position, $this->_result), |
||
| 183 | '\OutOfBoundsException', |
||
| 184 | ['Collection contains no element at current position'] |
||
| 185 | ); |
||
| 186 | |||
| 187 | return $this->_result[$this->_position]; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Returns the values from a single field this collection, identified by the given $key. |
||
| 192 | * |
||
| 193 | * @param string $key The name of the field for which the values will be returned. |
||
| 194 | * |
||
| 195 | * @return iterable |
||
| 196 | */ |
||
| 197 | public function column($key) |
||
| 198 | { |
||
| 199 | foreach ($this as $item) { |
||
| 200 | yield Util\Arrays::get($item, $key); |
||
| 201 | } |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Return an iterable generator containing only the fields specified in the $keys array. |
||
| 206 | * |
||
| 207 | * @param array $keys The list of field names to be returned. |
||
| 208 | * |
||
| 209 | * @return \Generator |
||
| 210 | */ |
||
| 211 | public function select(array $keys) |
||
| 212 | { |
||
| 213 | foreach ($this as $item) { |
||
| 214 | $result = array_fill_keys($keys, null); |
||
| 215 | Util\Arrays::copyIfKeysExist($item, $result, $keys); |
||
| 216 | yield $result; |
||
| 217 | } |
||
| 218 | } |
||
| 219 | } |
||
| 220 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..