Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Php 8.0 compatibility #37

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
bd744eb
Php 8.0 compatibility.
antonLytkin18 Nov 16, 2021
f95b337
Increase phpunit version to 9.5.
antonLytkin18 Nov 16, 2021
15a8dab
Increase phpunit version to 9.5.
antonLytkin18 Nov 16, 2021
53471ba
Increase phpunit version to 9.5.
antonLytkin18 Nov 16, 2021
3287b1e
Increase package version.
antonLytkin18 Nov 16, 2021
2acd153
Increase php version to 8.0.
antonLytkin18 Nov 16, 2021
a7332d6
Define php version from 7.3 to 8.0.
antonLytkin18 Nov 16, 2021
ab0b42a
Define php version from 7.3 to 8.0.
antonLytkin18 Nov 16, 2021
4b221ce
Define php version from 7.3 to 8.0.
antonLytkin18 Nov 16, 2021
da70a10
Fix Stream::sotBy method working with float values
sokolovsky Dec 16, 2021
fbc8076
Increase fix version for php 8
sokolovsky Dec 16, 2021
c1775b5
Support iterator state pattern
sokolovsky Dec 19, 2021
b7a6dde
Code formatting.
antonLytkin18 Dec 20, 2021
e0eb808
Php 8.0. Increase package version.
antonLytkin18 Dec 20, 2021
c9b355f
Php 8.0. Increase package version.
antonLytkin18 Dec 20, 2021
c2490e1
Merge pull request #39 from worksolutions/fix-iterator-behavior
antonLytkin18 Dec 20, 2021
60a0af2
Fix. Iterator must have rewind method to used as state pattern.
sokolovsky Dec 22, 2021
9444255
Merge pull request #40 from worksolutions/fix-iterator-behavior
antonLytkin18 Dec 22, 2021
30dd666
Fix. Iterator must have rewind method to used as state pattern. Secon…
sokolovsky Dec 22, 2021
5a3faf4
Merge pull request #41 from worksolutions/fix-iterator-behavior
antonLytkin18 Dec 22, 2021
418e373
Who attempts of run rewind methods is not allowed for everything iter…
sokolovsky Dec 22, 2021
aa2206a
Merge pull request #42 from worksolutions/fix-iterator-behavior
antonLytkin18 Dec 22, 2021
74c40d5
Fix chunk with tail elements
sokolovsky Mar 5, 2022
b7c9ac5
Increase version after fixing
sokolovsky Mar 5, 2022
fdc961e
Merge pull request #43 from worksolutions/chunk-reorgonizer-fix
antonLytkin18 Mar 5, 2022
0f2db14
Fix. Sorting with extraction function inside
sokolovsky Oct 20, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: ['7.1', '7.2', '7.3', '7.4']
php-versions: ['7.3', '7.4', '8.0']
name: Testing with PHP ${{ matrix.php-versions }}

