From b822c628b5060905bd465ce2e1a804a62ebdf605 Mon Sep 17 00:00:00 2001 From: AlexandruGG Date: Sat, 24 Jul 2021 10:08:05 +0100 Subject: [PATCH 1/2] Introduce helpers for static constructors --- composer.json | 3 ++- spec/loophp/collection/CollectionSpec.php | 15 +++++++++++ src/Utils/helpers.php | 31 +++++++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 src/Utils/helpers.php diff --git a/composer.json b/composer.json index 62ff7ce96..4ab87337f 100644 --- a/composer.json +++ b/composer.json @@ -40,7 +40,8 @@ "autoload": { "psr-4": { "loophp\\collection\\": "./src/" - } + }, + "files": ["src/Utils/helpers.php"] }, "autoload-dev": { "psr-4": { diff --git a/spec/loophp/collection/CollectionSpec.php b/spec/loophp/collection/CollectionSpec.php index 92cda16bc..2fb9a4d2e 100644 --- a/spec/loophp/collection/CollectionSpec.php +++ b/spec/loophp/collection/CollectionSpec.php @@ -29,6 +29,7 @@ use PhpSpec\ObjectBehavior; use stdClass; use function gettype; +use function loophp\collection\Utils\collectIterable; use const INF; use const PHP_EOL; use const PHP_VERSION_ID; @@ -37,6 +38,13 @@ class CollectionSpec extends ObjectBehavior { private const PHP_8 = 80_000; + public function getMatchers(): array + { + return [ + 'beSameAs' => static fn (CollectionInterface $subject, CollectionInterface $expected): bool => $subject->same($expected), + ]; + } + public function it_can_all(): void { $this::fromIterable([1, 2, 3]) @@ -438,6 +446,13 @@ public function it_can_be_constructed_with_an_arrayObject(): void $this->shouldImplement(Collection::class); } + public function it_can_be_constructed_with_collectIterable(): void + { + $input = [1, 2, 3]; + + $this::fromIterable($input)->shouldBeSameAs(collectIterable($input)); + } + public function it_can_be_instantiated_with_withClosure(): void { $fibonacci = static function ($start, $inc) { diff --git a/src/Utils/helpers.php b/src/Utils/helpers.php new file mode 100644 index 000000000..571084fda --- /dev/null +++ b/src/Utils/helpers.php @@ -0,0 +1,31 @@ + $iterable + * + * @return CollectionInterface + */ + function collectIterable(iterable $iterable): CollectionInterface + { + return Collection::fromIterable($iterable); + } +} From 0663e85b94f0880111f329709ef753190ccf9a86 Mon Sep 17 00:00:00 2001 From: AlexandruGG Date: Sat, 24 Jul 2021 10:25:12 +0100 Subject: [PATCH 2/2] Fixes --- composer.json | 4 +++- src/Utils/helpers.php | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 4ab87337f..303314195 100644 --- a/composer.json +++ b/composer.json @@ -41,7 +41,9 @@ "psr-4": { "loophp\\collection\\": "./src/" }, - "files": ["src/Utils/helpers.php"] + "files": [ + "src/Utils/helpers.php" + ] }, "autoload-dev": { "psr-4": { diff --git a/src/Utils/helpers.php b/src/Utils/helpers.php index 571084fda..5c32cacaa 100644 --- a/src/Utils/helpers.php +++ b/src/Utils/helpers.php @@ -11,6 +11,7 @@ use loophp\collection\Collection; use loophp\collection\Contract\Collection as CollectionInterface; + use function function_exists; if (!function_exists('collectIterable')) {