-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- all value object traits now doesn't perform validations when the method `fromSafeNative()` is called, only assertions are performed - added trait `AppendOnlyValueObjectSetTrait` - removed trait `NativeFactoryMethodTrait`
- Loading branch information
Showing
10 changed files
with
258 additions
and
56 deletions.
There are no files selected for viewing
154 changes: 154 additions & 0 deletions
154
src/ArchitectureBundle/Domain/ValueObject/AppendOnlyValueObjectSetTrait.php
This file contains 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,154 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SixtyEightPublishers\ArchitectureBundle\Domain\ValueObject; | ||
|
||
use SixtyEightPublishers\ArchitectureBundle\Domain\Exception\InvalidNativeValueTypeException; | ||
use SixtyEightPublishers\ArchitectureBundle\Domain\Exception\ValueObjectSetException; | ||
use function assert; | ||
use function count; | ||
use function is_array; | ||
use function is_subclass_of; | ||
use function sprintf; | ||
|
||
trait AppendOnlyValueObjectSetTrait | ||
{ | ||
/** | ||
* @param array<ValueObjectInterface> $items | ||
*/ | ||
protected function __construct( | ||
protected readonly array $items, | ||
) { | ||
} | ||
|
||
public static function empty(): static | ||
{ | ||
return new static([]); | ||
} | ||
|
||
public static function fromNative(mixed $native): static | ||
{ | ||
$itemClassname = self::getValidItemClassname(); | ||
|
||
if (!is_array($native)) { | ||
throw InvalidNativeValueTypeException::fromNativeValue( | ||
$native, | ||
sprintf('array<%s>', $itemClassname), | ||
static::class, | ||
); | ||
} | ||
|
||
$items = []; | ||
|
||
foreach ($native as $item) { | ||
$items[] = ([$itemClassname, 'fromNative'])($item); | ||
} | ||
|
||
return new static($items); | ||
} | ||
|
||
public static function fromSafeNative(mixed $native): static | ||
{ | ||
$itemClassname = self::getValidItemClassname(); | ||
assert(is_array($native)); | ||
|
||
$items = []; | ||
|
||
foreach ($native as $item) { | ||
$items[] = ([$itemClassname, 'fromSafeNative'])($item); | ||
} | ||
|
||
return new static($items); | ||
} | ||
|
||
/** | ||
* @param array<ValueObjectInterface> $items | ||
*/ | ||
public static function fromItems(array $items): static | ||
{ | ||
$itemClassname = self::getValidItemClassname(); | ||
|
||
foreach ($items as $item) { | ||
if (!$item instanceof $itemClassname) { | ||
throw ValueObjectSetException::invalidItemPassed(static::class, $itemClassname, $item); | ||
} | ||
} | ||
|
||
return new static($items); | ||
} | ||
|
||
/** | ||
* @return array<int, mixed> | ||
*/ | ||
public function toNative(): array | ||
{ | ||
$values = []; | ||
|
||
foreach ($this->all() as $item) { | ||
$values[] = $item->toNative(); | ||
} | ||
|
||
return $values; | ||
} | ||
|
||
public function equals(ValueObjectInterface $object): bool | ||
{ | ||
if (!$object instanceof static || $object->count() !== $this->count()) { | ||
return false; | ||
} | ||
|
||
foreach ($object->all() as $index => $item) { | ||
if (!isset($this->items[$index]) || !$this->items[$index]->equals($item)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public function append(ValueObjectInterface $item): static | ||
{ | ||
$items = $this->all(); | ||
$items[] = $item; | ||
|
||
return self::fromItems($items); | ||
} | ||
|
||
/** | ||
* @return array<ValueObjectInterface> | ||
*/ | ||
public function all(): array | ||
{ | ||
return $this->items; | ||
} | ||
|
||
public function count(): int | ||
{ | ||
return count($this->all()); | ||
} | ||
|
||
/** | ||
* @return class-string<ValueObjectInterface> | ||
*/ | ||
abstract protected static function getItemClassname(): string; | ||
|
||
/** | ||
* @return class-string<ValueObjectInterface> | ||
*/ | ||
private static function getValidItemClassname(): string | ||
{ | ||
/** @var class-string $itemClassname */ | ||
$itemClassname = static::getItemClassname(); | ||
|
||
if (empty($itemClassname)) { | ||
throw ValueObjectSetException::undeclaredItemType(static::class); | ||
} | ||
|
||
if (!is_subclass_of($itemClassname, ValueObjectInterface::class, true)) { | ||
throw ValueObjectSetException::declaredItemTypeMustBeValueObjectImplementor(static::class, $itemClassname); | ||
} | ||
|
||
return $itemClassname; | ||
} | ||
} |
This file contains 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 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 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 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
23 changes: 0 additions & 23 deletions
23
src/ArchitectureBundle/Domain/ValueObject/NativeFactoryMethodTrait.php
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.