steps:
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Collections library for php language",
"minimum-stability": "dev",
"license": "MIT",
"version": "1.0.9",
"version": "1.1.7",
"authors": [
{
"name": "Maxim Sokolovsky",
Expand All @@ -19,11 +19,11 @@
}
],
"require": {
"php": ">=7.1",
"php": "^7.3 || ~8.0",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^7.5"
"phpunit/phpunit": "^9.5"
},
"suggest": {
"phpbench/phpbench": "Uses only for benchmark purposes"
Expand Down
31 changes: 30 additions & 1 deletion src/WS/Utils/Collections/CollectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

namespace WS\Utils\Collections;

use Iterator;
use IteratorAggregate;
use RuntimeException;
use WS\Utils\Collections\Exception\UnsupportedException;

class CollectionFactory
{
Expand Down Expand Up @@ -37,7 +40,7 @@ public static function generate(int $times, ?callable $generator = null): Collec
/**
* Generate collection of int numbers between $from and $to. If $to arg is absent $from - is count of numbers
* @param int $from
* @param int $to
* @param int|null $to
* @return Collection
*/
public static function numbers(int $from, ?int $to = null): Collection
Expand Down Expand Up @@ -68,11 +71,25 @@ public static function fromStrict(array $values): Collection
return new ArrayStrictList($values);
}

/**
* @throws UnsupportedException
*/
public static function fromIterable(iterable $iterable): Collection
{
$list = ArrayList::of();
$count = 0;
$lastItem = null;

foreach ($iterable as $item) {
if ($count <= 1) {
$isObject = is_object($item);
if ($item === $lastItem && $isObject) {
return self::createIterableCollection($iterable);
}
$lastItem = $item;
}
$list->add($item);
$count++;
}

return $list;
Expand All @@ -82,4 +99,16 @@ public static function empty(): Collection
{
return ArrayList::of();
}

private static function createIterableCollection(iterable $iterable): IteratorCollection
{
if ($iterable instanceof IteratorAggregate) {
/** @noinspection PhpUnhandledExceptionInspection */
$iterable = $iterable->getIterator();
}
if (!$iterable instanceof Iterator) {
throw new UnsupportedException('Only Iterator interface can be applied to IteratorCollection');
}
return new IteratorCollection($iterable);
}
}
9 changes: 9 additions & 0 deletions src/WS/Utils/Collections/Exception/UnsupportedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace WS\Utils\Collections\Exception;

use RuntimeException;

class UnsupportedException extends RuntimeException
{
}
25 changes: 8 additions & 17 deletions src/WS/Utils/Collections/Functions/Reorganizers.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,14 @@ public static function random(int $count = 1): Closure
public static function chunk(int $size): Closure
{
return static function (Collection $collection) use ($size): Collection {
$chunkCollection = self::collectionConstructor();
$currentChunk = self::collectionConstructor();
$pointer = $size;
$collection
->stream()
->each(static function ($el) use ($size, $chunkCollection, & $currentChunk, & $pointer) {
$pointer--;
$currentChunk->add($el);

if ($pointer === 0) {
$chunkCollection->add($currentChunk);
$currentChunk = self::collectionConstructor();
$pointer = $size;
}
})
;
return $chunkCollection;
$array = $collection->toArray();
$chunkedArray = array_chunk($array, $size);
$result = self::collectionConstructor();
foreach ($chunkedArray as $items) {
$result->add(self::collectionConstructor($items));
};

return $result;
};
}

Expand Down
135 changes: 135 additions & 0 deletions src/WS/Utils/Collections/IteratorCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

namespace WS\Utils\Collections;

use Iterator;
use WS\Utils\Collections\Exception\UnsupportedException;

/**
* Collection which support Traversable interface
*/
class IteratorCollection implements Collection
{

/**
* @var Iterator
*/
private $iterator;

public function __construct(Iterator $iterator)
{
$this->iterator = $iterator;
}

/**
* @throws UnsupportedException
* @codeCoverageIgnore
*/
public function add($element): bool
{
throw new UnsupportedException();
}

/**
* @throws UnsupportedException
* @codeCoverageIgnore
*/
public function addAll(iterable $elements): bool
{
throw new UnsupportedException();
}

/**
* @throws UnsupportedException
* @codeCoverageIgnore
*/
public function merge(Collection $collection): bool
{
throw new UnsupportedException();
}

/**
* @throws UnsupportedException
* @codeCoverageIgnore
*/
public function clear(): void
{
throw new UnsupportedException();
}

/**
* @throws UnsupportedException
* @codeCoverageIgnore
*/
public function remove($element): bool
{
throw new UnsupportedException();
}

/**
* @throws UnsupportedException
* @codeCoverageIgnore
*/
public function contains($element): bool
{
throw new UnsupportedException();
}

/**
* @throws UnsupportedException
* @codeCoverageIgnore
*/
public function equals(Collection $collection): bool
{
throw new UnsupportedException();
}

public function size(): int
{
$this->iterator->rewind();
$count = 0;
while ($this->iterator->valid()) {
$this->iterator->next();
$count++;
}

return $count;
}

/**
* @codeCoverageIgnore
* @return bool
*/
public function isEmpty(): bool
{
return $this->size() === 0;
}

public function stream(): Stream
{
return new IteratorStream($this);
}

/**
* @codeCoverageIgnore
* @return array
*/
public function toArray(): array
{
throw new UnsupportedException();
}

/**
* @codeCoverageIgnore
* @return Collection
*/
public function copy(): Collection
{
throw new UnsupportedException();
}

public function getIterator()
{
return $this->iterator;
}
}
Loading