Skip to content

Commit 41f73fe

Browse files
Merge pull request #7 from WebdevCave/feature/object-hydration
Feature/object hydration
2 parents d780214 + 56551ca commit 41f73fe

File tree

8 files changed

+369
-64
lines changed

8 files changed

+369
-64
lines changed

README.md

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
[![codecov](https://codecov.io/github/WebdevCave/yadic-php/graph/badge.svg?token=6GLECJQG16)](https://codecov.io/github/WebdevCave/yadic-php)
99

1010
This is a simple to use, yet powerful service container that provides a seamless way to automate dependency injection
11-
with auto-wiring.
11+
featuring auto-wiring and object hydration.
1212

1313
```bash
1414
composer require webdevcave/yadic
@@ -18,6 +18,8 @@ Alternatively, you can clone the repository or download the source files directl
1818

1919
## Usage
2020

21+
### Autowiring
22+
2123
```php
2224
<?php
2325

@@ -70,6 +72,120 @@ $container->addAlias(StorageInterface::class, Storage::class);
7072
var_dump($container->get(MyController::class)->save()); //bool(true)
7173
```
7274

75+
### Invoking a method ft. autowiring
76+
77+
```php
78+
$arguments = ['nonInjectableArgument' => 'value']; //optional
79+
$container->invoke([$instance, 'methodName'], $arguments);
80+
```
81+
82+
### Hydration
83+
84+
```php
85+
//Class declarations:
86+
87+
use Webdevcave\Yadic\Annotations\ArrayOf;
88+
89+
class Candidate
90+
{
91+
public function __construct(
92+
public ?string $name = null,
93+
public ?int $age = null,
94+
#[ArrayOf(Skill::class)]
95+
public array $skills = []
96+
) {
97+
}
98+
}
99+
100+
class Skill
101+
{
102+
public function __construct(
103+
public string $title,
104+
) {
105+
}
106+
}
107+
108+
// Hydration example 1:
109+
$data = [
110+
'name' => 'John Doe',
111+
'age' => 25,
112+
'skills' => [
113+
['title' => 'PHP'],
114+
['title' => 'Java'],
115+
['title' => 'Rust'],
116+
['title' => 'React'],
117+
],
118+
];
119+
$instance = $container->hydrate(Candidate::class, $data);
120+
121+
//Results output
122+
/*
123+
print_r($instance);
124+
This test printed output:
125+
Candidate Object
126+
(
127+
[name] => John Doe
128+
[age] => 25
129+
[skills] => Array
130+
(
131+
[0] => Skill Object
132+
(
133+
[title] => PHP
134+
)
135+
136+
[1] => Skill Object
137+
(
138+
[title] => Java
139+
)
140+
141+
[2] => Skill Object
142+
(
143+
[title] => Rust
144+
)
145+
146+
[3] => Skill Object
147+
(
148+
[title] => React
149+
)
150+
151+
)
152+
153+
)
154+
*/
155+
156+
// Hydration example 2:
157+
$data = [
158+
[
159+
'name' => 'Foo',
160+
//...
161+
],
162+
[
163+
'name' => 'Bar',
164+
//...
165+
]
166+
];
167+
$instances = $container->hydrate(Candidate::class, $data);
168+
//Results output
169+
/*
170+
print_r($instances);
171+
This test printed output:
172+
Array
173+
(
174+
[0] => Candidate Object
175+
(
176+
[name] => Foo
177+
//...
178+
)
179+
180+
[1] => Candidate Object
181+
(
182+
[name] => Bar
183+
//...
184+
)
185+
)
186+
*/
187+
```
188+
73189
## Contributing
74190

75191
Contributions are welcome! If you find any issues or have suggestions for improvements,

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"autowiring",
1010
"repository",
1111
"container",
12-
"service-container"
12+
"service-container",
13+
"hydration"
1314
],
1415
"autoload": {
1516
"psr-4": {

composer.lock

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

src/Annotations/ArrayOf.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Webdevcave\Yadic\Annotations;
4+
5+
use Attribute;
6+
7+
#[Attribute(Attribute::TARGET_PARAMETER | Attribute::TARGET_PROPERTY)]
8+
class ArrayOf
9+
{
10+
public function __construct(
11+
public string $target,
12+
) {
13+
}
14+
}

0 commit comments

Comments
 (0)