8
8
[ ![ codecov] ( https://codecov.io/github/WebdevCave/yadic-php/graph/badge.svg?token=6GLECJQG16 )] ( https://codecov.io/github/WebdevCave/yadic-php )
9
9
10
10
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 .
12
12
13
13
``` bash
14
14
composer require webdevcave/yadic
@@ -18,6 +18,8 @@ Alternatively, you can clone the repository or download the source files directl
18
18
19
19
## Usage
20
20
21
+ ### Autowiring
22
+
21
23
``` php
22
24
<?php
23
25
@@ -70,6 +72,120 @@ $container->addAlias(StorageInterface::class, Storage::class);
70
72
var_dump($container->get(MyController::class)->save()); //bool(true)
71
73
```
72
74
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
+
73
189
## Contributing
74
190
75
191
Contributions are welcome! If you find any issues or have suggestions for improvements,
0 commit comments