Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 7 additions & 4 deletions .github/workflows/auto-assign.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
name: Auto assign issues
name: Auto assign issues and pull requests

on:
issues:
types:
- opened
pull_request:
types:
- opened

jobs:
run:
Expand All @@ -12,11 +15,11 @@ jobs:
issues: write
pull-requests: write
steps:
- name: Assign issues
uses: gustavofreze/auto-assign@1.0.0
- name: Assign issues and pull requests
uses: gustavofreze/auto-assign@1.1.4
with:
assignees: '${{ secrets.ASSIGNEES }}'
github_token: '${{ secrets.GITHUB_TOKEN }}'
allow_self_assign: 'true'
allow_no_assignees: 'true'
assignment_options: 'ISSUE'
assignment_options: 'ISSUE,PULL_REQUEST'
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ show-reports:

clean:
@sudo chown -R ${USER}:${USER} ${PWD}
@rm -rf report vendor .phpunit.cache .lock
@rm -rf report vendor .phpunit.cache *.lock
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ namespace Example;

use TinyBlocks\Collection\Collection;
use TinyBlocks\Collection\Order;
use TinyBlocks\Collection\PreserveKeys;
use TinyBlocks\Mapper\KeyPreservation;

$collection = Collection::createFrom(elements: [1, 2, 3, 4, 5])
->add(elements: [6, 7])
->filter(predicates: fn(int $value): bool => $value > 3)
->sort(order: Order::ASCENDING_VALUE)
->map(transformations: fn(int $value): int => $value * 2)
->toArray(preserveKeys: PreserveKeys::DISCARD);
->toArray(keyPreservation: KeyPreservation::DISCARD);

# Output: [8, 10, 12, 14]
```
Expand Down Expand Up @@ -320,9 +320,9 @@ These methods allow the Collection's elements to be transformed or converted int
By default, `PreserveKeys::PRESERVE` is used.

```php
use TinyBlocks\Collection\PreserveKeys;
use TinyBlocks\Mapper\KeyPreservation;

$collection->toArray(preserveKeys: PreserveKeys::DISCARD);
$collection->toArray(preserveKeys: KeyPreservation::DISCARD);
```

#### Convert to JSON
Expand All @@ -337,9 +337,9 @@ These methods allow the Collection's elements to be transformed or converted int
By default, `PreserveKeys::PRESERVE` is used.

```php
use TinyBlocks\Collection\PreserveKeys;
use TinyBlocks\Mapper\KeyPreservation;

