AbstractStyle::addByValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 3
1
<?php
2
3
namespace ZfcDatagrid\Column\Style;
4
5
use ZfcDatagrid\Column\AbstractColumn;
6
use ZfcDatagrid\Filter;
7
8
abstract class AbstractStyle
9
{
10
    protected $byValueOperator = 'OR';
11
12
    /**
13
     * @var array
14
     */
15
    private $byValues = [];
16
17
    /**
18
     * Display the values with AND or OR (if multiple showOnValues are defined).
19
     *
20
     * @param string $operator
21
     */
22 View Code Duplication
    public function setByValueOperator($operator = 'OR')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
23
    {
24
        if ($operator != 'AND' && $operator != 'OR') {
25
            throw new \InvalidArgumentException('not allowed operator: "'.$operator.'" (AND / OR is allowed)');
26
        }
27
28
        $this->byValueOperator = (string) $operator;
29
    }
30
31
    /**
32
     * Get the show on value operator, e.g.
33
     * OR, AND.
34
     *
35
     * @return string
36
     */
37
    public function getByValueOperator()
38
    {
39
        return $this->byValueOperator;
40
    }
41
42
    /**
43
     * Set the style value based and not general.
44
     *
45
     * @param AbstractColumn $column
46
     * @param mixed          $value
47
     * @param string         $operator
48
     */
49
    public function addByValue(AbstractColumn $column, $value, $operator = Filter::EQUAL)
50
    {
51
        $this->byValues[] = [
52
            'column' => $column,
53
            'value' => $value,
54
            'operator' => $operator,
55
        ];
56
    }
57
58
    /**
59
     * @return array
60
     */
61
    public function getByValues()
62
    {
63
        return $this->byValues;
64
    }
65
66
    /**
67
     * @return bool
68
     */
69
    public function hasByValues()
70
    {
71
        if (count($this->byValues) > 0) {
72
            return true;
73
        }
74
75
        return false;
76
    }
77
78
    /**
79
     * @param array $row
80
     *
81
     * @return bool
82
     */
83 View Code Duplication
    public function isApply(array $row)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
    {
85
        if ($this->hasByValues() === false) {
86
            return true;
87
        }
88
89
        $isApply = false;
90
        foreach ($this->getByValues() as $rule) {
91
            $value = '';
92
            if (isset($row[$rule['column']->getUniqueId()])) {
93
                $value = $row[$rule['column']->getUniqueId()];
94
            }
95
96
            if ($rule['value'] instanceof AbstractColumn) {
97
                if (isset($row[$rule['value']->getUniqueId()])) {
98
                    $ruleValue = $row[$rule['value']->getUniqueId()];
99
                } else {
100
                    $ruleValue = '';
101
                }
102
            } else {
103
                $ruleValue = $rule['value'];
104
            }
105
106
            $isApplyMatch = Filter::isApply($value, $ruleValue, $rule['operator']);
107
            if ($this->getByValueOperator() == 'OR' && true === $isApplyMatch) {
108
                // For OR one match is enough
109
                return true;
110
            } elseif ($this->getByValueOperator() == 'AND' && false === $isApplyMatch) {
111
                return false;
112
            } else {
113
                $isApply = $isApplyMatch;
114
            }
115
        }
116
117
        return $isApply;
118
    }
119
}
120