1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19

20
/**
21
 * Condition that compare versions
22
 *
23
 * @author  Tomáš Fejfar (tomas.fejfar@gmail.com)
24
 * @package phing.tasks.system.condition
25
 */
26
class VersionCompareCondition implements Condition
27
{
28

29
    /**
30
     * Actual version
31
     *
32
     * @var string
33
     */
34
    private $version;
35

36
    /**
37
     * Version to be compared to
38
     *
39
     * @var string
40
     */
41
    private $desiredVersion;
42

43
    /**
44
     * Operator to use (default "greater or equal")
45
     *
46
     * @var string operator for possible values @see http://php.net/version%20compare
47
     */
48
    private $operator = '>=';
49

50
    private $debug = false;
51

52
    /**
53
     * @param $version
54
     */
55 1
    public function setVersion($version)
56
    {
57 1
        $this->version = $version;
58
    }
59

60
    /**
61
     * @param $desiredVersion
62
     */
63 1
    public function setDesiredVersion($desiredVersion)
64
    {
65 1
        $this->desiredVersion = $desiredVersion;
66
    }
67

68
    /**
69
     * @param $operator
70
     * @throws BuildException
71
     */
72 1
    public function setOperator($operator)
73
    {
74 1
        $allowed = ['<', 'lt', '<=', 'le', '>', 'gt', '>=', 'ge', '==', '=', 'eq', '!=', '<>', 'ne'];
75 1
        if (!in_array($operator, $allowed)) { // allowed operators for php's version_comapare()
76 1
            throw new BuildException(
77 1
                sprintf(
78 1
                    'Operator "%s" is not supported. Supported operators: %s',
79 1
                    $operator,
80 1
                    implode(', ', $allowed)
81
                )
82
            );
83
        }
84 1
        $this->operator = $operator;
85
    }
86

87 1
    public function setDebug(bool $debug)
88
    {
89 1
        $this->debug = $debug;
90
    }
91

92
    /**
93
     * @return mixed
94
     * @throws BuildException
95
     */
96 1
    public function evaluate()
97
    {
98 1
        if ($this->version === null || $this->desiredVersion === null) {
99 0
            throw new BuildException("Missing one version parameter for version compare");
100
        }
101 1
        $isValid = version_compare($this->version, $this->desiredVersion, $this->operator);
102 1
        if ($this->debug) {
103 1
            echo sprintf(
104 1
                'Assertion that %s %s %s failed' . PHP_EOL,
105 1
                $this->version,
106 1
                $this->operator,
107 1
                $this->desiredVersion
108
            );
109
        }
110 1
        return $isValid;
111
    }
112
}

Read our documentation on viewing source code .

Loading