Skip to content

Commit 26e2cef

Browse files
committed
Add ability to use without the Laravel framework
1 parent 16a9875 commit 26e2cef

File tree

9 files changed

+208
-43
lines changed

9 files changed

+208
-43
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"pestphp/pest": "^1.0|^2.0|^3.0",
2828
"laravel/pint": "^1.0",
2929
"fakerphp/faker": "^1.0",
30-
"illuminate/support": "^9.0|^10.0|^11.0",
30+
"illuminate/collections": "^9.0|^10.0|^11.0",
3131
"orchestra/testbench": "^7.0|^8.0|^9.0"
3232
},
3333
"config": {

composer.lock

Lines changed: 24 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ class Reservation
128128
protected static function toFactoryInstance(array $attributes): static
129129
{
130130
return new static(
131-
$fluent->name,
132-
$fluent->email,
133-
$fluent->datetime,
131+
$attributes['name'],
132+
$attributes['email'],
133+
$attributes['datetime'],
134134
);
135135
}
136136
}

src/Data.php

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?php
2+
3+
namespace DirectoryTree\Dummy;
4+
5+
use ArrayAccess;
6+
use Illuminate\Support\Arr;
7+
use JsonSerializable;
8+
9+
class Data implements ArrayAccess, JsonSerializable
10+
{
11+
/**
12+
* The data attributes.
13+
*/
14+
protected array $attributes = [];
15+
16+
/**
17+
* Constructor.
18+
*/
19+
public function __construct(iterable $attributes = [])
20+
{
21+
foreach ($attributes as $key => $value) {
22+
$this->attributes[$key] = $value;
23+
}
24+
}
25+
26+
/**
27+
* Get an attribute from the data instance using "dot" notation.
28+
*/
29+
public function get(string $key, mixed $default = null): mixed
30+
{
31+
return data_get($this->attributes, $key, $default);
32+
}
33+
34+
/**
35+
* Get an attribute from the data instance.
36+
*/
37+
public function value(string $key, mixed $default = null): mixed
38+
{
39+
if (array_key_exists($key, $this->attributes)) {
40+
return $this->attributes[$key];
41+
}
42+
43+
return value($default);
44+
}
45+
46+
/**
47+
* Get the value of the given key as a new data instance.
48+
*/
49+
public function scope(string $key, mixed $default = null): static
50+
{
51+
return new static(
52+
(array) $this->get($key, $default)
53+
);
54+
}
55+
56+
/**
57+
* Get the attributes from the data instance.
58+
*
59+
* @param array|mixed|null $keys
60+
*/
61+
public function all(mixed $keys = null): array
62+
{
63+
$data = $this->attributes;
64+
65+
if (! $keys) {
66+
return $data;
67+
}
68+
69+
$results = [];
70+
71+
foreach (is_array($keys) ? $keys : func_get_args() as $key) {
72+
Arr::set($results, $key, Arr::get($data, $key));
73+
}
74+
75+
return $results;
76+
}
77+
78+
/**
79+
* Convert the object into something JSON serializable.
80+
*/
81+
public function jsonSerialize(): array
82+
{
83+
return $this->attributes;
84+
}
85+
86+
/**
87+
* Determine if the given offset exists.
88+
*/
89+
public function offsetExists(mixed $offset): bool
90+
{
91+
return isset($this->attributes[$offset]);
92+
}
93+
94+
/**
95+
* Get the value for a given offset.
96+
*/
97+
public function offsetGet(mixed $offset): mixed
98+
{
99+
return $this->value($offset);
100+
}
101+
102+
/**
103+
* Set the value at the given offset.
104+
*/
105+
public function offsetSet(mixed $offset, mixed $value): void
106+
{
107+
$this->attributes[$offset] = $value;
108+
}
109+
110+
/**
111+
* Unset the value at the given offset.
112+
*/
113+
public function offsetUnset(mixed $offset): void
114+
{
115+
unset($this->attributes[$offset]);
116+
}
117+
118+
/**
119+
* Handle dynamic calls to the data instance to set attributes.
120+
*/
121+
public function __call(string $method, array $parameters): static
122+
{
123+
$this->attributes[$method] = count($parameters) > 0 ? reset($parameters) : true;
124+
125+
return $this;
126+
}
127+
128+
/**
129+
* Dynamically retrieve the value of an attribute.
130+
*/
131+
public function __get(string $key): mixed
132+
{
133+
return $this->value($key);
134+
}
135+
136+
/**
137+
* Dynamically set the value of an attribute.
138+
*/
139+
public function __set(string $key, mixed $value): void
140+
{
141+
$this->offsetSet($key, $value);
142+
}
143+
144+
/**
145+
* Dynamically check if an attribute is set.
146+
*/
147+
public function __isset(string $key): bool
148+
{
149+
return $this->offsetExists($key);
150+
}
151+
152+
/**
153+
* Dynamically unset an attribute.
154+
*/
155+
public function __unset(string $key): void
156+
{
157+
$this->offsetUnset($key);
158+
}
159+
}