$collection->toJson(preserveKeys: PreserveKeys::DISCARD);
$collection->toJson(preserveKeys: KeyPreservation::DISCARD);
```

<div id='faq'></div>
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
},
"require": {
"php": "^8.3",
"tiny-blocks/serializer": "^3"
"tiny-blocks/mapper": "^1.0"
},
"require-dev": {
"phpmd/phpmd": "^2.15",
Expand Down
17 changes: 9 additions & 8 deletions src/Collectible.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Closure;
use Countable;
use IteratorAggregate;
use TinyBlocks\Mapper\KeyPreservation;
use Traversable;

/**
Expand Down Expand Up @@ -224,27 +225,27 @@ public function slice(int $index, int $length = -1): Collectible;
* Converts the Collection to an array.
*
* The key preservation behavior should be provided from the `PreserveKeys` enum:
* - {@see PreserveKeys::PRESERVE}: Preserves the array keys.
* - {@see PreserveKeys::DISCARD}: Discards the array keys.
* - {@see KeyPreservation::PRESERVE}: Preserves the array keys.
* - {@see KeyPreservation::DISCARD}: Discards the array keys.
*
* By default, `PreserveKeys::PRESERVE` is used.
*
* @param PreserveKeys $preserveKeys The option to preserve or discard array keys.
* @param KeyPreservation $keyPreservation The option to preserve or discard array keys.
* @return array<Key, Value> The resulting array.
*/
public function toArray(PreserveKeys $preserveKeys = PreserveKeys::PRESERVE): array;
public function toArray(KeyPreservation $keyPreservation = KeyPreservation::PRESERVE): array;

/**
* Converts the Collection to a JSON string.
*
* The key preservation behavior should be provided from the `PreserveKeys` enum:
* - {@see PreserveKeys::PRESERVE}: Preserves the array keys.
* - {@see PreserveKeys::DISCARD}: Discards the array keys.
* - {@see KeyPreservation::PRESERVE}: Preserves the array keys.
* - {@see KeyPreservation::DISCARD}: Discards the array keys.
*
* By default, `PreserveKeys::PRESERVE` is used.
*
* @param PreserveKeys $preserveKeys The option to preserve or discard array keys.
* @param KeyPreservation $keyPreservation The option to preserve or discard array keys.
* @return string The resulting JSON string.
*/
public function toJson(PreserveKeys $preserveKeys = PreserveKeys::PRESERVE): string;
public function toJson(KeyPreservation $keyPreservation = KeyPreservation::PRESERVE): string;
}
18 changes: 5 additions & 13 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,23 @@
use TinyBlocks\Collection\Internal\Operations\Transform\GroupBy;
use TinyBlocks\Collection\Internal\Operations\Transform\JoinToString;
use TinyBlocks\Collection\Internal\Operations\Transform\Map;
use TinyBlocks\Collection\Internal\Operations\Transform\MapToArray;
use TinyBlocks\Collection\Internal\Operations\Transform\MapToJson;
use TinyBlocks\Collection\Internal\Operations\Write\Add;
use TinyBlocks\Collection\Internal\Operations\Write\Create;
use TinyBlocks\Collection\Internal\Operations\Write\Remove;
use TinyBlocks\Collection\Internal\Operations\Write\RemoveAll;
use TinyBlocks\Mapper\IterableMappability;
use TinyBlocks\Mapper\IterableMapper;
use Traversable;

/**
* Represents a collection that provides a set of utility methods for operations like adding,
* filtering, mapping, and transforming elements. Internally uses iterators to apply operations
* lazily and efficiently.
*/
class Collection implements Collectible
class Collection implements Collectible, IterableMapper
{
use IterableMappability;

private LazyIterator $iterator;

private function __construct(LazyIterator $iterator)
Expand Down Expand Up @@ -169,14 +171,4 @@ public function slice(int $index, int $length = -1): static
)
);
}

public function toArray(PreserveKeys $preserveKeys = PreserveKeys::PRESERVE): array
{
return MapToArray::from(elements: $this->iterator->getIterator(), preserveKeys: $preserveKeys)->toArray();
}

public function toJson(PreserveKeys $preserveKeys = PreserveKeys::PRESERVE): string
{
return MapToJson::from(elements: $this->iterator->getIterator(), preserveKeys: $preserveKeys)->toJson();
}
}
29 changes: 0 additions & 29 deletions src/Internal/Operations/Transform/MapToArray.php

This file was deleted.

29 changes: 0 additions & 29 deletions src/Internal/Operations/Transform/MapToJson.php

This file was deleted.

28 changes: 0 additions & 28 deletions src/PreserveKeys.php

This file was deleted.

2 changes: 1 addition & 1 deletion tests/CollectionPerformanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function testChainedOperationsPerformanceAndMemoryWithCollection(): void
$endMemory = memory_get_usage();

/** @Then verify that the total duration of the chained operations is within limits */
self::assertLessThan(9, $endTime - $startTime);
self::assertLessThan(10, $endTime - $startTime);

/** @And verify that memory usage is within acceptable limits */
$memoryUsageInMB = ($endMemory - $startMemory) / 1024 / 1024;
Expand Down
8 changes: 4 additions & 4 deletions tests/Models/CryptoCurrency.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace TinyBlocks\Collection\Models;

use TinyBlocks\Serializer\Serializer;
use TinyBlocks\Serializer\SerializerAdapter;
use TinyBlocks\Mapper\ObjectMappability;
use TinyBlocks\Mapper\ObjectMapper;

final class CryptoCurrency implements Serializer
final class CryptoCurrency implements ObjectMapper
{
use SerializerAdapter;
use ObjectMappability;

public function __construct(public string $name, public float $price, public string $symbol)
{
Expand Down
8 changes: 4 additions & 4 deletions tests/Models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace TinyBlocks\Collection\Models;

use TinyBlocks\Serializer\Serializer;
use TinyBlocks\Serializer\SerializerAdapter;
use TinyBlocks\Mapper\ObjectMappability;
use TinyBlocks\Mapper\ObjectMapper;

final readonly class Product implements Serializer
final readonly class Product implements ObjectMapper
{
use SerializerAdapter;
use ObjectMappability;

public function __construct(public string $name, public Amount $amount)
{
Expand Down
12 changes: 6 additions & 6 deletions tests/Operations/Filter/CollectionFilterOperationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use PHPUnit\Framework\TestCase;
use TinyBlocks\Collection\Collection;
use TinyBlocks\Collection\Models\CryptoCurrency;
use TinyBlocks\Collection\PreserveKeys;
use TinyBlocks\Mapper\KeyPreservation;

final class CollectionFilterOperationTest extends TestCase
{
Expand All @@ -30,7 +30,7 @@ public function testFilterAppliesMultiplePredicates(): void
);

/** @Then the filtered collection should discard the keys and return the expected values */
self::assertSame([4], $actual->toArray(preserveKeys: PreserveKeys::DISCARD));
self::assertSame([4], $actual->toArray(keyPreservation: KeyPreservation::DISCARD));
}

#[DataProvider('elementsDataProvider')]
Expand All @@ -43,7 +43,7 @@ public function testFilterAppliesDefaultArrayFilter(iterable $elements, iterable
$actual = $collection->filter();

/** @Then the filtered collection should contain only truthy elements */
self::assertSame(array_values((array)$expected), $actual->toArray(preserveKeys: PreserveKeys::DISCARD));
self::assertSame(array_values((array)$expected), $actual->toArray(keyPreservation: KeyPreservation::DISCARD));
}

#[DataProvider('elementsDataProviderWithKeys')]
Expand All @@ -70,12 +70,12 @@ public static function elementsDataProvider(): iterable

yield 'Array with boolean values' => [
'elements' => [false, true, false, true],
'expected' => [1 => true, 3 => true]
'expected' => [true, true]
];

yield 'Array with null and numbers' => [
'elements' => [null, 1, 2, 0],
'expected' => [1 => 1, 2 => 2]
'expected' => [1, 2]
];

yield 'Array with only falsy values' => [
Expand All @@ -85,7 +85,7 @@ public static function elementsDataProvider(): iterable

yield 'Array with objects and truthy values' => [
'elements' => [$bitcoin, 1, 'valid string'],
'expected' => [$bitcoin->toArray(), 1, 'valid string']
'expected' => [$bitcoin->toArray(keyPreservation: KeyPreservation::DISCARD), 1, 'valid string']
];
}

Expand Down
Loading