BasicAssertion::invert()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 19

Duplication

Lines 6
Ratio 31.58 %

Importance

Changes 0
Metric Value
dl 6
loc 19
rs 9.6333
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
3
/**
4
 * \AppserverIo\Doppelgaenger\Entities\Assertions\BasicAssertion
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Bernhard Wick <[email protected]>
15
 * @copyright 2015 TechDivision GmbH - <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/doppelgaenger
18
 * @link      http://www.appserver.io/
19
 */
20
21
namespace AppserverIo\Doppelgaenger\Entities\Assertions;
22
23
/**
24
 * Basic assertions to compare two values
25
 *
26
 * @author    Bernhard Wick <[email protected]>
27
 * @copyright 2015 TechDivision GmbH - <[email protected]>
28
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
29
 * @link      https://github.com/appserver-io/doppelgaenger
30
 * @link      http://www.appserver.io/
31
 */
32
class BasicAssertion extends AbstractAssertion
33
{
34
    /**
35
     * @var string $firstOperand The first operand to compare
36
     */
37
    public $firstOperand;
38
39
    /**
40
     * @var string $secondOperand The second operand to compare
41
     */
42
    public $secondOperand;
43
44
    /**
45
     * @var string $operator The operator used for comparison
46
     */
47
    public $operator;
48
49
    /**
50
     * @var array $inversionMapping A map to inverse operators
51
     */
52
    protected $inversionMapping;
53
54
    /**
55
     * Default constructor
56
     *
57
     * @param string $firstOperand  The first operand to compare
58
     * @param string $secondOperand The second operand to compare
59
     * @param string $operator      The operator used for comparison
60
     */
61
    public function __construct($firstOperand = '', $secondOperand = '', $operator = '')
62
    {
63
        $this->firstOperand = $firstOperand;
64
        $this->secondOperand = $secondOperand;
65
        $this->operator = $operator;
66
67
        // Set the mapping for our inversion
68
        $this->inversionMapping = array(
69
            '==' => '!=',
70
            '===' => '!==',
71
            '<>' => '==',
72
            '<' => '>=',
73
            '>' => '<=',
74
            '<=' => '>',
75
            '>=' => '<',
76
            '!=' => '==',
77
            '!==' => '==='
78
        );
79
80
        parent::__construct();
81
    }
82
83
    /**
84
     * Will return a string representation of this assertion
85
     *
86
     * @return string
87
     */
88
    public function getString()
89
    {
90
        return (string)$this->firstOperand . ' ' . $this->operator . ' ' . $this->secondOperand;
91
    }
92
93
    /**
94
     * Will return an inverted string representation.
95
     * Implemented here, as we want to check if there is an entry in our inversion map we can use
96
     *
97
     * @return string
98
     */
99
    public function getInvertString()
100
    {
101
        if (isset($this->inversionMapping[$this->operator])) {
102
            // only return if we found something
103
            return (string)$this->firstOperand . ' ' .
104
            $this->inversionMapping[$this->operator] . ' ' . $this->secondOperand;
105
        }
106
    }
107
108
    /**
109
     * Invert the logical meaning of this assertion
110
     *
111
     * @return bool
112
     */
113
    public function invert()
114
    {
115
        if (isset($this->inversionMapping[$this->operator])) {
116
            // Invert the operator
117
            $this->operator = $this->inversionMapping[$this->operator];
118
            // Don't forget to mark this assertion as inverted
119 View Code Duplication
            if ($this->inverted === true) {
120
                $this->inverted = false;
121
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
122
            } else {
123
                $this->inverted = true;
124
            }
125
126
            return true;
127
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
128
        } else {
129
            return false;
130
        }
131
    }
132
}
133