Completed
Push — master ( e401e5...bd7189 )
by Julien
01:48
created

AbstractArgsDefinition::propertizeArguments()   C

Complexity

Conditions 8
Paths 25

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 28
rs 5.3846
cc 8
eloc 18
nc 25
nop 3
1
<?php
2
/**
3
 * Fwk
4
 *
5
 * Copyright (c) 2011-2016, Julien Ballestracci <[email protected]>.
6
 * All rights reserved.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
12
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
13
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
15
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
16
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
17
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
19
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
20
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
21
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
22
 * POSSIBILITY OF SUCH DAMAGE.
23
 *
24
 * PHP Version 5.3
25
 *
26
 * @category  DependencyInjection
27
 * @package   Fwk\Di
28
 * @author    Julien Ballestracci <[email protected]>
29
 * @copyright 2011-2016 Julien Ballestracci <[email protected]>
30
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
31
 * @link      http://fwk.io/di
32
 */
33
namespace Fwk\Di\Definitions;
34
35
use Fwk\Di\Container;
36
use Fwk\Di\InvokableInterface;
37
use Fwk\Di\Exceptions;
38
use Fwk\Di\Reference;
39
40
/**
41
 * Abstract Definition Utility
42
 *
43
 * @category Utilities
44
 * @package  Fwk\Di
45
 * @author   Julien Ballestracci <[email protected]>
46
 * @license  http://www.opensource.org/licenses/bsd-license.php  BSD License
47
 * @link     http://fwk.io/di
48
 */
49
abstract class AbstractArgsDefinition extends AbstractDefinition
50
{
51
    /**
52
     * List of arguments
53
     * @var array<mixed>
54
     */
55
    protected $arguments = array();
56
    
57
    /**
58
     * Constructor
59
     * 
60
     * @param mixed        $arg       Mixed argument
61
     * @param array<mixed> $arguments List of arguments
62
     * 
63
     * @abstract
64
     * @return   void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
65
     */
66
    abstract public function __construct($arg, array $arguments = array());
67
            
68
    /**
69
     * Return the list of arguments
70
     * 
71
     * @return array<mixed>
72
     */
73
    public function getArguments()
74
    {
75
        return $this->arguments;
76
    }
77
    
78
    /**
79
     * Adds an argument to the Definition.
80
     * 
81
     * For a ClassDefinition these arguments are passed to the constructor.
82
     * 
83
     * @param string|InvokableInterface $argument The Argument
84
     * 
85
     * @return self
86
     */
87
    public function addArgument($argument)
88
    {
89
        $this->arguments[] = $argument;
90
        
91
        return $this;
92
    }
93
    
94
    /**
95
     * Add multiples arguments (merge)
96
     * 
97
     * @param array<mixed> $arguments List of new arguments
98
     * 
99
     * @return self
100
     */
101
    public function addArguments(array $arguments)
102
    {
103
        $this->arguments += $arguments;
104
        
105
        return $this;
106
    }
107
    
108
    /**
109
     * Returns all arguments (computed)
110
     * 
111
     * @param Container   $container  The Di Container
112
     * @param null|string $definition Name of the current definition (if any)
113
     * 
114
     * @return array<mixed>
115
     */
116
    protected function getConstructorArguments(Container $container, 
117
        $definition = null
118
    ) {
119
        if (!count($this->arguments)) {
120
            return array($container);
121
        }
122
123
        return $this->propertizeArguments(
124
            $this->arguments, 
125
            $container, 
126
            $definition
127
        );
128
    }
129
    
130
    /**
131
     * Transform arguments to their real value if they are instance of InvokableInterface
132
     * or a @reference.
133
     * 
134
     * @param array<mixed> $args       List of arguments
135
     * @param Container    $container  The Di Container
136
     * @param null|string  $definition Name of the current definition (if any)
137
     * 
138
     * @return array<mixed>
139
     * @throws Exceptions\InvalidArgumentException
140
     */
141
    protected function propertizeArguments(array $args, Container $container,
142
        $definition = null
143
    ) {
144
        $return = array();
145
        foreach ($args as $idx => $arg) {
146
            $arg = $this->transformValueType($arg);
147
            if (is_string($arg)) {
148
                $arg = $container->propertizeString($arg);
149
            }
150
            
151
            if (is_string($arg) && strpos($arg, '@', 0) === 0) {
152
                $arg = new Reference(substr($arg, 1));
153
            } elseif (is_array($arg)) {
154
                $arg = $this->propertizeArguments($arg, $container, $definition);
155
            }
156
            
157
            try {
158
                $return[$idx] = (($arg instanceof InvokableInterface)
159
                    ? $arg->invoke($container, $definition) 
160
                    : $arg
161
                );
162
            } catch(\Fwk\Di\Exception $exp) {
163
                throw new Exceptions\InvalidArgumentException($idx, $definition, $exp);
164
            }
165
        }
166
        
167
        return $return;
168
    }
169
    
170
    /**
171
     * Transforms a string to a type, if known:
172
     * 
173
     * - boolean: true / false
174
     * - null: null
175
     * 
176
     * @param string $value The initial string value
177
     * 
178
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use null|string|boolean.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
179
     */
180
    protected function transformValueType($value)
181
    {
182
        if (!is_string($value)) {
183
            return $value;
184
        }
185
        
186
        $value = trim($value);
187
        if (strtolower($value) === "true") {
188
            $value = true;
189
        } elseif (strtolower($value) === "false") {
190
            $value = false;
191
        } elseif (strtolower($value) === "null") {
192
            $value = null;
193
        }
194
        
195
        return $value;
196
    }
197
    
198
    /**
199
     * Factory method
200
     * 
201
     * @param mixed        $arg       Mixed argument
202
     * @param array<mixed> $arguments List of arguments
203
     * 
204
     * @return self
205
     * @static
206
     */
207
    public static function factory($arg, array $arguments = array())
208
    {
209
        return new static($arg, $arguments);
210
    }
211
}
212