first commit

This commit is contained in:
2024-07-15 12:33:27 +02:00
commit ce50ae282b
22084 changed files with 2623791 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
# Create a release when a version tag is pushed
name: Create Release
on:
push:
tags:
- 'v*'
jobs:
build:
name: Create Release
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v1
- name: Create Release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
release_name: Release ${{ github.ref }}
body: For changes in this release please see https://github.com/phootwork/phootwork/releases/tag/${{ github.ref }}
draft: false
prerelease: false

View File

@@ -0,0 +1,3 @@
composer.phar
composer.lock
vendor/

View File

@@ -0,0 +1,61 @@
<?php declare(strict_types=1);
/**
* This file is part of the Phootwork package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
* @copyright Thomas Gossmann
*/
namespace phootwork\collection;
use phootwork\lang\AbstractArray;
/**
* AbstractCollection providing implementation for the Collection interface.
*
* @author Thomas Gossmann
*/
abstract class AbstractCollection extends AbstractArray implements Collection {
/**
* Remove all elements from the collection.
*/
public function clear(): void {
$this->array = [];
}
/**
* @internal
*/
public function rewind(): void {
reset($this->array);
}
/**
* @internal
*/
public function current(): mixed {
return current($this->array);
}
/**
* @internal
*/
public function key(): int|string|null {
return key($this->array);
}
/**
* @internal
*/
public function next(): void {
next($this->array);
}
/**
* @internal
*/
public function valid(): bool {
return key($this->array) !== null;
}
}

View File

@@ -0,0 +1,43 @@
<?php declare(strict_types=1);
/**
* This file is part of the Phootwork package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
* @copyright Thomas Gossmann
*/
namespace phootwork\collection;
use phootwork\lang\Comparator;
use phootwork\lang\parts\EachPart;
use phootwork\lang\parts\ReducePart;
use phootwork\lang\parts\RemovePart;
use phootwork\lang\parts\ReversePart;
/**
* Abstract class for all list-like collections
*
* @author Thomas Gossmann
* @author Cristiano Cinotti
*/
abstract class AbstractList extends AbstractCollection {
use EachPart;
use ReducePart;
use RemovePart;
use ReversePart;
/**
* Sorts the collection in reverse order
*
* @see #sort
* @see #reverse
*
* @param Comparator|callable|null $cmp
*
* @return $this
*/
public function reverseSort(Comparator|callable|null $cmp = null): self {
return $this->sort($cmp)->reverse();
}
}

View File

@@ -0,0 +1,136 @@
<?php declare(strict_types=1);
/**
* This file is part of the Phootwork package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
* @copyright Thomas Gossmann
*/
namespace phootwork\collection;
use Iterator;
use phootwork\lang\parts\AccessorsPart;
use phootwork\lang\parts\AddPart;
use phootwork\lang\parts\IndexFindersPart;
use phootwork\lang\parts\InsertPart;
/**
* Represents a List
*
* @author Thomas Gossmann
* @author Cristiano Cinotti
*/
class ArrayList extends AbstractList {
use AccessorsPart {
get as traitGet;
}
use AddPart;
use IndexFindersPart {
indexOf as traitIndexOf;
findLastIndex as traitFindLastIndex;
findIndex as traitFindIndex;
}
use InsertPart;
/**
* Creates a new ArrayList
*
* @param array|Iterator $collection
*/
public function __construct(array|Iterator $collection = []) {
/** @var mixed $element */
foreach ($collection as $element) {
$this->add($element);
}
}
/**
* Removes an element from the list by its index.
*
* @param int $index
*
* @return ArrayList
*/
public function removeByIndex(int $index): self {
if (isset($this->array[$index])) {
unset($this->array[$index]);
if (!array_is_list($this->array)) {
$this->array = array_values($this->array);
}
}
return $this;
}
/**
* Returns the element at the given index (or null if the index isn't present)
*
* @param int $index
*
* @return mixed
*/
public function get(int $index): mixed {
return $this->traitGet($index);
}
/**
* Returns the index of the given element or null if the element can't be found
*
* @param mixed $element
*
* @return int|null the index for the given element
*/
public function indexOf(mixed $element): ?int {
$index = $this->traitIndexOf($element);
return $index === null ? $index : (int) $index;
}
/**
* Searches the array with a given callback and returns the index for the last element if found.
*
* The callback function takes one or two parameters:
*
* function ($element [, $query]) {}
*
* The callback must return a boolean
* When it's passed, $query must be the first argument:
*
* - find($query, callback)
* - find(callback)
*
* @param array $arguments
*
* @return int|null the index or null if it hasn't been found
*/
public function findLastIndex(mixed ...$arguments): ?int {
$lastIndex = $this->traitFindLastIndex(...$arguments);
return $lastIndex === null ? $lastIndex : (int) $lastIndex;
}
/**
* Searches the array with a given callback and returns the index for the first element if found.
*
* The callback function takes one or two parameters:
*
* function ($element [, $query]) {}
*
* The callback must return a boolean
* When it's passed, $query must be the first argument:
*
* - find($query, callback)
* - find(callback)
*
* @param array $arguments
*
* @return int|null the index or null if it hasn't been found
*/
public function findIndex(mixed ...$arguments): ?int {
$index = $this->traitFindIndex(...$arguments);
return $index === null ? $index : (int) $index;
}
}

