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\Arrays;
3
 
4
/*
5
 Copyright (c) 2009 hamcrest.org
6
 */
7
 
8
// NOTE: This class is not exactly a direct port of Java's since Java handles
9
//       arrays quite differently than PHP
10
 
11
// TODO: Allow this to take matchers or values within the array
12
use Hamcrest\Description;
13
use Hamcrest\TypeSafeMatcher;
14
use Hamcrest\Util;
15
 
16
/**
17
 * Matcher for array whose elements satisfy a sequence of matchers.
18
 * The array size must equal the number of element matchers.
19
 */
20
class IsArray extends TypeSafeMatcher
21
{
22
 
23
    private $_elementMatchers;
24
 
25
    public function __construct(array $elementMatchers)
26
    {
27
        parent::__construct(self::TYPE_ARRAY);
28
 
29
        Util::checkAllAreMatchers($elementMatchers);
30
 
31
        $this->_elementMatchers = $elementMatchers;
32
    }
33
 
34
    protected function matchesSafely($array)
35
    {
36
        if (array_keys($array) != array_keys($this->_elementMatchers)) {
37
            return false;
38
        }
39
 
40
        /** @var $matcher \Hamcrest\Matcher */
41
        foreach ($this->_elementMatchers as $k => $matcher) {
42
            if (!$matcher->matches($array[$k])) {
43
                return false;
44
            }
45
        }
46
 
47
        return true;
48
    }
49
 
50
    protected function describeMismatchSafely($actual, Description $mismatchDescription)
51
    {
52
        if (count($actual) != count($this->_elementMatchers)) {
53
            $mismatchDescription->appendText('array length was ' . count($actual));
54
 
55
            return;
56
        } elseif (array_keys($actual) != array_keys($this->_elementMatchers)) {
57
            $mismatchDescription->appendText('array keys were ')
58
                                                    ->appendValueList(
59
                                                        $this->descriptionStart(),
60
                                                        $this->descriptionSeparator(),
61
                                                        $this->descriptionEnd(),
62
                                                        array_keys($actual)
63
                                                    )
64
                                                    ;
65
 
66
            return;
67
        }
68
 
69
        /** @var $matcher \Hamcrest\Matcher */
70
        foreach ($this->_elementMatchers as $k => $matcher) {
71
            if (!$matcher->matches($actual[$k])) {
72
                $mismatchDescription->appendText('element ')->appendValue($k)
73
                    ->appendText(' was ')->appendValue($actual[$k]);
74
 
75
                return;
76
            }
77
        }
78
    }
79
 
80
    public function describeTo(Description $description)
81
    {
82
        $description->appendList(
83
            $this->descriptionStart(),
84
            $this->descriptionSeparator(),
85
            $this->descriptionEnd(),
86
            $this->_elementMatchers
87
        );
88
    }
89
 
90
    /**
91
     * Evaluates to true only if each $matcher[$i] is satisfied by $array[$i].
92
     *
93
     * @factory ...
94
     */
95
    public static function anArray(/* args... */)
96
    {
97
        $args = func_get_args();
98
 
99
        return new self(Util::createMatcherArray($args));
100
    }
101
 
102
    // -- Protected Methods
103
 
104
    protected function descriptionStart()
105
    {
106
        return '[';
107
    }
108
 
109
    protected function descriptionSeparator()
110
    {
111
        return ', ';
112
    }
113
 
114
    protected function descriptionEnd()
115
    {
116
        return ']';
117
    }
118
}