Completed
Pull Request — master (#126)
by Phil
19:36 queued 04:37
created

Inflector   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 136
c 0
b 0
f 0
wmc 12
lcom 1
cbo 2
ccs 27
cts 27
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getType() 0 4 1
A invokeMethod() 0 6 1
A invokeMethods() 0 8 2
A setProperty() 0 6 1
A setProperties() 0 8 2
A inflect() 0 19 4
1
<?php declare(strict_types=1);
2
3
namespace League\Container\Inflector;
4
5
use League\Container\ContainerAwareTrait;
6
use League\Container\Argument\ArgumentResolverInterface;
7
use League\Container\Argument\ArgumentResolverTrait;
8
9
class Inflector implements ArgumentResolverInterface
10
{
11
    use ArgumentResolverTrait;
12
    use ContainerAwareTrait;
13
14
    /**
15
     * @var string
16
     */
17
    protected $type;
18
19
    /**
20
     * @var callable
21
     */
22
    protected $callback;
23
24
    /**
25
     * @var array
26
     */
27
    protected $methods = [];
28
29
    /**
30
     * @var array
31 6
     */
32
    protected $properties = [];
33 6
34
    /**
35 6
     * Construct.
36
     *
37
     * @param string        $type
38
     * @param callable|null $callback
39
     */
40
    public function __construct(string $type, callable $callback = null)
41
    {
42
        $this->type     = $type;
43
        $this->callback = $callback;
44 3
    }
45
46 3
    /**
47 3
     * Get the type.
48 2
     *
49
     * @return string
50 3
     */
51
    public function getType(): string
52
    {
53
        return $this->type;
54
    }
55
56
    /**
57
     * Defines a method to be invoked on the subject object.
58
     *
59
     * @param string $name
60 9
     * @param array  $args
61
     *
62 9
     * @return self
63
     */
64 9
    public function invokeMethod(string $name, array $args): self
65
    {
66
        $this->methods[$name] = $args;
67
68
        return $this;
69
    }
70
71
    /**
72
     * Defines multiple methods to be invoked on the subject object.
73 3
     *
74
     * @param array $methods
75 3
     *
76 3
     * @return self
77 2
     */
78
    public function invokeMethods(array $methods): self
79 3
    {
80
        foreach ($methods as $name => $args) {
81
            $this->invokeMethod($name, $args);
82
        }
83
84
        return $this;
85
    }
86
87
    /**
88 9
     * Defines a property to be set on the subject object.
89
     *
90 9
     * @param string $property
91 9
     * @param mixed  $value
92
     *
93 9
     * @return self
94 6
     */
95 6
    public function setProperty(string $property, $value): self
96
    {
97 9
        $this->properties[$property] = $value;
98 3
99
        return $this;
100 3
    }
101 6
102 9
    /**
103
     * Defines multiple properties to be set on the subject object.
104
     *
105
     * @param array $properties
106
     *
107
     * @return self
108
     */
109
    public function setProperties(array $properties): self
110
    {
111
        foreach ($properties as $property => $value) {
112
            $this->setProperty($property, $value);
113
        }
114
115
        return $this;
116
    }
117
118
    /**
119
     * Apply inflections to an object.
120
     *
121
     * @param object $object
122
     *
123
     * @return void
124
     */
125
    public function inflect($object)
126
    {
127
        $properties = $this->resolveArguments(array_values($this->properties));
128
        $properties = array_combine(array_keys($this->properties), $properties);
129
130
        foreach ($properties as $property => $value) {
131
            $object->{$property} = $value;
132
        }
133
134
        foreach ($this->methods as $method => $args) {
135
            $args = $this->resolveArguments($args);
136
137
            call_user_func_array([$object, $method], $args);
138
        }
139
140
        if (! is_null($this->callback)) {
141
            call_user_func_array($this->callback, [$object]);
142
        }
143
    }
144
}
145