Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
namespace Hamcrest\Number;
3
 
4
/*
5
 Copyright (c) 2009 hamcrest.org
6
 */
7
 
8
use Hamcrest\Description;
9
use Hamcrest\TypeSafeMatcher;
10
 
11
class OrderingComparison extends TypeSafeMatcher
12
{
13
 
14
    private $_value;
15
    private $_minCompare;
16
    private $_maxCompare;
17
 
18
    public function __construct($value, $minCompare, $maxCompare)
19
    {
20
        parent::__construct(self::TYPE_NUMERIC);
21
 
22
        $this->_value = $value;
23
        $this->_minCompare = $minCompare;
24
        $this->_maxCompare = $maxCompare;
25
    }
26
 
27
    protected function matchesSafely($other)
28
    {
29
        $compare = $this->_compare($this->_value, $other);
30
 
31
        return ($this->_minCompare <= $compare) && ($compare <= $this->_maxCompare);
32
    }
33
 
34
    protected function describeMismatchSafely($item, Description $mismatchDescription)
35
    {
36
        $mismatchDescription
37
            ->appendValue($item)->appendText(' was ')
38
            ->appendText($this->_comparison($this->_compare($this->_value, $item)))
39
            ->appendText(' ')->appendValue($this->_value)
40
            ;
41
    }
42
 
43
    public function describeTo(Description $description)
44
    {
45
        $description->appendText('a value ')
46
            ->appendText($this->_comparison($this->_minCompare))
47
            ;
48
        if ($this->_minCompare != $this->_maxCompare) {
49
            $description->appendText(' or ')
50
                ->appendText($this->_comparison($this->_maxCompare))
51
                ;
52
        }
53
        $description->appendText(' ')->appendValue($this->_value);
54
    }
55
 
56
    /**
57
     * The value is not > $value, nor < $value.
58
     *
59
     * @factory
60
     */
61
    public static function comparesEqualTo($value)
62
    {
63
        return new self($value, 0, 0);
64
    }
65
 
66
    /**
67
     * The value is > $value.
68
     *
69
     * @factory
70
     */
71
    public static function greaterThan($value)
72
    {
73
        return new self($value, -1, -1);
74
    }
75
 
76
    /**
77
     * The value is >= $value.
78
     *
79
     * @factory atLeast
80
     */
81
    public static function greaterThanOrEqualTo($value)
82
    {
83
        return new self($value, -1, 0);
84
    }
85
 
86
    /**
87
     * The value is < $value.
88
     *
89
     * @factory
90
     */
91
    public static function lessThan($value)
92
    {
93
        return new self($value, 1, 1);
94
    }
95
 
96
    /**
97
     * The value is <= $value.
98
     *
99
     * @factory atMost
100
     */
101
    public static function lessThanOrEqualTo($value)
102
    {
103
        return new self($value, 0, 1);
104
    }
105
 
106
    // -- Private Methods
107
 
108
    private function _compare($left, $right)
109
    {
110
        $a = $left;
111
        $b = $right;
112
 
113
        if ($a < $b) {
114
            return -1;
115
        } elseif ($a == $b) {
116
            return 0;
117
        } else {
118
            return 1;
119
        }
120
    }
121
 
122
    private function _comparison($compare)
123
    {
124
        if ($compare > 0) {
125
            return 'less than';
126
        } elseif ($compare == 0) {
127
            return 'equal to';
128
        } else {
129
            return 'greater than';
130
        }
131
    }
132
}