Skip to content

Commit

Permalink
Merge pull request #7 from WebdevCave/feature/object-hydration
Browse files Browse the repository at this point in the history
Feature/object hydration
  • Loading branch information
carloscarucce authored Jul 6, 2024
2 parents d780214 + 56551ca commit 41f73fe
Show file tree
Hide file tree
Showing 8 changed files with 369 additions and 64 deletions.
118 changes: 117 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[![codecov](https://codecov.io/github/WebdevCave/yadic-php/graph/badge.svg?token=6GLECJQG16)](https://codecov.io/github/WebdevCave/yadic-php)

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

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

## Usage

### Autowiring

```php
<?php

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

### Invoking a method ft. autowiring

```php
$arguments = ['nonInjectableArgument' => 'value']; //optional
$container->invoke([$instance, 'methodName'], $arguments);
```

### Hydration

```php
//Class declarations:

use Webdevcave\Yadic\Annotations\ArrayOf;

class Candidate
{
public function __construct(
public ?string $name = null,
public ?int $age = null,
#[ArrayOf(Skill::class)]
public array $skills = []
) {
}
}

class Skill
{
public function __construct(
public string $title,
) {
}
}

// Hydration example 1:
$data = [
'name' => 'John Doe',
'age' => 25,
'skills' => [
['title' => 'PHP'],
['title' => 'Java'],
['title' => 'Rust'],
['title' => 'React'],
],
];
$instance = $container->hydrate(Candidate::class, $data);

//Results output
/*
print_r($instance);
This test printed output:
Candidate Object
(
[name] => John Doe
[age] => 25
[skills] => Array
(
[0] => Skill Object
(
[title] => PHP
)

[1] => Skill Object
(
[title] => Java
)

[2] => Skill Object
(
[title] => Rust
)

[3] => Skill Object
(
[title] => React
)

)

)
*/

// Hydration example 2:
$data = [
[
'name' => 'Foo',
//...
],
[
'name' => 'Bar',
//...
]
];
$instances = $container->hydrate(Candidate::class, $data);
//Results output
/*
print_r($instances);
This test printed output:
Array
(
[0] => Candidate Object
(
[name] => Foo
//...
)

[1] => Candidate Object
(
[name] => Bar
//...
)
)
*/
```

## Contributing

Contributions are welcome! If you find any issues or have suggestions for improvements,
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"autowiring",
"repository",
"container",
"service-container"
"service-container",
"hydration"
],
"autoload": {
"psr-4": {
Expand Down
40 changes: 20 additions & 20 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions src/Annotations/ArrayOf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Webdevcave\Yadic\Annotations;

use Attribute;

#[Attribute(Attribute::TARGET_PARAMETER | Attribute::TARGET_PROPERTY)]
class ArrayOf
{
public function __construct(
public string $target,
) {
}
}
Loading

0 comments on commit 41f73fe

Please sign in to comment.