08f86970e354d5db928446aad5740d98713a2f45
[yaffs-website] / vendor / league / container / src / Inflector / Inflector.php
1 <?php
2
3 namespace League\Container\Inflector;
4
5 use League\Container\ImmutableContainerAwareTrait;
6 use League\Container\Argument\ArgumentResolverInterface;
7 use League\Container\Argument\ArgumentResolverTrait;
8
9 class Inflector implements ArgumentResolverInterface
10 {
11     use ArgumentResolverTrait;
12     use ImmutableContainerAwareTrait;
13
14     /**
15      * @var array
16      */
17     protected $methods = [];
18
19     /**
20      * @var array
21      */
22     protected $properties = [];
23
24     /**
25      * Defines a method to be invoked on the subject object.
26      *
27      * @param  string $name
28      * @param  array  $args
29      * @return $this
30      */
31     public function invokeMethod($name, array $args)
32     {
33         $this->methods[$name] = $args;
34
35         return $this;
36     }
37
38     /**
39      * Defines multiple methods to be invoked on the subject object.
40      *
41      * @param  array $methods
42      * @return $this
43      */
44     public function invokeMethods(array $methods)
45     {
46         foreach ($methods as $name => $args) {
47             $this->invokeMethod($name, $args);
48         }
49
50         return $this;
51     }
52
53     /**
54      * Defines a property to be set on the subject object.
55      *
56      * @param  string $property
57      * @param  mixed  $value
58      * @return $this
59      */
60     public function setProperty($property, $value)
61     {
62         $this->properties[$property] = $value;
63
64         return $this;
65     }
66
67     /**
68      * Defines multiple properties to be set on the subject object.
69      *
70      * @param  array $properties
71      * @return $this
72      */
73     public function setProperties(array $properties)
74     {
75         foreach ($properties as $property => $value) {
76             $this->setProperty($property, $value);
77         }
78
79         return $this;
80     }
81
82     /**
83      * Apply inflections to an object.
84      *
85      * @param  object $object
86      * @return void
87      */
88     public function inflect($object)
89     {
90         $properties = $this->resolveArguments(array_values($this->properties));
91         $properties = array_combine(array_keys($this->properties), $properties);
92
93         foreach ($properties as $property => $value) {
94             $object->{$property} = $value;
95         }
96
97         foreach ($this->methods as $method => $args) {
98             $args = $this->resolveArguments($args);
99
100             call_user_func_array([$object, $method], $args);
101         }
102     }
103 }