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
 
10
class MatchingOnce
11
{
12
 
13
    private $_elementMatchers;
14
    private $_mismatchDescription;
15
 
16
    public function __construct(array $elementMatchers, Description $mismatchDescription)
17
    {
18
        $this->_elementMatchers = $elementMatchers;
19
        $this->_mismatchDescription = $mismatchDescription;
20
    }
21
 
22
    public function matches($item)
23
    {
24
        return $this->_isNotSurplus($item) && $this->_isMatched($item);
25
    }
26
 
27
    public function isFinished($items)
28
    {
29
        if (empty($this->_elementMatchers)) {
30
            return true;
31
        }
32
 
33
        $this->_mismatchDescription
34
                 ->appendText('No item matches: ')->appendList('', ', ', '', $this->_elementMatchers)
35
                 ->appendText(' in ')->appendValueList('[', ', ', ']', $items)
36
                 ;
37
 
38
        return false;
39
    }
40
 
41
    // -- Private Methods
42
 
43
    private function _isNotSurplus($item)
44
    {
45
        if (empty($this->_elementMatchers)) {
46
            $this->_mismatchDescription->appendText('Not matched: ')->appendValue($item);
47
 
48
            return false;
49
        }
50
 
51
        return true;
52
    }
53
 
54
    private function _isMatched($item)
55
    {
56
            /** @var $matcher \Hamcrest\Matcher */
57
        foreach ($this->_elementMatchers as $i => $matcher) {
58
            if ($matcher->matches($item)) {
59
                unset($this->_elementMatchers[$i]);
60
 
61
                return true;
62
            }
63
        }
64
 
65
        $this->_mismatchDescription->appendText('Not matched: ')->appendValue($item);
66
 
67
        return false;
68
    }
69
}