| 1 |
<?php declare(strict_types=1); |
| 2 |
|
| 3 |
namespace Imagify\Dependencies\League\Container\Inflector; |
| 4 |
|
| 5 |
use Generator; |
| 6 |
use Imagify\Dependencies\League\Container\ContainerAwareTrait; |
| 7 |
|
| 8 |
class InflectorAggregate implements InflectorAggregateInterface |
| 9 |
{ |
| 10 |
use ContainerAwareTrait; |
| 11 |
|
| 12 |
/** |
| 13 |
* @var Inflector[] |
| 14 |
*/ |
| 15 |
protected $inflectors = []; |
| 16 |
|
| 17 |
/** |
| 18 |
* {@inheritdoc} |
| 19 |
*/ |
| 20 |
public function add(string $type, callable $callback = null) : Inflector |
| 21 |
{ |
| 22 |
$inflector = new Inflector($type, $callback); |
| 23 |
$this->inflectors[] = $inflector; |
| 24 |
|
| 25 |
return $inflector; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* {@inheritdoc} |
| 30 |
*/ |
| 31 |
public function getIterator() : Generator |
| 32 |
{ |
| 33 |
$count = count($this->inflectors); |
| 34 |
|
| 35 |
for ($i = 0; $i < $count; $i++) { |
| 36 |
yield $this->inflectors[$i]; |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* {@inheritdoc} |
| 42 |
*/ |
| 43 |
public function inflect($object) |
| 44 |
{ |
| 45 |
foreach ($this->getIterator() as $inflector) { |
| 46 |
$type = $inflector->getType(); |
| 47 |
|
| 48 |
if (! $object instanceof $type) { |
| 49 |
continue; |
| 50 |
} |
| 51 |
|
| 52 |
$inflector->setLeagueContainer($this->getLeagueContainer()); |
| 53 |
$inflector->inflect($object); |
| 54 |
} |
| 55 |
|
| 56 |
return $object; |
| 57 |
} |
| 58 |
} |
| 59 |
|