Completed
Push — feat-events-tags ( 999033...033a2d )
by Julien
04:43
created

AbstractDefinition::propertizeArguments()   C

Complexity

Conditions 8
Paths 25

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 8.216

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 28
ccs 17
cts 20
cp 0.85
rs 5.3846
cc 8
eloc 18
nc 25
nop 3
crap 8.216
1
<?php
2
/**
3
 * Fwk
4
 *
5
 * Copyright (c) 2011-2012, 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-2014 Julien Ballestracci <[email protected]>
30
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
31
 * @link      http://www.nitronet.org/fwk
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://www.nitronet.org/fwk
48
 */
49
abstract class 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 9
    public function addArgument($argument)
88
    {
89 9
        $this->arguments[] = $argument;
90
        
91 9
        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 10
    protected function getConstructorArguments(Container $container, 
117
        $definition = null
118
    ) {
119 10
        return $this->propertizeArguments(
120 10
            $this->arguments, 
121 10
            $container, 
122
            $definition
123 10
        );
124
    }
125
    
126
    /**
127
     * Transform arguments to their real value if they are instance of InvokableInterface
128
     * or a @reference.
129
     * 
130
     * @param array<mixed> $args       List of arguments
131
     * @param Container    $container  The Di Container
132
     * @param null|string  $definition Name of the current definition (if any)
133
     * 
134
     * @return array<mixed>
135
     * @throws Exceptions\InvalidArgumentException
136
     */
137 13
    protected function propertizeArguments(array $args, Container $container,
138
        $definition = null
139
    ) {
140 13
        $return = array();
141 13
        foreach ($args as $idx => $arg) {
142 12
            $arg = $this->transformValueType($arg);
143 12
            if (is_string($arg)) {
144 9
                $arg = $container->propertizeString($arg);
145 9
            }
146
            
147 12
            if (is_string($arg) && strpos($arg, '@', 0) === 0) {
148 3
                $arg = new Reference(substr($arg, 1));
149 12
            } elseif (is_array($arg)) {
150
                $arg = $this->propertizeArguments($arg, $container, $definition);
151
            }
152
            
153
            try {
154 12
                $return[$idx] = (($arg instanceof InvokableInterface)
155 12
                    ? $arg->invoke($container, $definition) 
156 3
                    : $arg
157
                );
158 12
            } catch(\Fwk\Di\Exception $exp) {
159 3
                throw new Exceptions\InvalidArgumentException($idx, $definition, $exp);
160
            }
161 11
        }
162
        
163 11
        return $return;
164
    }
165
    
166
    /**
167
     * Transforms a string to a type, if known:
168
     * 
169
     * - boolean: true / false
170
     * - null: null
171
     * 
172
     * @param string $value The initial string value
173
     * 
174
     * @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...
175
     */
176 12
    protected function transformValueType($value)
177
    {
178 12
        if (!is_string($value)) {
179 5
            return $value;
180
        }
181
        
182 9
        $value = trim($value);
183 9
        if (strtolower($value) === "true") {
184
            $value = true;
185 9
        } elseif (strtolower($value) === "false") {
186
            $value = false;
187 9
        } elseif (strtolower($value) === "null") {
188
            $value = null;
189
        }
190
        
191 9
        return $value;
192
    }
193
    
194
    /**
195
     * Factory method
196
     * 
197
     * @param mixed        $arg       Mixed argument
198
     * @param array<mixed> $arguments List of arguments
199
     * 
200
     * @return self
201
     * @static
202
     */
203 2
    public static function factory($arg, array $arguments = array())
204
    {
205 2
        return new static($arg, $arguments);
206
    }
207
}
208