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\Text;
3
 
4
/*
5
 Copyright (c) 2009 hamcrest.org
6
 */
7
use Hamcrest\BaseMatcher;
8
use Hamcrest\Core\AnyOf;
9
use Hamcrest\Core\IsNull;
10
use Hamcrest\Description;
11
 
12
/**
13
 * Matches empty Strings (and null).
14
 */
15
class IsEmptyString extends BaseMatcher
16
{
17
 
18
    private static $_INSTANCE;
19
    private static $_NULL_OR_EMPTY_INSTANCE;
20
    private static $_NOT_INSTANCE;
21
 
22
    private $_empty;
23
 
24
    public function __construct($empty = true)
25
    {
26
        $this->_empty = $empty;
27
    }
28
 
29
    public function matches($item)
30
    {
31
        return $this->_empty
32
            ? ($item === '')
33
            : is_string($item) && $item !== '';
34
    }
35
 
36
    public function describeTo(Description $description)
37
    {
38
        $description->appendText($this->_empty ? 'an empty string' : 'a non-empty string');
39
    }
40
 
41
    /**
42
     * Matches if value is a zero-length string.
43
     *
44
     * @factory emptyString
45
     */
46
    public static function isEmptyString()
47
    {
48
        if (!self::$_INSTANCE) {
49
            self::$_INSTANCE = new self(true);
50
        }
51
 
52
        return self::$_INSTANCE;
53
    }
54
 
55
    /**
56
     * Matches if value is null or a zero-length string.
57
     *
58
     * @factory nullOrEmptyString
59
     */
60
    public static function isEmptyOrNullString()
61
    {
62
        if (!self::$_NULL_OR_EMPTY_INSTANCE) {
63
            self::$_NULL_OR_EMPTY_INSTANCE = AnyOf::anyOf(
64
                IsNull::nullvalue(),
65
                self::isEmptyString()
66
            );
67
        }
68
 
69
        return self::$_NULL_OR_EMPTY_INSTANCE;
70
    }
71
 
72
    /**
73
     * Matches if value is a non-zero-length string.
74
     *
75
     * @factory nonEmptyString
76
     */
77
    public static function isNonEmptyString()
78
    {
79
        if (!self::$_NOT_INSTANCE) {
80
            self::$_NOT_INSTANCE = new self(false);
81
        }
82
 
83
        return self::$_NOT_INSTANCE;
84
    }
85
}