Completed
Push — master ( 061e7a...dc8b78 )
by Rémi
04:53
created

Transform   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 2
cbo 2
dl 0
loc 142
c 0
b 0
f 0
ccs 41
cts 41
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getFactory() 0 8 2
A getRules() 0 4 1
A cleanRules() 0 4 1
A addRule() 0 16 3
A __callStatic() 0 7 1
A __call() 0 6 1
A transform() 0 20 3
1
<?php
2
/**
3
 * inetprocess/transformation
4
 *
5
 * PHP Version 5.3
6
 *
7
 * @author Emmanuel Dyan
8
 * @copyright 2005-2015 iNet Process
9
 *
10
 * @package inetprocess/transformation
11
 *
12
 * @license GNU General Public License v2.0
13
 *
14
 * @link http://www.inetprocess.com
15
 */
16
17
namespace Inet\Transformation;
18
19
use Inet\Transformation\Exception\InvalidRuleException;
20
use Inet\Transformation\Exception\NotTransformableException;
21
22
/**
23
 * Main class that calls statically any Rule
24
 *
25
 * # CallBack: Be careful that the value to transform is set as the latest parameter
26
 * @method static Transform Callback(callable $callable, ...)
27
 * @method static Transform Concat(string $before, [string $after])
28
 * @method static Transform Date(string $inputFormat, string $outputFormat)
29
 * @method static Transform Implode(string $glue)
30
 * @method static Transform Explode(string $delimiter)
31
 * @method static Transform NormalizeURL(string $protocol)
32
 * @method static Transform Replace(string $search, string $replace)
33
 * @method static Transform ReplaceRegexp(string $pattern, string $replacement)
34
 * @method static Transform Slugify()
35
 * @method static Transform Timezone(string $inputFormat, string $targetTimezone, [string $currentTimezone])
36
 */
37
class Transform
38
{
39
    /**
40
     * Contains the factory
41
     *
42
     * @var Transform
43
     */
44
    protected static $transform;
45
46
    /**
47
     * List of rules to execute on an input
48
     *
49
     * @var array
50
     */
51
    protected $currentRules = array();
52
53
    /**
54
     * Doesn't allow the class to be instanciated
55
     */
56 1
    protected function __construct()
57
    {
58 1
    }
59
60
    /**
61
     * Returns the factory to avoid having multiple instances of the same class
62
     *
63
     * @return Transform
64
     */
65 74
    protected static function getFactory()
66
    {
67 74
        if (!static::$transform instanceof Transform) {
68 1
            static::$transform = new static();
69 1
        }
70
71 74
        return static::$transform;
72
    }
73
74
    /**
75
     * Return an Array of rules for an instance
76
     *
77
     * @return array
78
     */
79 61
    protected function getRules()
80
    {
81 61
        return $this->currentRules;
82
    }
83
84
    /**
85
     * Called on the first time to remove all old rules
86
     *
87
     * @return void
88
     */
89 74
    protected function cleanRules()
90
    {
91 74
        $this->currentRules = array();
92 74
    }
93
94
    /**
95
     * Add a rule to the rules registry
96
     *
97
     * @param string $ruleName
98
     * @param array  $arguments
99
     *
100
     * @return void
101
     */
102 74
    protected function addRule($ruleName, $arguments)
103
    {
104 74
        $ruleClass = "Inet\Transformation\Rule\\" . $ruleName;
105 74
        if (!class_exists($ruleClass)) {
106 2
            throw new InvalidRuleException("The rule '$ruleName' does not exist");
107
        }
108
109 72
        if (!is_subclass_of($ruleClass, '\Inet\Transformation\Rule\AbstractRule')) {
110 1
            throw new InvalidRuleException("The rule '$ruleName' must implement AbstractRule");
111
        }
112
113 71
        $this->currentRules[] = array(
114 71
            'rule' => $ruleClass,
115 71
            'arguments' => $arguments,
116
        );
117 71
    }
118
119
    /**
120
     * For the first execution, the method is called statically such as T::Replace('a', 'b')
121
     * Then get the factory, clean the previous rules and do a __call()
122
     *
123
     * @param string $ruleName
124
     * @param array  $arguments
125
     *
126
     * @return Transform
127
     */
128 74
    public static function __callStatic($ruleName, $arguments)
129
    {
130 74
        $transform = self::getFactory();
131 74
        $transform->cleanRules();
132
133 74
        return $transform->__call($ruleName, $arguments);
134
    }
135
136
    /**
137
     * Just add a rule in the registry when called
138
     *
139
     * @param string $ruleName  Name of the rule
140
     * @param array  $arguments Arguments for the rule
141
     *
142
     * @return Transform
143
     */
144 74
    public function __call($ruleName, $arguments)
145
    {
146 74
        $this->addRule($ruleName, $arguments);
147
148 71
        return $this;
149
    }
150
151
    /**
152
     * Transform an input by applying rule
153
     *
154
     * @param string $input Should be a string !
155
     *
156
     * @return string Transformed string
157
     */
158 71
    public function transform($input)
159
    {
160 71
        $validTypes = array('boolean', 'integer', 'double', 'string', 'array');
161 71
        $inputType = gettype($input);
162 71
        if (!in_array($inputType, $validTypes)) {
163 10
            throw new NotTransformableException;
164
        }
165
166
        // Apply rule by rule
167 61
        foreach ($this->getRules() as $ruleArgs) {
168
            // Execute the transformation
169 61
            $rule = $ruleArgs['rule'];
170 61
            $args = $ruleArgs['arguments'];
171
172 61
            $rule = new $rule;
173 61
            $input = $rule->transform($input, $args);
174 30
        }
175
176 30
        return $input;
177
    }
178
}
179