Skip to content

Feature/object hydration #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading