Skip to content

Commit

Permalink
[feature] Allow any type for Story States (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
wouterj authored Dec 29, 2021
1 parent d6d7d52 commit fb79022
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions src/Story.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
*/
abstract class Story
{
/** @var array<string, Proxy> */
private $objects = [];
/** @var array<string, mixed> */
private $state = [];

final public function __call(string $method, array $arguments)
{
Expand All @@ -31,35 +31,37 @@ final public static function load(): self
/**
* @return static
*/
final public function add(string $name, object $object): self
final public function add(string $name, $state): self
{
// ensure factories are persisted
if ($object instanceof Factory) {
$object = $object->create();
}
if (\is_object($state)) {
// ensure factories are persisted
if ($state instanceof Factory) {
$state = $state->create();
}

// ensure objects are proxied
if (!$object instanceof Proxy) {
$object = new Proxy($object);
}
// ensure objects are proxied
if (!$state instanceof Proxy) {
$state = new Proxy($state);
}

// ensure proxies are persisted
if (!$object->isPersisted()) {
$object->save();
// ensure proxies are persisted
if (!$state->isPersisted()) {
$state->save();
}
}

$this->objects[$name] = $object;
$this->state[$name] = $state;

return $this;
}

final public function get(string $name): Proxy
{
if (!\array_key_exists($name, $this->objects)) {
if (!\array_key_exists($name, $this->state)) {
throw new \InvalidArgumentException(\sprintf('"%s" was not registered. Did you forget to call "%s::add()"?', $name, static::class));
}

return $this->objects[$name];
return $this->state[$name];
}

abstract public function build(): void;
Expand Down

0 comments on commit fb79022

Please sign in to comment.