-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
misc: introduce layer for object builder arguments
- Loading branch information
Showing
15 changed files
with
277 additions
and
58 deletions.
There are no files selected for viewing
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,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Mapper\Object; | ||
|
||
use Countable; | ||
use CuyZ\Valinor\Mapper\Object\Exception\InvalidArgumentIndex; | ||
use IteratorAggregate; | ||
use Traversable; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @implements IteratorAggregate<Argument> | ||
*/ | ||
final class Arguments implements IteratorAggregate, Countable | ||
{ | ||
/** @var Argument[] */ | ||
private array $arguments; | ||
|
||
public function __construct(Argument ...$arguments) | ||
{ | ||
$this->arguments = $arguments; | ||
} | ||
|
||
public function at(int $index): Argument | ||
{ | ||
if ($index >= count($this->arguments)) { | ||
throw new InvalidArgumentIndex($index, $this); | ||
} | ||
|
||
return $this->arguments[$index]; | ||
} | ||
|
||
public function signature(): string | ||
{ | ||
$parameters = array_map( | ||
fn (Argument $argument) => $argument->isRequired() | ||
? "{$argument->name()}: {$argument->type()}" | ||
: "{$argument->name()}?: {$argument->type()}", | ||
$this->arguments | ||
); | ||
|
||
return 'array{' . implode(', ', $parameters) . '}'; | ||
} | ||
|
||
public function count(): int | ||
{ | ||
return count($this->arguments); | ||
} | ||
|
||
/** | ||
* @return Traversable<Argument> | ||
*/ | ||
public function getIterator(): Traversable | ||
{ | ||
yield from $this->arguments; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Mapper\Object\Exception; | ||
|
||
use CuyZ\Valinor\Mapper\Object\Arguments; | ||
use OutOfBoundsException; | ||
|
||
/** @internal */ | ||
final class InvalidArgumentIndex extends OutOfBoundsException | ||
{ | ||
public function __construct(int $index, Arguments $arguments) | ||
{ | ||
$max = $arguments->count() - 1; | ||
|
||
parent::__construct( | ||
"Index $index is out of range, it should be between 0 and $max.", | ||
1648672136 | ||
); | ||
} | ||
} |
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
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
Oops, something went wrong.