View File

@@ -0,0 +1,54 @@
<?php declare(strict_types=1);
/**
* This file is part of the Phootwork package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
* @copyright Thomas Gossmann
*/
namespace phootwork\collection;
/**
* Collection interface
*
* @author Thomas Gossmann
*/
interface Collection extends \Iterator {
/**
* Resets the collection
*
* @return void
*/
public function clear(): void;
/**
* Checks whether this collection is empty
*
* @return bool
*/
public function isEmpty(): bool;
/**
* Checks whether the given element is in this collection
*
* @param mixed $element
*
* @return bool
*/
public function contains(mixed $element): bool;
/**
* Returns the amount of elements in this collection
*
* @return int
*/
public function size(): int;
/**
* Returns the collection as an array
*
* @return array
*/
public function toArray(): array;
}

View File

@@ -0,0 +1,135 @@
<?php declare(strict_types=1);
/**
* This file is part of the Phootwork package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
* @copyright Thomas Gossmann
*/
namespace phootwork\collection;
use Iterator;
use stdClass;
/**
* CollectionUtils help to transform data recursively into collections.
*
* It must be mentioned the API is experimental and may change. Please
* report to the issue tracker.
*/
class CollectionUtils {
/**
* Returns a proper collection for the given array (also transforms nested collections)
* (experimental API)
*
* @param array|Iterator $collection
*
* @return Map|ArrayList the collection
*
* @psalm-suppress MixedReturnStatement `self::toCollection()` returns a collection if the input
* is an array or Iterator
* @psalm-suppress MixedInferredReturnType
*/
public static function fromCollection(array|Iterator $collection): Map|ArrayList {
return self::toCollection($collection);
}
/**
* @param mixed $data
*
* @return mixed
*/
private static function toCollection(mixed $data): mixed {
// prepare normal array
if (!($data instanceof Iterator)) {
/** @var mixed $data */
$data = json_decode(json_encode($data));
}
// check if we can transform it into a collection or just return as is
if (!(is_array($data) || $data instanceof Iterator || $data instanceof stdClass)) {
return $data;
}
// check we have a list
if (is_array($data) || $data instanceof AbstractList) {
return self::toList($data);
}
// everything else must be a map
return self::toMap($data);
}
/**
* Recursively transforms data into a map (on the first level, deeper levels
* transformed to an appropriate collection) (experimental API)
*
* @param array|Iterator|stdClass $collection
*
* @return Map
*/
public static function toMap(Iterator|array|stdClass $collection): Map {
if ($collection instanceof stdClass) {
/** @var array $collection */
$collection = json_decode(json_encode($collection), true);
}
$map = new Map();
/**
* @var string $k
* @var string $v
*/
foreach ($collection as $k => $v) {
$map->set($k, self::toCollection($v));
}
return $map;
}
/**
* Recursively transforms data into a list (on the first level, deeper levels
* transformed to an appropriate collection) (experimental API)
*
* @param array|Iterator $collection
*
* @return ArrayList
*/
public static function toList(Iterator|array $collection): ArrayList {
$list = new ArrayList();
/** @var mixed $v */
foreach ($collection as $v) {
$list->add(self::toCollection($v));
}
return $list;
}
/**
* Recursively exports a collection to an array
*
* @param mixed $collection
*
* @return array
*
* @psalm-suppress MixedAssignment
*/
public static function toArrayRecursive(mixed $collection): array {
$arr = $collection;
if (is_object($collection) && method_exists($collection, 'toArray')) {
$arr = $collection->toArray();
}
/** @var array $arr */
return array_map(
function (mixed $v): mixed {
if (is_object($v) && method_exists($v, 'toArray')) {
return static::toArrayRecursive($v);
}
return $v;
},
$arr
);
}
}

