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
use Hamcrest\Description;
9
use Hamcrest\Matcher;
10
 
11
class SeriesMatchingOnce
12
{
13
 
14
    private $_elementMatchers;
15
    private $_keys;
16
    private $_mismatchDescription;
17
    private $_nextMatchKey;
18
 
19
    public function __construct(array $elementMatchers, Description $mismatchDescription)
20
    {
21
        $this->_elementMatchers = $elementMatchers;
22
        $this->_keys = array_keys($elementMatchers);
23
        $this->_mismatchDescription = $mismatchDescription;
24
    }
25
 
26
    public function matches($item)
27
    {
28
        return $this->_isNotSurplus($item) && $this->_isMatched($item);
29
    }
30
 
31
    public function isFinished()
32
    {
33
        if (!empty($this->_elementMatchers)) {
34
            $nextMatcher = current($this->_elementMatchers);
35
            $this->_mismatchDescription->appendText('No item matched: ')->appendDescriptionOf($nextMatcher);
36
 
37
            return false;
38
        }
39
 
40
        return true;
41
    }
42
 
43
    // -- Private Methods
44
 
45
    private function _isNotSurplus($item)
46
    {
47
        if (empty($this->_elementMatchers)) {
48
            $this->_mismatchDescription->appendText('Not matched: ')->appendValue($item);
49
 
50
            return false;
51
        }
52
 
53
        return true;
54
    }
55
 
56
    private function _isMatched($item)
57
    {
58
        $this->_nextMatchKey = array_shift($this->_keys);
59
        $nextMatcher = array_shift($this->_elementMatchers);
60
 
61
        if (!$nextMatcher->matches($item)) {
62
            $this->_describeMismatch($nextMatcher, $item);
63
 
64
            return false;
65
        }
66
 
67
        return true;
68
    }
69
 
70
    private function _describeMismatch(Matcher $matcher, $item)
71
    {
72
        $this->_mismatchDescription->appendText('item with key ' . $this->_nextMatchKey . ': ');
73
        $matcher->describeMismatch($item, $this->_mismatchDescription);
74
    }
75
}