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\Description;
8
use Hamcrest\TypeSafeMatcher;
9
 
10
/**
11
 * Tests if a string is equal to another string, ignoring any changes in
12
 * whitespace.
13
 */
14
class IsEqualIgnoringWhiteSpace extends TypeSafeMatcher
15
{
16
 
17
    private $_string;
18
 
19
    public function __construct($string)
20
    {
21
        parent::__construct(self::TYPE_STRING);
22
 
23
        $this->_string = $string;
24
    }
25
 
26
    protected function matchesSafely($item)
27
    {
28
        return (strtolower($this->_stripSpace($item))
29
                === strtolower($this->_stripSpace($this->_string)));
30
    }
31
 
32
    protected function describeMismatchSafely($item, Description $mismatchDescription)
33
    {
34
        $mismatchDescription->appendText('was ')->appendText($item);
35
    }
36
 
37
    public function describeTo(Description $description)
38
    {
39
        $description->appendText('equalToIgnoringWhiteSpace(')
40
                                ->appendValue($this->_string)
41
                                ->appendText(')')
42
                                ;
43
    }
44
 
45
    /**
46
     * Matches if value is a string equal to $string, regardless of whitespace.
47
     *
48
     * @factory
49
     */
50
    public static function equalToIgnoringWhiteSpace($string)
51
    {
52
        return new self($string);
53
    }
54
 
55
    // -- Private Methods
56
 
57
    private function _stripSpace($string)
58
    {
59
        $parts = preg_split("/[\r\n\t ]+/", $string);
60
        foreach ($parts as $i => $part) {
61
            $parts[$i] = trim($part, " \r\n\t");
62
        }
63
 
64
        return trim(implode(' ', $parts), " \r\n\t");
65
    }
66
}