88[ ![ codecov] ( https://codecov.io/github/WebdevCave/yadic-php/graph/badge.svg?token=6GLECJQG16 )] ( https://codecov.io/github/WebdevCave/yadic-php )
99
1010This 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
1414composer 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);
7072var_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
75191Contributions are welcome! If you find any issues or have suggestions for improvements,
0 commit comments