Commit version 24.12.13800
This commit is contained in:
@ -1,74 +1,75 @@
|
||||
<?php
|
||||
/**
|
||||
* Flight: An extensible micro-framework.
|
||||
*
|
||||
* @copyright Copyright (c) 2011, Mike Cao <mike@mikecao.com>
|
||||
* @license MIT, http://flightphp.com/license
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace flight\util;
|
||||
|
||||
if (!interface_exists('JsonSerializable')) {
|
||||
require_once dirname(__FILE__) . '/LegacyJsonSerializable.php';
|
||||
}
|
||||
use ArrayAccess;
|
||||
use Countable;
|
||||
use Iterator;
|
||||
use JsonSerializable;
|
||||
|
||||
/**
|
||||
* The Collection class allows you to access a set of data
|
||||
* using both array and object notation.
|
||||
*
|
||||
* @license MIT, http://flightphp.com/license
|
||||
* @copyright Copyright (c) 2011, Mike Cao <mike@mikecao.com>
|
||||
* @implements ArrayAccess<string, mixed>
|
||||
* @implements Iterator<string, mixed>
|
||||
*/
|
||||
class Collection implements \ArrayAccess, \Iterator, \Countable, \JsonSerializable {
|
||||
class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable
|
||||
{
|
||||
/**
|
||||
* Collection data.
|
||||
*
|
||||
* @var array
|
||||
* @var array<string, mixed>
|
||||
*/
|
||||
private $data;
|
||||
private array $data;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param array $data Initial data
|
||||
* @param array<string, mixed> $data Initial data
|
||||
*/
|
||||
public function __construct(array $data = array()) {
|
||||
public function __construct(array $data = [])
|
||||
{
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an item.
|
||||
*
|
||||
* @param string $key Key
|
||||
* @return mixed Value
|
||||
* @return mixed Value if `$key` exists in collection data, otherwise returns `NULL`
|
||||
*/
|
||||
public function __get($key) {
|
||||
return isset($this->data[$key]) ? $this->data[$key] : null;
|
||||
public function __get(string $key)
|
||||
{
|
||||
return $this->data[$key] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an item.
|
||||
*
|
||||
* @param string $key Key
|
||||
* @param mixed $value Value
|
||||
* @param mixed $value Value
|
||||
*/
|
||||
public function __set($key, $value) {
|
||||
public function __set(string $key, $value): void
|
||||
{
|
||||
$this->data[$key] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if an item exists.
|
||||
*
|
||||
* @param string $key Key
|
||||
* @return bool Item status
|
||||
*/
|
||||
public function __isset($key) {
|
||||
public function __isset(string $key): bool
|
||||
{
|
||||
return isset($this->data[$key]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes an item.
|
||||
*
|
||||
* @param string $key Key
|
||||
*/
|
||||
public function __unset($key) {
|
||||
public function __unset(string $key): void
|
||||
{
|
||||
unset($this->data[$key]);
|
||||
}
|
||||
|
||||
@ -76,23 +77,27 @@ class Collection implements \ArrayAccess, \Iterator, \Countable, \JsonSerializab
|
||||
* Gets an item at the offset.
|
||||
*
|
||||
* @param string $offset Offset
|
||||
*
|
||||
* @return mixed Value
|
||||
*/
|
||||
public function offsetGet($offset) {
|
||||
return isset($this->data[$offset]) ? $this->data[$offset] : null;
|
||||
#[\ReturnTypeWillChange]
|
||||
public function offsetGet($offset)
|
||||
{
|
||||
return $this->data[$offset] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets an item at the offset.
|
||||
*
|
||||
* @param string $offset Offset
|
||||
* @param mixed $value Value
|
||||
* @param ?string $offset Offset
|
||||
* @param mixed $value Value
|
||||
*/
|
||||
public function offsetSet($offset, $value) {
|
||||
if (is_null($offset)) {
|
||||
#[\ReturnTypeWillChange]
|
||||
public function offsetSet($offset, $value): void
|
||||
{
|
||||
if ($offset === null) {
|
||||
$this->data[] = $value;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
$this->data[$offset] = $value;
|
||||
}
|
||||
}
|
||||
@ -100,117 +105,119 @@ class Collection implements \ArrayAccess, \Iterator, \Countable, \JsonSerializab
|
||||
/**
|
||||
* Checks if an item exists at the offset.
|
||||
*
|
||||
* @param string $offset Offset
|
||||
* @return bool Item status
|
||||
* @param string $offset
|
||||
*/
|
||||
public function offsetExists($offset) {
|
||||
public function offsetExists($offset): bool
|
||||
{
|
||||
return isset($this->data[$offset]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes an item at the offset.
|
||||
*
|
||||
* @param string $offset Offset
|
||||
* @param string $offset
|
||||
*/
|
||||
public function offsetUnset($offset) {
|
||||
public function offsetUnset($offset): void
|
||||
{
|
||||
unset($this->data[$offset]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the collection.
|
||||
*/
|
||||
public function rewind() {
|
||||
public function rewind(): void
|
||||
{
|
||||
reset($this->data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets current collection item.
|
||||
*
|
||||
* @return mixed Value
|
||||
*/
|
||||
public function current() {
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function current()
|
||||
{
|
||||
return current($this->data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets current collection key.
|
||||
*
|
||||
* @return mixed Value
|
||||
*/
|
||||
public function key() {
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function key()
|
||||
{
|
||||
return key($this->data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the next collection value.
|
||||
*
|
||||
* @return mixed Value
|
||||
*/
|
||||
public function next()
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function next(): void
|
||||
{
|
||||
return next($this->data);
|
||||
next($this->data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Checks if the current collection key is valid.
|
||||
*
|
||||
* @return bool Key status
|
||||
*/
|
||||
public function valid()
|
||||
*/
|
||||
public function valid(): bool
|
||||
{
|
||||
$key = key($this->data);
|
||||
return ($key !== NULL && $key !== FALSE);
|
||||
return key($this->data) !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the size of the collection.
|
||||
*
|
||||
* @return int Collection size
|
||||
*/
|
||||
public function count() {
|
||||
return sizeof($this->data);
|
||||
public function count(): int
|
||||
{
|
||||
return \count($this->data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the item keys.
|
||||
*
|
||||
* @return array Collection keys
|
||||
* @return array<int, string> Collection keys
|
||||
*/
|
||||
public function keys() {
|
||||
public function keys(): array
|
||||
{
|
||||
return array_keys($this->data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the collection data.
|
||||
*
|
||||
* @return array Collection data
|
||||
* @return array<string, mixed> Collection data
|
||||
*/
|
||||
public function getData() {
|
||||
public function getData(): array
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the collection data.
|
||||
*
|
||||
* @param array $data New collection data
|
||||
* @param array<string, mixed> $data New collection data
|
||||
*/
|
||||
public function setData(array $data) {
|
||||
public function setData(array $data): void
|
||||
{
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the collection data which can be serialized to JSON
|
||||
*
|
||||
* @return array Collection data which can be serialized by <b>json_encode</b>
|
||||
*/
|
||||
public function jsonSerialize() {
|
||||
#[\ReturnTypeWillChange]
|
||||
public function jsonSerialize()
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all items from the collection.
|
||||
*/
|
||||
public function clear() {
|
||||
$this->data = array();
|
||||
public function clear(): void
|
||||
{
|
||||
$this->data = [];
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Flight: An extensible micro-framework.
|
||||
*
|
||||
* @copyright Copyright (c) 2011, Mike Cao <mike@mikecao.com>
|
||||
* @license MIT, http://flightphp.com/license
|
||||
*/
|
||||
|
||||
interface JsonSerializable {
|
||||
public function jsonSerialize();
|
||||
}
|
8
libs/flight/util/ReturnTypeWillChange.php
Normal file
8
libs/flight/util/ReturnTypeWillChange.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
// This file is only here so that the PHP8 attribute for doesn't throw an error in files
|
||||
class ReturnTypeWillChange
|
||||
{
|
||||
}
|
Reference in New Issue
Block a user