22
vendor/phootwork/collection/LICENSE vendored Normal file
View File

@@ -0,0 +1,22 @@
The MIT License (MIT)
Copyright (c) 2015 - 2021 Thomas Gossmann
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

268
vendor/phootwork/collection/Map.php vendored Normal file
View File

@@ -0,0 +1,268 @@
<?php declare(strict_types=1);
/**
* This file is part of the Phootwork package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
* @copyright Thomas Gossmann
*/
namespace phootwork\collection;
use Iterator;
use phootwork\lang\AbstractArray;
use phootwork\lang\Comparator;
use phootwork\lang\parts\SortAssocPart;
use phootwork\lang\Text;
/**
* Represents a Map
*
* @author Thomas Gossmann
*/
class Map extends AbstractCollection implements \ArrayAccess {
use SortAssocPart;
/**
* Creates a new Map
*
* @param array|Iterator $collection
*/
public function __construct(array|Iterator $collection = []) {
$this->setAll($collection);
}
/**
* Sets an element with the given key on that map
*
* @param string|int|Text $key
* @param mixed $element
*
* @return Map $this
*/
public function set(string|int|Text $key, mixed $element): self {
$key = is_int($key) ? $key : (string) $key;
$this->array[$key] = $element;
return $this;
}
/**
* Returns the element for the given key
*
* @param string|int|Text $key
*
* @return mixed
*/
public function get(string|int|Text $key): mixed {
$key = is_int($key) ? $key : (string) $key;
return $this->array[$key] ?? null;
}
/**
* Returns the key for the given value or null if not found
*
* @param mixed $value the value
*
* @return string|null
*/
public function getKey(mixed $value): ?string {
/**
* @var string $k
* @var mixed $v
*/
foreach ($this->array as $k => $v) {
if ($v === $value) {
return $k;
}
}
return null;
}
/**
* Sets many elements on that map
*
* @param array|Iterator $collection
*
* @return Map $this
*/
public function setAll(array|Iterator $collection): self {
/**
* @var string $key
* @var mixed $element
*/
foreach ($collection as $key => $element) {
$this->set($key, $element);
}
return $this;
}
/**
* Removes and returns an element from the map by the given key. Returns null if the key
* does not exist.
*
* @param string|Text $key
*
* @return $this
*/
public function remove(string|Text $key): self {
if (isset($this->array[(string) $key])) {
unset($this->array[(string) $key]);
}
return $this;
}
/**
* Returns all keys as Set
*
* @return Set the map's keys
*/
public function keys(): Set {
return new Set(array_keys($this->array));
}
/**
* Returns all values as ArrayList
*
* @return ArrayList the map's values
*/
public function values(): ArrayList {
return new ArrayList(array_values($this->array));
}
/**
* Returns whether the key exist.
*
* @param string|Text $key
*
* @return bool
*/
public function has(string|Text $key): bool {
return isset($this->array[(string) $key]);
}
/**
* Sorts the map
*
* @param Comparator|callable|null $cmp
*
* @return $this
*/
public function sort(Comparator|callable|null $cmp = null): AbstractArray {
return $this->sortAssoc($cmp);
}
/**
* Iterates the map and calls the callback function with the current key and value as parameters
*
* @param callable $callback
*/
public function each(callable $callback): void {
/**
* @var string $key
* @var mixed $value
*/
foreach ($this->array as $key => $value) {
$callback($key, $value);
}
}
/**
* Searches the collection with a given callback and returns the key for the first element if found.
*
* The callback function takes one or two parameters:
*
* function ($element [, $query]) {}
*
* The callback must return a boolean
* When it's passed, $query must be the first argument:
*
* - find($query, callback)
* - find(callback)
*
* @param array $arguments
*
* @return mixed|null the key or null if it hasn't been found
*/
public function findKey(mixed ...$arguments) {
/** @var mixed $index */
$index = count($arguments) === 1 ? $this->find($arguments[0]) : $this->find($arguments[0], $arguments[1]);
return $this->getKey($index);
}
/**
* Searches the collection with a given callback and returns the key for the last element if found.
*
* The callback function takes one or two parameters:
*
* function ($element [, $query]) {}
*
* The callback must return a boolean
* When it's passed, $query must be the first argument:
*
* - find($query, callback)
* - find(callback)
*
* @param array $arguments
*
* @return mixed|null the key or null if it hasn't been found
*/
public function findLastKey(mixed ...$arguments) {
/** @var mixed $index */
$index = count($arguments) === 1 ? $this->findLast($arguments[0]) : $this->findLast($arguments[0], $arguments[1]);
return $this->getKey($index);
}
/**
* @param mixed $offset
* @param mixed $value
*
* @internal
*/
public function offsetSet(mixed $offset, mixed $value): void {
/** @var string|null $offset */
if ($offset !== null) {
$this->array[$offset] = $value;
}
}
/**
* @param mixed $offset
*
* @return bool
*
* @internal
*/
public function offsetExists(mixed $offset): bool {
/** @var string $offset */
return isset($this->array[$offset]);
}
/**
* @param mixed $offset
*
* @internal
*/
public function offsetUnset(mixed $offset): void {
/** @var string $offset */
unset($this->array[$offset]);
}
/**
* @param mixed $offset
*
* @return mixed
*
* @internal
*/
public function offsetGet(mixed $offset): mixed {
/** @var string $offset */
return isset($this->array[$offset]) ? $this->array[$offset] : null;
}
}

