3 namespace League\Container\Inflector;
5 use League\Container\ImmutableContainerAwareTrait;
6 use League\Container\Argument\ArgumentResolverInterface;
7 use League\Container\Argument\ArgumentResolverTrait;
9 class Inflector implements ArgumentResolverInterface
11 use ArgumentResolverTrait;
12 use ImmutableContainerAwareTrait;
17 protected $methods = [];
22 protected $properties = [];
25 * Defines a method to be invoked on the subject object.
31 public function invokeMethod($name, array $args)
33 $this->methods[$name] = $args;
39 * Defines multiple methods to be invoked on the subject object.
41 * @param array $methods
44 public function invokeMethods(array $methods)
46 foreach ($methods as $name => $args) {
47 $this->invokeMethod($name, $args);
54 * Defines a property to be set on the subject object.
56 * @param string $property
60 public function setProperty($property, $value)
62 $this->properties[$property] = $value;
68 * Defines multiple properties to be set on the subject object.
70 * @param array $properties
73 public function setProperties(array $properties)
75 foreach ($properties as $property => $value) {
76 $this->setProperty($property, $value);
83 * Apply inflections to an object.
85 * @param object $object
88 public function inflect($object)
90 $properties = $this->resolveArguments(array_values($this->properties));
91 $properties = array_combine(array_keys($this->properties), $properties);
93 foreach ($properties as $property => $value) {
94 $object->{$property} = $value;
97 foreach ($this->methods as $method => $args) {
98 $args = $this->resolveArguments($args);
100 call_user_func_array([$object, $method], $args);