VarTranslator   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 15
lcom 2
cbo 4
dl 0
loc 120
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getTranslation() 0 7 2
A setTranslation() 0 5 1
C apply() 0 30 9
A applyOnVar() 0 5 1
A createTranslation() 0 4 1
1
<?php
2
/**
3
 * Webino (http://webino.sk)
4
 *
5
 * @link        https://github.com/webino/WebinoDraw for the canonical source repository
6
 * @copyright   Copyright (c) 2012-2017 Webino, s. r. o. (http://webino.sk)
7
 * @author      Peter Bačinský <[email protected]>
8
 * @license     BSD-3-Clause
9
 */
10
11
namespace WebinoDraw\VarTranslator;
12
13
use WebinoDraw\VarTranslator\Operation\Filter;
14
use WebinoDraw\VarTranslator\Operation\Helper;
15
use WebinoDraw\VarTranslator\Operation\OnVar;
16
17
/**
18
 * Replace variables in array with values in the other array.
19
 *
20
 * The first array is a specification with custom options
21
 * with {$variable} in values.
22
 *
23
 * The second array contains data by variable names like
24
 * keys. Those {$variable} will be substituted with data.
25
 */
26
class VarTranslator
27
{
28
    /**
29
     * @var Filter
30
     */
31
    protected $filter;
32
33
    /**
34
     * @var Helper
35
     */
36
    protected $helper;
37
38
    /**
39
     * @var OnVar
40
     */
41
    protected $onVar;
42
43
    /**
44
     * Global translation
45
     *
46
     * @var Translation
47
     */
48
    protected $translation;
49
50
    /**
51
     * @param Filter $filter
52
     * @param Helper $helper
53
     * @param OnVar $onVar
54
     */
55
    public function __construct(Filter $filter, Helper $helper, OnVar $onVar)
56
    {
57
        $this->filter = $filter;
58
        $this->helper = $helper;
59
        $this->onVar  = $onVar;
60
    }
61
62
    /**
63
     * Get global translation
64
     *
65
     * @return Translation
66
     */
67
    public function getTranslation()
68
    {
69
        if (null === $this->translation) {
70
            $this->setTranslation(new Translation);
71
        }
72
        return $this->translation;
73
    }
74
75
    /**
76
     * Set global translation
77
     *
78
     * @param Translation $translation
79
     * @return $this
80
     */
81
    public function setTranslation(Translation $translation)
82
    {
83
        $this->translation = $translation;
84
        return $this;
85
    }
86
87
    /**
88
     * @param Translation $translation
89
     * @param array $spec
90
     * @return $this
91
     */
92
    public function apply(Translation $translation, array $spec)
93
    {
94
        $translation->merge($this->getTranslation()->getArrayCopy());
95
96
        empty($spec['var']['default'])
97
            or $translation->setDefaults($spec['var']['default']);
98
99
        empty($spec['var']['set'])
100
            or $translation->mergeValues($spec['var']['set']);
101
102
        empty($spec['var']['fetch'])
103
            or $translation->fetchVars($spec['var']['fetch']);
104
105
        empty($spec['var']['filter']['pre'])
106
            or $this->filter->apply($translation, $spec['var']['filter']['pre']);
107
108
        empty($spec['var']['helper'])
109
            or $this->helper->apply($translation, $spec['var']['helper']);
110
111
        empty($spec['var']['filter']['post'])
112
            or $this->filter->apply($translation, $spec['var']['filter']['post']);
113
114
        empty($spec['var']['default'])
115
            or $translation->setDefaults($spec['var']['default']);
116
117
        empty($spec['var']['push'])
118
            or $this->getTranslation()->pushVars($spec['var']['push'], $translation);
0 ignored issues
show
Documentation introduced by
$translation is of type object<WebinoDraw\VarTranslator\Translation>, but the function expects a null|object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
119
120
        return $this;
121
    }
122
123
    /**
124
     * Apply variable logic
125
     *
126
     * @param Translation $varTranslation
127
     * @param array $spec
128
     * @param callable $callback
129
     * @return $this
130
     */
131
    public function applyOnVar(Translation $varTranslation, array $spec, callable $callback)
132
    {
133
        $this->onVar->apply($varTranslation, $spec, $callback);
134
        return $this;
135
    }
136
137
    /**
138
     * @param array $vars
139
     * @return Translation
140
     */
141
    public function createTranslation(array $vars)
142
    {
143
        return new Translation($vars);
144
    }
145
}
146