src/Factory.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
namespace DirectoryTree\Dummy;
44

55
use Closure;
6+
use Faker\Factory as FakerFactory;
67
use Faker\Generator;
78
use Illuminate\Container\Container;
89
use Illuminate\Support\Collection;
9-
use Illuminate\Support\Fluent;
1010
use Illuminate\Support\Traits\Conditionable;
1111

1212
class Factory
@@ -59,7 +59,7 @@ protected function definition(): array
5959
*/
6060
protected function generate(array $attributes): mixed
6161
{
62-
return new Fluent($attributes);
62+
return new Data($attributes);
6363
}
6464

6565
/**
@@ -286,6 +286,10 @@ public function faker(): Generator
286286
*/
287287
protected function newFaker(): Generator
288288
{
289-
return Container::getInstance()->make(Generator::class);
289+
if (class_exists(Container::class)) {
290+
return Container::getInstance()->make(Generator::class);
291+
}
292+
293+
return FakerFactory::create();
290294
}
291295
}

tests/FactoryTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
<?php
22

3+
use DirectoryTree\Dummy\Data;
34
use DirectoryTree\Dummy\Tests\Fixtures\FactoryClassStub;
45
use DirectoryTree\Dummy\Tests\Fixtures\FactoryStub;
56
use DirectoryTree\Dummy\Tests\Fixtures\FactoryWithConfigurationStub;
67
use DirectoryTree\Dummy\Tests\Fixtures\FactoryWithCustomClassStub;
78
use DirectoryTree\Dummy\Tests\Fixtures\FactoryWithStateStub;
89
use Illuminate\Support\Collection;
9-
use Illuminate\Support\Fluent;
1010

1111
it('can generate single instance', function () {
1212
$instance = FactoryStub::new()->make();
1313

14-
expect($instance)->toBeInstanceOf(Fluent::class);
14+
expect($instance)->toBeInstanceOf(Data::class);
1515
});
1616

1717
it('can generate multiple instances in a collection', function () {
1818
$collection = FactoryStub::new()->count(5)->make();
1919

2020
$collection->each(
21-
fn ($instance) => expect($instance)->toBeInstanceOf(Fluent::class)
21+
fn ($instance) => expect($instance)->toBeInstanceOf(Data::class)
2222
);
2323

2424
expect($collection)->toHaveCount(5);

tests/Fixtures/FactoryWithConfigurationStub.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace DirectoryTree\Dummy\Tests\Fixtures;
44

5+
use DirectoryTree\Dummy\Data;
56
use DirectoryTree\Dummy\Factory;
6-
use Illuminate\Support\Fluent;
77

88
class FactoryWithConfigurationStub extends Factory
99
{
@@ -17,8 +17,8 @@ protected function definition(): array
1717

1818
protected function configure(): static
1919
{
20-
return $this->afterMaking(function (Fluent $fluent) {
21-
$fluent->name = 'Custom';
20+
return $this->afterMaking(function (Data $data) {
21+
$data->name = 'Custom';
2222
});
2323
}
2424
}

0 commit comments

Comments
 (0)