https://github.com/maximaster/atoa
Convert a value to another value using your own callables.
https://github.com/maximaster/atoa
Last synced: 2 months ago
JSON representation
Convert a value to another value using your own callables.
- Host: GitHub
- URL: https://github.com/maximaster/atoa
- Owner: maximaster
- Created: 2024-09-03T14:31:15.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2024-09-03T14:31:34.000Z (almost 2 years ago)
- Last Synced: 2025-12-14T18:53:23.921Z (8 months ago)
- Language: PHP
- Size: 6.84 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# maximaster/atoa
Convert a value to another value using your own callables.
```bash
composer require maximaster/atoa
```
## Example
```php
namespace Maximaster\Atoa;
use Maximaster\Atoa\Contract\Atoa;
use Maximaster\Atoa\Converter;
class A { public function __construct(public string $value) {} }
class B { public function __construct(public string $value) {} }
// Implement converters for all needed cases.
// Make sure that callable has both input and return types described.
$converter = new Converter([
static fn (A $a): B => new B($a->value),
static fn (B $b): A => new A($b->value),
static fn (string $c): int => intval($c),
]);
// Use Atoa interface instead of implementation in your services.
(static function (Atoa $atoa): void {
// Ask converter to get desired type and pass any other type.
// If the converter does know how to convert this object to desired type, it
// would do it.
$a = $converter->convertTo(A::class, new B('hello')); // A('hello')
$b = $converter->convertTo(B::class, new A('hello')); // B('hello')
$c = $converter->convertTo('int', '42'); // 42
})($converter);
```