Skip to content

V0.5 Support for Arr helpers and collections #16

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
merged 8 commits into from
Feb 1, 2025
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
2 changes: 1 addition & 1 deletion .github/workflows/php-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
run: composer run-script ci-test

- name: Upload coverage report
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: coverage-report
path: coverage.xml
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ vendor/
.idea/
coverage.xml
package.xml
clover.xml
coverage-html/
.phpunit/
/.php-cs-fixer.cache
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ final readonly class UserData extends Data
public function __construct(
public string $firstName,
#[Aliases('familyName')]
public stirng $lastName
public string $lastName
) {
$this->fullName = "$this->firstName $this->lastName";
}
Expand Down Expand Up @@ -67,10 +67,6 @@ This package was inspired from the [spatie/data-transfer-object](https://github.
The main thing that I tried to focus on when creating this package is to make it outside of Laravel ecosystem,
meaning: no dependency on [illuminate/support](https://github.com/illuminate/support).

**In no way** I am trying to compare this package with the original one,
Clearly, the original package is more advanced and has more features than this one,
and if you are using Laravel, I highly recommend using the original package instead of this one.

### Requirements

- PHP 8.4 or higher
Expand Down
1,664 changes: 0 additions & 1,664 deletions clover.xml

This file was deleted.

3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
}
},
"scripts": {
"ci-test": "XDEBUG_MODE=coverage vendor/bin/phpunit --testsuite=ci --configuration phpunit.xml",
"ci-test": "XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-text --testsuite=ci --configuration phpunit.xml",
"unit-test": "XDEBUG_MODE=coverage vendor/bin/phpunit --testsuite=unit --configuration phpunit.xml",
"phpstan": "vendor/bin/phpstan analyse --configuration phpstan.neon --memory-limit=256M"
}
}
2 changes: 1 addition & 1 deletion docs/DataConfiguration.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ DataConfiguration::getInstance([
BackedEnumSerializer::class,
ScalarTypeSerializer::class,
]
]
],
])
```
1 change: 0 additions & 1 deletion src/Concerns/BaseData.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Nuxtifyts\PhpDto\Normalizers\Concerns\HasNormalizers;
use Nuxtifyts\PhpDto\Pipelines\DeserializePipeline\DeserializePipeline;
use Nuxtifyts\PhpDto\Pipelines\DeserializePipeline\DeserializePipelinePassable;
use ReflectionClass;
use Throwable;

trait BaseData
Expand Down
2 changes: 1 addition & 1 deletion src/Configuration/DataConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static function getInstance(
serializers: SerializersConfiguration::getInstance(
Arr::getArray($config ?? [], 'serializers'),
$forceCreate
)
),
);
}
}
165 changes: 164 additions & 1 deletion src/Support/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace Nuxtifyts\PhpDto\Support;

use BackedEnum;
use InvalidArgumentException;

final readonly class Arr
{
/**
* @param array<array-key, mixed> $array
* @param string $key
* @param array<array-key, mixed> $default
*
* @return array<array-key, mixed>
Expand All @@ -30,4 +32,165 @@ public static function isArrayOfClassStrings(array $array, string $classString):
&& is_subclass_of($value, $classString)
);
}

/**
* @param array<array-key, mixed> $array
*/
public static function getStringOrNull(array $array, string $key): ?string
{
$value = $array[$key] ?? null;

return is_string($value) ? $value : null;
}

/**
* @param array<array-key, mixed> $array
*/
public static function getString(array $array, string $key, string $default = ''): string
{
return self::getStringOrNull($array, $key) ?? $default;
}

/**
* @param array<array-key, mixed> $array
*/
public static function getIntegerOrNull(array $array, string $key): ?int
{
$value = $array[$key] ?? null;

return is_int($value) ? $value : null;
}

/**
* @param array<array-key, mixed> $array
*/
public static function getInteger(array $array, string $key, int $default = 0): int
{
return self::getIntegerOrNull($array, $key) ?? $default;
}

/**
* @param array<array-key, mixed> $array
*/
public static function getFloatOrNull(array $array, string $key): ?float
{
$value = $array[$key] ?? null;

return is_float($value) ? $value : null;
}

/**
* @param array<array-key, mixed> $array
*/
public static function getFloat(array $array, string $key, float $default = 0.0): float
{
return self::getFloatOrNull($array, $key) ?? $default;
}

/**
* @param array<array-key, mixed> $array
*/
public static function getBooleanOrNull(array $array, string $key): ?bool
{
$value = $array[$key] ?? null;

return is_bool($value) ? $value : null;
}

/**
* @param array<array-key, mixed> $array
*/
public static function getBoolean(array $array, string $key, bool $default = false): bool
{
return self::getBooleanOrNull($array, $key) ?? $default;
}

/**
* @template T of BackedEnum
*
* @param array<array-key, mixed> $array
* @param class-string<T> $enumClass
* @param ?T $default
*
* @return ?T
*/
public static function getBackedEnumOrNull(
array $array,
string $key,
string $enumClass,
?BackedEnum $default = null
): ?BackedEnum {
$value = $array[$key] ?? null;

if ($value instanceof $enumClass) {
return $value;
} else if (
(is_string($value) || is_integer($value))
&& $resolvedValue = $enumClass::tryFrom($value)
) {
return $resolvedValue;
}

return is_null($default)
? null
: ($default instanceof $enumClass
? $default
: throw new InvalidArgumentException('Default value must be an instance of ' . $enumClass)
);
}

/**
* @template T of BackedEnum
*
* @param array<array-key, mixed> $array
* @param class-string<T> $enumClass
* @param T $default
*
* @return T
*/
public static function getBackedEnum(
array $array,
string $key,
string $enumClass,
BackedEnum $default
): BackedEnum {
return self::getBackedEnumOrNull($array, $key, $enumClass, $default) ?? $default;
}


/**
* @param array<array-key, mixed> $array
*
* @return ($preserveKeys is true ? array<array-key, mixed> : list<mixed>)
*/
public static function flatten(array $array, float $depth = INF, bool $preserveKeys = true): array
{
$result = [];

foreach ($array as $key => $item) {
$item = $item instanceof Collection ? $item->all() : $item;

if (! is_array($item)) {
if ($preserveKeys) {
$result[$key] = $item;
} else {
$result[] = $item;
}
} else {
$values = $depth === 1.0
? $item
: self::flatten($item, $depth - 1, $preserveKeys);

foreach ($values as $subKey => $value) {
if ($preserveKeys) {
$result[$subKey] = $value;
} else {
$result[] = $value;
}
}
}
}

return $result;
}
}
117 changes: 117 additions & 0 deletions src/Support/Collection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

namespace Nuxtifyts\PhpDto\Support;

/**
* @template TKey of array-key
* @template TValue of mixed
*/
class Collection
{
/** @var array<TKey, TValue> */
protected array $items = [];

/**
* @param array<TKey, TValue> $items
*/
public function __construct(array $items = [])
{
$this->items = $items;
}

/**
* @param TValue $item
*
* @return self<TKey, TValue>
*/
public function push(mixed $item): self
{
$this->items[] = $item;
return $this;
}

/**
* @param TKey $key
* @param TValue $value
*
* @return self<TKey, TValue>
*/
public function put(mixed $key, mixed $value): self
{
$this->items[$key] = $value;
return $this;
}

/**
* @param ?callable(TValue $item): bool $callable
*
* @return ?TValue
*/
public function first(?callable $callable = null): mixed
{
return is_null($callable)
? reset($this->items) ?: null
: array_find($this->items, $callable);
}

/**
* @template TNewValue of mixed
* @param callable(TValue $item): TNewValue $callable
*
* @return self<TKey, TNewValue>
*/
public function map(callable $callable): self
{
return new self(array_map($callable, $this->items));
}

/**
* @return ($preserveKeys is true ? Collection<array-key, mixed> : Collection<int, mixed>)
*/
public function collapse(bool $preserveKeys = false): self
{
return $this->flatten(1, $preserveKeys);
}

/**
* @return ($preserveKeys is true ? Collection<array-key, mixed> : Collection<int, mixed>)
*/
public function flatten(float $depth = INF, bool $preserveKeys = true): self
{
return new self(Arr::flatten($this->items, $depth, $preserveKeys));
}

public function isNotEmpty(): bool
{
return !empty($this->items);
}

public function isEmpty(): bool
{
return !$this->isNotEmpty();
}

/**
* @param callable(TValue $item): bool $callable
*/
public function every(callable $callable): bool
{
return array_all($this->items, $callable);
}

/**
* @param callable(TValue $item): bool $callable
*/
public function some(callable $callable): bool
{
return array_any($this->items, $callable);
}

/**
* @return array<TKey, TValue>
*/
public function all(): array
{
return $this->items;
}
}
Loading
Loading