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\Collection;
3
 
4
/*
5
 Copyright (c) 2009 hamcrest.org
6
 */
7
use Hamcrest\BaseMatcher;
8
use Hamcrest\Description;
9
 
10
/**
11
 * Matches if traversable is empty or non-empty.
12
 */
13
class IsEmptyTraversable extends BaseMatcher
14
{
15
 
16
    private static $_INSTANCE;
17
    private static $_NOT_INSTANCE;
18
 
19
    private $_empty;
20
 
21
    public function __construct($empty = true)
22
    {
23
        $this->_empty = $empty;
24
    }
25
 
26
    public function matches($item)
27
    {
28
        if (!$item instanceof \Traversable) {
29
            return false;
30
        }
31
 
32
        foreach ($item as $value) {
33
            return !$this->_empty;
34
        }
35
 
36
        return $this->_empty;
37
    }
38
 
39
    public function describeTo(Description $description)
40
    {
41
        $description->appendText($this->_empty ? 'an empty traversable' : 'a non-empty traversable');
42
    }
43
 
44
    /**
45
     * Returns true if traversable is empty.
46
     *
47
     * @factory
48
     */
49
    public static function emptyTraversable()
50
    {
51
        if (!self::$_INSTANCE) {
52
            self::$_INSTANCE = new self;
53
        }
54
 
55
        return self::$_INSTANCE;
56
    }
57
 
58
    /**
59
     * Returns true if traversable is not empty.
60
     *
61
     * @factory
62
     */
63
    public static function nonEmptyTraversable()
64
    {
65
        if (!self::$_NOT_INSTANCE) {
66
            self::$_NOT_INSTANCE = new self(false);
67
        }
68
 
69
        return self::$_NOT_INSTANCE;
70
    }
71
}