JimChenWYU /
TimSDK
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 | * Created by PhpStorm. |
||
| 4 | * User: lenovo |
||
| 5 | * Date: 6/13/2018 |
||
| 6 | * Time: 11:29 PM |
||
| 7 | */ |
||
| 8 | |||
| 9 | namespace TimSDK\Support; |
||
| 10 | |||
| 11 | use ArrayAccess; |
||
| 12 | use ArrayIterator; |
||
| 13 | use Countable; |
||
| 14 | use IteratorAggregate; |
||
| 15 | use JsonSerializable; |
||
| 16 | use Serializable; |
||
| 17 | use TimSDK\Core\Exceptions\JsonParseException; |
||
| 18 | |||
| 19 | class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable, Serializable |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * The collection data. |
||
| 23 | * |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | protected $items = []; |
||
| 27 | |||
| 28 | public function __construct($items = []) |
||
| 29 | { |
||
| 30 | $this->items = $this->getArrayableItems($items); |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Return all items. |
||
| 35 | * |
||
| 36 | * @return array |
||
| 37 | */ |
||
| 38 | public function all() |
||
| 39 | { |
||
| 40 | return $this->items; |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Return specific items. |
||
| 45 | * |
||
| 46 | * @param array $keys |
||
| 47 | * |
||
| 48 | * @return array |
||
| 49 | */ |
||
| 50 | public function only($keys) |
||
| 51 | { |
||
| 52 | if (is_string($keys)) { |
||
| 53 | $keys = explode(',', str_replace(' ', '', $keys)); |
||
| 54 | } |
||
| 55 | |||
| 56 | $return = []; |
||
| 57 | foreach ($keys as $key) { |
||
| 58 | $value = $this->get($key); |
||
| 59 | if (!is_null($value)) { |
||
| 60 | $return[$key] = $value; |
||
| 61 | } |
||
| 62 | } |
||
| 63 | return $return; |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Replace the items |
||
| 68 | * |
||
| 69 | * @param $items |
||
| 70 | */ |
||
| 71 | public function setAll($items) |
||
| 72 | { |
||
| 73 | $this->items = $this->getArrayableItems($items); |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Set the item value. |
||
| 78 | * |
||
| 79 | * @param string $key |
||
| 80 | * @param mixed $value |
||
| 81 | */ |
||
| 82 | public function set($key, $value) |
||
| 83 | { |
||
| 84 | if (is_null($key)) { |
||
| 85 | $this->items[] = $value; |
||
| 86 | } else { |
||
| 87 | Arr::set($this->items, $key, $value); |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Retrieve item from Collection. |
||
| 93 | * |
||
| 94 | * @param string $key |
||
| 95 | * @param mixed $default |
||
| 96 | * |
||
| 97 | * @return mixed |
||
| 98 | */ |
||
| 99 | public function get($key, $default = null) |
||
| 100 | { |
||
| 101 | return Arr::get($this->items, $key, $default); |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * To determine Whether the specified element exists |
||
| 106 | * |
||
| 107 | * @param string $key |
||
| 108 | * @return bool |
||
| 109 | */ |
||
| 110 | public function has($key) |
||
| 111 | { |
||
| 112 | return Arr::has($this->items, $key); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Remove item form Collection. |
||
| 117 | * |
||
| 118 | * @param array|string $keys |
||
| 119 | */ |
||
| 120 | public function forget($keys) |
||
| 121 | { |
||
| 122 | Arr::forget($this->items, $keys); |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Merge data. |
||
| 127 | * |
||
| 128 | * @param Collection|array $items |
||
| 129 | * |
||
| 130 | * @return \TimSDK\Support\Collection |
||
| 131 | */ |
||
| 132 | public function merge($items) |
||
| 133 | { |
||
| 134 | $items = $this->getArrayableItems($items); |
||
| 135 | |||
| 136 | foreach ($items as $key => $value) { |
||
| 137 | $this->set($key, $value); |
||
| 138 | } |
||
| 139 | |||
| 140 | return new static($this->all()); |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Build to array. |
||
| 145 | * |
||
| 146 | * @return array |
||
| 147 | */ |
||
| 148 | public function toArray() |
||
| 149 | { |
||
| 150 | return $this->all(); |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Build to json. |
||
| 155 | * |
||
| 156 | * @param int $option |
||
| 157 | * |
||
| 158 | * @return string |
||
| 159 | */ |
||
| 160 | public function toJson($option = JSON_UNESCAPED_UNICODE) |
||
| 161 | { |
||
| 162 | return json_encode($this->all(), $option); |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Count elements of an object |
||
| 167 | * @link http://php.net/manual/en/countable.count.php |
||
| 168 | * @return int The custom count as an integer. |
||
| 169 | * </p> |
||
| 170 | * <p> |
||
| 171 | * The return value is cast to an integer. |
||
| 172 | * @since 5.1.0 |
||
| 173 | */ |
||
| 174 | public function count() |
||
| 175 | { |
||
| 176 | return count($this->items); |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Retrieve an external iterator |
||
| 181 | * @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
| 182 | * @return ArrayIterator |
||
| 183 | * <b>Traversable</b> |
||
| 184 | * @since 5.0.0 |
||
| 185 | */ |
||
| 186 | public function getIterator() |
||
| 187 | { |
||
| 188 | return new ArrayIterator($this->items); |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Specify data which should be serialized to JSON |
||
| 193 | * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
||
| 194 | * @return mixed data which can be serialized by <b>json_encode</b>, |
||
| 195 | * which is a value of any type other than a resource. |
||
| 196 | * @since 5.4.0 |
||
| 197 | */ |
||
| 198 | public function jsonSerialize() |
||
| 199 | { |
||
| 200 | return $this->items; |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * String representation of object |
||
| 205 | * @link http://php.net/manual/en/serializable.serialize.php |
||
| 206 | * @return string the string representation of the object or null |
||
| 207 | * @since 5.1.0 |
||
| 208 | */ |
||
| 209 | public function serialize() |
||
| 210 | { |
||
| 211 | return serialize($this->items); |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Constructs the object |
||
| 216 | * @link http://php.net/manual/en/serializable.unserialize.php |
||
| 217 | * @param string $serialized <p> |
||
| 218 | * The string representation of the object. |
||
| 219 | * </p> |
||
| 220 | * @return array |
||
| 221 | * @since 5.1.0 |
||
| 222 | */ |
||
| 223 | public function unserialize($serialized) |
||
| 224 | { |
||
| 225 | return $this->items = unserialize($serialized); |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * To string. |
||
| 230 | * |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | public function __toString() |
||
| 234 | { |
||
| 235 | return $this->toJson(); |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Assigns a value to the specified data. |
||
| 240 | * |
||
| 241 | * @param string $key |
||
| 242 | * @param mixed $value |
||
| 243 | */ |
||
| 244 | public function __set($key, $value) |
||
| 245 | { |
||
| 246 | $this->set($key, $value); |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Get a data by key. |
||
| 251 | * |
||
| 252 | * @param string $key |
||
| 253 | * |
||
| 254 | * @return mixed |
||
| 255 | */ |
||
| 256 | public function __get($key) |
||
| 257 | { |
||
| 258 | return $this->get($key); |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Whether or not an data exists by key. |
||
| 263 | * |
||
| 264 | * @param string $key |
||
| 265 | * |
||
| 266 | * @return bool |
||
| 267 | */ |
||
| 268 | public function __isset($key) |
||
| 269 | { |
||
| 270 | return $this->has($key); |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Unsets an data by key. |
||
| 275 | * |
||
| 276 | * @param string $key |
||
| 277 | */ |
||
| 278 | public function __unset($key) |
||
| 279 | { |
||
| 280 | $this->forget($key); |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * var_export. |
||
| 285 | * |
||
| 286 | * @return array |
||
| 287 | */ |
||
| 288 | public function __set_state() |
||
| 289 | { |
||
| 290 | return $this->all(); |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * (PHP 5 >= 5.0.0)<br/> |
||
| 295 | * Whether a offset exists. |
||
| 296 | * |
||
| 297 | * @see http://php.net/manual/en/arrayaccess.offsetexists.php |
||
| 298 | * |
||
| 299 | * @param mixed $offset <p> |
||
| 300 | * An offset to check for. |
||
| 301 | * </p> |
||
| 302 | * |
||
| 303 | * @return bool true on success or false on failure. |
||
| 304 | * The return value will be casted to boolean if non-boolean was returned |
||
| 305 | */ |
||
| 306 | public function offsetExists($offset) |
||
| 307 | { |
||
| 308 | return $this->has($offset); |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * (PHP 5 >= 5.0.0)<br/> |
||
| 313 | * Offset to unset. |
||
| 314 | * |
||
| 315 | * @see http://php.net/manual/en/arrayaccess.offsetunset.php |
||
| 316 | * |
||
| 317 | * @param mixed $offset <p> |
||
| 318 | * The offset to unset. |
||
| 319 | * </p> |
||
| 320 | */ |
||
| 321 | public function offsetUnset($offset) |
||
| 322 | { |
||
| 323 | if ($this->offsetExists($offset)) { |
||
| 324 | $this->forget($offset); |
||
| 325 | } |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * (PHP 5 >= 5.0.0)<br/> |
||
| 330 | * Offset to retrieve. |
||
| 331 | * |
||
| 332 | * @see http://php.net/manual/en/arrayaccess.offsetget.php |
||
| 333 | * |
||
| 334 | * @param mixed $offset <p> |
||
| 335 | * The offset to retrieve. |
||
| 336 | * </p> |
||
| 337 | * |
||
| 338 | * @return mixed Can return all value types |
||
| 339 | */ |
||
| 340 | public function offsetGet($offset) |
||
| 341 | { |
||
| 342 | return $this->offsetExists($offset) ? $this->get($offset) : null; |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * (PHP 5 >= 5.0.0)<br/> |
||
| 347 | * Offset to set. |
||
| 348 | * |
||
| 349 | * @see http://php.net/manual/en/arrayaccess.offsetset.php |
||
| 350 | * |
||
| 351 | * @param mixed $offset <p> |
||
| 352 | * The offset to assign the value to. |
||
| 353 | * </p> |
||
| 354 | * @param mixed $value <p> |
||
| 355 | * The value to set. |
||
| 356 | * </p> |
||
| 357 | */ |
||
| 358 | public function offsetSet($offset, $value) |
||
| 359 | { |
||
| 360 | $this->set($offset, $value); |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Results array of items from Collection or Arrayable. |
||
| 365 | * |
||
| 366 | * @param mixed $items |
||
| 367 | * @return array |
||
| 368 | */ |
||
| 369 | protected function getArrayableItems($items) |
||
| 370 | { |
||
| 371 | if ($items instanceof self) { |
||
| 372 | return $items->all(); |
||
| 373 | } elseif ($items instanceof JsonSerializable) { |
||
| 374 | return $items->jsonSerialize(); |
||
| 375 | } elseif (is_string($items)) { |
||
| 376 | try { |
||
| 377 | return Json::decode($items, true); |
||
| 378 | } catch (JsonParseException $e) { |
||
|
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
Loading history...
|
|||
| 379 | } |
||
| 380 | } |
||
| 381 | |||
| 382 | return (array) $items; |
||
| 383 | } |
||
| 384 | } |
||
| 385 |