Skip to content

Commit

Permalink
feat: update Collection::fromFile constructor (#322)
Browse files Browse the repository at this point in the history
- Remove `length` parameter
- Add `consumer` parameter

BREAKING CHANGE: yes
  • Loading branch information
drupol authored Nov 25, 2023
1 parent 2ec3238 commit bafda9e
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 12 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
],
"require": {
"php": ">= 8.1",
"loophp/iterators": "^2.4"
"loophp/iterators": "^3"
},
"require-dev": {
"ext-pcov": "*",
Expand Down
7 changes: 3 additions & 4 deletions docs/pages/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,10 @@ fromFile

Create a collection from a file.

Signature: ``Collection::fromIterable(string $filepath): Collection;``
Signature: ``Collection::fromFile(string $filepath, ?Closure $consumer = null): Collection;``

.. code-block:: php
Collection::fromFile('http://loripsum.net/api');
.. literalinclude:: code/operations/fromFile.php
:language: php

fromGenerator
~~~~~~~~~~~~~
Expand Down
18 changes: 18 additions & 0 deletions docs/pages/code/operations/fromFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace App;

use loophp\collection\Collection;

include __DIR__ . '/../../../../vendor/autoload.php';

// Get data from a file
Collection::fromFile('/etc/passwd');

// Get data from a URL
Collection::fromFile('http://loripsum.net/api');

// Get data from a CSV file
Collection::fromFile('data.csv', fgetcsv(...));
8 changes: 4 additions & 4 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,14 +358,14 @@ public static function fromCallable(callable $callable, iterable $parameters = [
}

/**
* @param null|non-negative-int $length
* @param Closure(resource): mixed $consumer
*
* @return CollectionInterface<int, string>&self<int, string>
* @return CollectionInterface<int, string|mixed>&self<int, string|mixed>
*/
public static function fromFile(string $filepath, ?int $length = 2): CollectionInterface
public static function fromFile(string $filepath, ?Closure $consumer = null): CollectionInterface
{
return new self(
static fn (): Generator => yield from new ResourceIteratorAggregate(fopen($filepath, 'rb'), true, $length),
static fn (): Generator => yield from new ResourceIteratorAggregate(fopen($filepath, 'rb'), true, $consumer),
);
}

Expand Down
6 changes: 3 additions & 3 deletions src/CollectionDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,11 @@ public static function fromCallable(callable $callable, iterable $parameters = [
}

/**
* @param null|non-negative-int $length
* @param Closure(resource): mixed $consumer
*/
public static function fromFile(string $filepath, ?int $length = 2): static
public static function fromFile(string $filepath, ?Closure $consumer = null): static
{
return new static(Collection::fromFile($filepath, $length));
return new static(Collection::fromFile($filepath, $consumer));
}

public static function fromGenerator(Generator $generator): static
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a,b,c
d,e,f
8 changes: 8 additions & 0 deletions tests/unit/CollectionConstructorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ public function testFromFileConstructor()
['a', 'b', 'c'],
Collection::fromFile(__DIR__ . '/../fixtures/sample.txt')
);

self::assertIdenticalIterable(
[['a', 'b', 'c'], ['d', 'e', 'f']],
Collection::fromFile(
__DIR__ . '/../fixtures/data.csv',
fgetcsv(...)
)
);
}

public function testFromGeneratorConstructor()
Expand Down

0 comments on commit bafda9e

Please sign in to comment.