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;
3
 
4
/*
5
 Copyright (c) 2009 hamcrest.org
6
 */
7
use Hamcrest\Internal\SelfDescribingValue;
8
 
9
/**
10
 * A {@link Hamcrest\Description} that is stored as a string.
11
 */
12
abstract class BaseDescription implements Description
13
{
14
 
15
    public function appendText($text)
16
    {
17
        $this->append($text);
18
 
19
        return $this;
20
    }
21
 
22
    public function appendDescriptionOf(SelfDescribing $value)
23
    {
24
        $value->describeTo($this);
25
 
26
        return $this;
27
    }
28
 
29
    public function appendValue($value)
30
    {
31
        if (is_null($value)) {
32
            $this->append('null');
33
        } elseif (is_string($value)) {
34
            $this->_toPhpSyntax($value);
35
        } elseif (is_float($value)) {
36
            $this->append('<');
37
            $this->append($value);
38
            $this->append('F>');
39
        } elseif (is_bool($value)) {
40
            $this->append('<');
41
            $this->append($value ? 'true' : 'false');
42
            $this->append('>');
43
        } elseif (is_array($value) || $value instanceof \Iterator || $value instanceof \IteratorAggregate) {
44
            $this->appendValueList('[', ', ', ']', $value);
45
        } elseif (is_object($value) && !method_exists($value, '__toString')) {
46
            $this->append('<');
47
            $this->append(get_class($value));
48
            $this->append('>');
49
        } else {
50
            $this->append('<');
51
            $this->append($value);
52
            $this->append('>');
53
        }
54
 
55
        return $this;
56
    }
57
 
58
    public function appendValueList($start, $separator, $end, $values)
59
    {
60
        $list = array();
61
        foreach ($values as $v) {
62
            $list[] = new SelfDescribingValue($v);
63
        }
64
 
65
        $this->appendList($start, $separator, $end, $list);
66
 
67
        return $this;
68
    }
69
 
70
    public function appendList($start, $separator, $end, $values)
71
    {
72
        $this->append($start);
73
 
74
        $separate = false;
75
 
76
        foreach ($values as $value) {
77
            /*if (!($value instanceof Hamcrest\SelfDescribing)) {
78
                $value = new Hamcrest\Internal\SelfDescribingValue($value);
79
            }*/
80
 
81
            if ($separate) {
82
                $this->append($separator);
83
            }
84
 
85
            $this->appendDescriptionOf($value);
86
 
87
            $separate = true;
88
        }
89
 
90
        $this->append($end);
91
 
92
        return $this;
93
    }
94
 
95
    // -- Protected Methods
96
 
97
    /**
98
     * Append the String <var>$str</var> to the description.
99
     */
100
    abstract protected function append($str);
101
 
102
    // -- Private Methods
103
 
104
    private function _toPhpSyntax($value)
105
    {
106
        $str = '"';
107
        for ($i = 0, $len = strlen($value); $i < $len; ++$i) {
108
            switch ($value[$i]) {
109
                case '"':
110
                    $str .= '\\"';
111
                    break;
112
 
113
                case "\t":
114
                    $str .= '\\t';
115
                    break;
116
 
117
                case "\r":
118
                    $str .= '\\r';
119
                    break;
120
 
121
                case "\n":
122
                    $str .= '\\n';
123
                    break;
124
 
125
                default:
126
                    $str .= $value[$i];
127
            }
128
        }
129
        $str .= '"';
130
        $this->append($str);
131
    }
132
}