Skip to content

Commit be6b6c8

Browse files
committed
Revert "[feature] Allow any type for Story States (#231)"
This reverts commit fb79022.
1 parent 02cd0c8 commit be6b6c8

File tree

1 file changed

+36
-23
lines changed

1 file changed

+36
-23
lines changed

src/Story.php

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
*/
88
abstract class Story
99
{
10-
/** @var array<string, mixed> */
11-
private $state = [];
10+
/** @var array<string, Proxy> */
11+
private $objects = [];
1212

1313
final public function __call(string $method, array $arguments)
1414
{
@@ -31,44 +31,57 @@ final public static function load(): self
3131
/**
3232
* @return static
3333
*/
34-
final public function add(string $name, $state): self
34+
final public function add(string $name, object $object): self
3535
{
3636
trigger_deprecation('zenstruck\foundry', '1.17.0', 'Using Story::add() is deprecated, use Story::addState().');
3737

38-
return $this->addState($name, $state);
38+
return $this->addState($name, $object);
3939
}
4040

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

47-
return $this->state[$name];
47+
return $this->objects[$name];
4848
}
4949

5050
abstract public function build(): void;
5151

52-
final protected function addState(string $name, $state): self
52+
final protected function addState(string $name, object $object): self
5353
{
54-
if (\is_object($state)) {
55-
// ensure factories are persisted
56-
if ($state instanceof Factory) {
57-
$state = $state->create();
58-
}
59-
60-
// ensure objects are proxied
61-
if (!$state instanceof Proxy) {
62-
$state = new Proxy($state);
63-
}
64-
65-
// ensure proxies are persisted
66-
if (!$state->isPersisted()) {
67-
$state->save();
68-
}
54+
// ensure factories are persisted
55+
if ($object instanceof Factory) {
56+
$object = $object->create();
6957
}
7058

71-
$this->state[$name] = $state;
59+
// ensure objects are proxied
60+
if (!$object instanceof Proxy) {
61+
$object = new Proxy($object);
62+
}
63+
64+
// ensure proxies are persisted
65+
if (!$object->isPersisted()) {
66+
$object->save();
67+
}
68+
69+
// ensure factories are persisted
70+
if ($object instanceof Factory) {
71+
$object = $object->create();
72+
}
73+
74+
// ensure objects are proxied
75+
if (!$object instanceof Proxy) {
76+
$object = new Proxy($object);
77+
}
78+
79+
// ensure proxies are persisted
80+
if (!$object->isPersisted()) {
81+
$object->save();
82+
}
83+
84+
$this->objects[$name] = $object;
7285

7386
return $this;
7487
}

0 commit comments

Comments
 (0)