65
vendor/phootwork/collection/Queue.php vendored Normal file
View File

@@ -0,0 +1,65 @@
<?php declare(strict_types=1);
/**
* This file is part of the Phootwork package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
* @copyright Thomas Gossmann
*/
namespace phootwork\collection;
use Iterator;
/**
* Represents a Queue
*
* FIFO - first in first out
*
* @author Thomas Gossmann
*/
class Queue extends AbstractList {
/**
* Creates a new Queue
*
* @param array|Iterator $collection
*/
public function __construct(array|Iterator $collection = []) {
$this->enqueue(...$collection);
}
/**
* Enqueues an element
*
* @param mixed ...$elements
*
* @return $this
*/
public function enqueue(mixed ...$elements): self {
array_unshift($this->array, ...$elements);
return $this;
}
/**
* Returns the element at the head or null if the queue is empty but doesn't remove that element
*
* @return mixed
*/
public function peek(): mixed {
if ($this->size() > 0) {
return $this->array[0];
}
return null;
}
/**
* Removes and returns the element at the head or null if the is empty
*
* @return mixed
*/
public function poll(): mixed {
return array_shift($this->array);
}
}

59
vendor/phootwork/collection/README.md vendored Normal file
View File

@@ -0,0 +1,59 @@
# PHP Collections library
![Tests](https://github.com/phootwork/phootwork/workflows/Tests/badge.svg)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/phootwork/phootwork/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/phootwork/phootwork/?branch=master)
[![Code Coverage](https://scrutinizer-ci.com/g/phootwork/phootwork/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/phootwork/phootwork/?branch=master)
[![License](https://img.shields.io/github/license/phootwork/collection.svg?style=flat-square)](https://packagist.org/packages/phootwork/collection)
[![Latest Stable Version](https://img.shields.io/packagist/v/phootwork/collection.svg?style=flat-square)](https://packagist.org/packages/phootwork/collection)
[![Total Downloads](https://img.shields.io/packagist/dt/phootwork/collection.svg?style=flat-square&colorB=007ec6)](https://packagist.org/packages/phootwork/collection)
PHP Collections library which contains ArrayList, Set, Map, Queue & Stack.
## Goals
- Provide collections for php
- Inspired by java `java.util.Collection`
- Functional sugar (map, filter, reduce, ...)
## Installation
Installation via composer:
```
composer require phootwork/collection
```
## Documentation
[https://phootwork.github.io/collection](https://phootwork.github.io/collection)
## Running tests
This package is a part of the Phootwork library. In order to run the test suite, you have to download the full library.
```
git clone https://github.com/phootwork/phootwork
```
Then install the dependencies via composer:
```
composer install
```
Now, run the *collection* test suite:
```
vendor/bin/phpunit --testsuite collection
```
If you want to run the whole library tests, simply run:
```
vendor/bin/phpunit
```
## Contact
Report issues at the github [Issue Tracker](https://github.com/phootwork/phootwork/issues).
## Changelog
Refer to [Releases](https://github.com/phootwork/phootwork/releases)

46
vendor/phootwork/collection/Set.php vendored Normal file
View File

@@ -0,0 +1,46 @@
<?php declare(strict_types=1);
/**
* This file is part of the Phootwork package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
* @copyright Thomas Gossmann
*/
namespace phootwork\collection;
use Iterator;
/**
* Represents a Set
*
* @author Thomas Gossmann
*/
class Set extends AbstractList {
/**
* Creates a new Set
*
* @param array|Iterator $collection
*/
public function __construct(array|Iterator $collection = []) {
$this->add(...$collection);
}
/**
* Adds an element to that set
*
* @param mixed ...$elements
*
* @return $this
*/
public function add(mixed ...$elements): self {
/** @var mixed $element */
foreach ($elements as $element) {
if (!in_array($element, $this->array, true)) {
$this->array[$this->size()] = $element;
}
}
return $this;
}
}

59
vendor/phootwork/collection/Stack.php vendored Normal file
View File

@@ -0,0 +1,59 @@
<?php declare(strict_types=1);
/**
* This file is part of the Phootwork package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
* @copyright Thomas Gossmann
*/
namespace phootwork\collection;
use Iterator;
use phootwork\lang\parts\PopPart;
/**
* Represents a Stack
*
* FILO - first in last out
*
* @author Thomas Gossmann
*/
class Stack extends AbstractList {
use PopPart;
/**
* Creates a new ArrayList
*
* @param array|Iterator $collection
*/
public function __construct(array|Iterator $collection = []) {
$this->push(...$collection);
}
/**
* Pushes an element onto the stack
*
* @param mixed ...$elements
*
* @return $this
*/
public function push(mixed ...$elements): self {
array_push($this->array, ...$elements);
return $this;
}
/**
* Returns the element at the head or null if the stack is empty but doesn't remove that element
*
* @return mixed
*/
public function peek(): mixed {
if ($this->size() > 0) {
return $this->array[$this->size() - 1];
}
return null;
}
}

View File

@@ -0,0 +1,37 @@
{
"name" : "phootwork/collection",
"type" : "library",
"description" : "The phootwork library fills gaps in the php language and provides better solutions than the existing ones php offers.",
"authors" : [{
"name" : "Thomas Gossmann",
"homepage" : "http://gos.si"
}
],
"license" : "MIT",
"keywords" : [
"Text object",
"Array object",
"collection",
"collections",
"list",
"set",
"map",
"queue",
"stack",
"xml",
"json"
],
"support" : {
"issues" : "https://github.com/phootwork/phootwork/issues"
},
"autoload" : {
"psr-4" : {
"phootwork\\collection\\" : ""
}
},
"require" : {
"php" : ">=8.0",
"phootwork/lang" : "^3.0"
},
"homepage" : "https://phootwork.github.io/collection/"
}