1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* inetprocess/transformation |
4
|
|
|
* |
5
|
|
|
* PHP Version 5.3 |
6
|
|
|
* |
7
|
|
|
* @author Rémi Sauvat |
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\Rule; |
18
|
|
|
|
19
|
|
|
use Inet\Transformation\Exception\TransformationException; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Call a function and send back the result |
23
|
|
|
*/ |
24
|
|
|
class Map extends AbstractRule |
25
|
|
|
{ |
26
|
|
|
protected $mapping = array(); |
27
|
|
|
/** |
28
|
|
|
* Operate the transformation |
29
|
|
|
* |
30
|
|
|
* @param string $input |
31
|
|
|
* @param array $arguments |
32
|
|
|
* |
33
|
|
|
* @throws Inet\Transformation\Exception\TransformationException |
34
|
|
|
* |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
8 |
|
public function transform($input, $arguments) |
38
|
|
|
{ |
39
|
|
|
// I should have two arguments: old format / new format |
40
|
8 |
|
if (count($arguments) !== 1) { |
41
|
2 |
|
throw new TransformationException( |
42
|
|
|
'Rule Map expects exactly 1 argument' |
43
|
2 |
|
); |
44
|
|
|
} |
45
|
6 |
|
$mapping = $arguments[0]; |
46
|
6 |
|
if (!is_array($mapping)) { |
47
|
1 |
|
throw new TransformationException( |
48
|
|
|
'First argument of Map should by an assosiative array' |
49
|
1 |
|
); |
50
|
|
|
} |
51
|
5 |
|
$this->mapping = $mapping; |
52
|
|
|
|
53
|
5 |
|
if (!is_array($input)) { |
54
|
4 |
|
return $this->map($input); |
55
|
|
|
} |
56
|
|
|
// More complex version if input is an array |
57
|
|
|
// Map each cell independently |
58
|
|
|
// And return an array |
|
|
|
|
59
|
1 |
|
return array_map(array($this, 'map'), $input); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
5 |
|
public function map($value) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
// If not array map and return fast |
65
|
5 |
|
if (array_key_exists($value, $this->mapping)) { |
66
|
2 |
|
return $this->mapping[$value]; |
67
|
|
|
} |
68
|
|
|
// Couldn't map so return as is |
69
|
4 |
|
return $value; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.