-
Notifications
You must be signed in to change notification settings - Fork 0
Dev #7
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
Merged
Dev #7
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace RunOpenCode\Component\Dataset\Model; | ||
|
|
||
| use RunOpenCode\Component\Dataset\Exception\LogicException; | ||
|
|
||
| /** | ||
| * Buffer of iterated items from collection. | ||
| * | ||
| * @template-covariant TKey | ||
| * @template-covariant TValue | ||
| * | ||
| * @phpstan-type ItemTuple = array{TKey, TValue} | ||
| * | ||
| * @implements \IteratorAggregate<TKey, TValue> | ||
| */ | ||
| final readonly class Buffer implements \IteratorAggregate, \Countable | ||
| { | ||
| /** | ||
| * Create new buffer. | ||
| * | ||
| * @param \ArrayObject<int, ItemTuple> $items Items within buffer. | ||
| * | ||
| * @internal | ||
| */ | ||
| public function __construct(private \ArrayObject $items) | ||
| { | ||
| // noop. | ||
| } | ||
|
|
||
| /** | ||
| * Get first item in buffer. | ||
| * | ||
| * @return Item<TKey, TValue> | ||
| * | ||
| * @phpstan-ignore-next-line generics.variance | ||
| */ | ||
| public function first(): Item | ||
| { | ||
| $first = $this->items[0] ?? throw new LogicException('Buffer is empty.'); | ||
|
|
||
| return new Item($first[0], $first[1]); | ||
| } | ||
|
|
||
| /** | ||
| * Get last item in buffer. | ||
| * | ||
| * @return Item<TKey, TValue> | ||
| * | ||
| * @phpstan-ignore-next-line generics.variance | ||
| */ | ||
| public function last(): Item | ||
| { | ||
| $last = $this->items[\count($this->items) - 1] ?? throw new LogicException('Buffer is empty.'); | ||
|
|
||
| return new Item($last[0], $last[1]); | ||
| } | ||
|
|
||
| /** | ||
| * Get all keys. | ||
| * | ||
| * @return list<TKey> | ||
| */ | ||
| public function keys(): array | ||
| { | ||
| $keys = []; | ||
|
|
||
| foreach ($this->items as [$key]) { | ||
| $keys[] = $key; | ||
| } | ||
|
|
||
| return $keys; | ||
| } | ||
|
|
||
| /** | ||
| * Get all values. | ||
| * | ||
| * @return list<TValue> | ||
| */ | ||
| public function values(): array | ||
| { | ||
| $values = []; | ||
|
|
||
| foreach ($this->items as [, $value]) { | ||
| $values[] = $value; | ||
| } | ||
|
|
||
| return $values; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function getIterator(): \Traversable | ||
| { | ||
| foreach ($this->items as [$key, $value]) { | ||
| yield $key => $value; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function count(): int | ||
| { | ||
| return $this->items->count(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace RunOpenCode\Component\Dataset\Model; | ||
|
|
||
| use RunOpenCode\Component\Dataset\Exception\OutOfBoundsException; | ||
|
|
||
| /** | ||
| * @template TKey | ||
| * @template TValue | ||
| * | ||
| * @implements \ArrayAccess<int, TKey|TValue> | ||
| */ | ||
| final readonly class Item implements \ArrayAccess | ||
| { | ||
| /** | ||
| * @param TKey $key | ||
| * @param TValue $value | ||
| */ | ||
| public function __construct( | ||
| private mixed $key, | ||
| private mixed $value | ||
| ) { | ||
| // noop. | ||
| } | ||
|
|
||
| /** | ||
| * Get key. | ||
| * | ||
| * @return TKey | ||
| */ | ||
| public function key(): mixed | ||
| { | ||
| return $this->key; | ||
| } | ||
|
|
||
| /** | ||
| * Get value. | ||
| * | ||
| * @return TValue | ||
| */ | ||
| public function value(): mixed | ||
| { | ||
| return $this->value; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function offsetExists(mixed $offset): bool | ||
| { | ||
| return $offset === 0 || $offset === 1; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| * | ||
| * @return ($offset is 0 ? TKey : TValue) | ||
| */ | ||
| public function offsetGet(mixed $offset): mixed | ||
| { | ||
| return match ($offset) { | ||
| 0 => $this->key, | ||
| 1 => $this->value, | ||
| default => throw new OutOfBoundsException($offset, $this, \sprintf( | ||
| 'Item tuple does not have offset "%s".', | ||
| $offset | ||
| )), | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function offsetSet(mixed $offset, mixed $value): void | ||
| { | ||
| throw new \BadMethodCallException('Item is immutable.'); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function offsetUnset(mixed $offset): void | ||
| { | ||
| throw new \BadMethodCallException('Item is immutable.'); | ||
| } | ||
| } | ||
This file was deleted.
Oops, something went wrong.
59 changes: 59 additions & 0 deletions
59
src/RunOpenCode/Component/Dataset/src/Operator/BufferCount.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace RunOpenCode\Component\Dataset\Operator; | ||
|
|
||
| use RunOpenCode\Component\Dataset\AbstractStream; | ||
| use RunOpenCode\Component\Dataset\Contract\OperatorInterface; | ||
| use RunOpenCode\Component\Dataset\Model\Buffer; | ||
|
|
||
| /** | ||
| * Buffer count operator. | ||
| * | ||
| * Iterates over given collection and creates a buffer of items with number of items | ||
| * up to given capacity. | ||
| * | ||
| * Yields created instances of {@see Buffer} for batch processing. | ||
| * | ||
| * @template TKey | ||
| * @template TValue | ||
| * | ||
| * @extends AbstractStream<int, Buffer<TKey, TValue>> | ||
| * @implements OperatorInterface<int, Buffer<TKey, TValue>> | ||
| */ | ||
| final class BufferCount extends AbstractStream implements OperatorInterface | ||
| { | ||
| /** | ||
| * @param iterable<TKey, TValue> $collection Collection to iterate over. | ||
| * @param positive-int $count How many items to buffer. | ||
| */ | ||
| public function __construct( | ||
| private readonly iterable $collection, | ||
| private readonly int $count = 1000, | ||
| ) { | ||
| parent::__construct($collection); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| protected function iterate(): \Traversable | ||
| { | ||
| /** @var \ArrayObject<int, array{TKey, TValue}> $items */ | ||
| $items = new \ArrayObject(); | ||
|
|
||
| foreach ($this->collection as $key => $value) { | ||
| $items[] = [$key, $value]; | ||
|
|
||
| if (\count($items) === $this->count) { | ||
| yield new Buffer($items); | ||
| $items = new \ArrayObject(); | ||
| } | ||
| } | ||
|
|
||
| if (\count($items) !== 0) { | ||
| yield new Buffer($items); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.