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
use Hamcrest\Description;
8
use Hamcrest\Matcher;
9
use Hamcrest\TypeSafeMatcher;
10
use Hamcrest\Util;
11
 
12
/**
13
 * Matches if an array contains the specified key.
14
 */
15
class IsArrayContainingKey extends TypeSafeMatcher
16
{
17
 
18
    private $_keyMatcher;
19
 
20
    public function __construct(Matcher $keyMatcher)
21
    {
22
        parent::__construct(self::TYPE_ARRAY);
23
 
24
        $this->_keyMatcher = $keyMatcher;
25
    }
26
 
27
    protected function matchesSafely($array)
28
    {
29
        foreach ($array as $key => $element) {
30
            if ($this->_keyMatcher->matches($key)) {
31
                return true;
32
            }
33
        }
34
 
35
        return false;
36
    }
37
 
38
    protected function describeMismatchSafely($array, Description $mismatchDescription)
39
    {
40
        //Not using appendValueList() so that keys can be shown
41
        $mismatchDescription->appendText('array was ')
42
                                                ->appendText('[')
43
                                                ;
44
        $loop = false;
45
        foreach ($array as $key => $value) {
46
            if ($loop) {
47
                $mismatchDescription->appendText(', ');
48
            }
49
            $mismatchDescription->appendValue($key)->appendText(' => ')->appendValue($value);
50
            $loop = true;
51
        }
52
        $mismatchDescription->appendText(']');
53
    }
54
 
55
    public function describeTo(Description $description)
56
    {
57
        $description
58
                 ->appendText('array with key ')
59
                 ->appendDescriptionOf($this->_keyMatcher)
60
                 ;
61
    }
62
 
63
    /**
64
     * Evaluates to true if any key in an array matches the given matcher.
65
     *
66
     * @param mixed $key as a {@link Hamcrest\Matcher} or a value.
67
     *
68
     * @return \Hamcrest\Arrays\IsArrayContainingKey
69
     * @factory hasKey
70
     */
71
    public static function hasKeyInArray($key)
72
    {
73
        return new self(Util::wrapValueWithIsEqual($key));
74
    }
75
}