FailedMessage::concat()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 8
rs 9.4285
1
<?php
2
3
/**
4
 * This file is part of expect package.
5
 *
6
 * (c) Noritaka Horio <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
namespace expect;
12
13
/**
14
 * Message when validation fails.
15
 *
16
 * @author Noritaka Horio <[email protected]>
17
 * @copyright Noritaka Horio <[email protected]>
18
 */
19
class FailedMessage implements Message
20
{
21
    /**
22
     * @var string
23
     */
24
    private $message;
25
26
    /**
27
     * @param string $message
28
     */
29
    public function __construct($message = '')
30
    {
31
        $this->message = $message;
32
    }
33
34
    /**
35
     * Append the text to the last.
36
     *
37
     * @param mixed $value
38
     *
39
     * @return $this
40
     */
41
    public function appendText($value)
42
    {
43
        $text = $this->stringify($value);
44
        $this->message = $this->message . $text;
45
46
        return $this;
47
    }
48
49
    /**
50
     * Append the length space.
51
     *
52
     * @param int $length
53
     *
54
     * @return $this
55
     */
56
    public function appendSpace($length)
57
    {
58
        $paddingLength = (int) $length;
59
        $this->message = $this->message . str_pad('', $paddingLength, ' ');
60
61
        return $this;
62
    }
63
64
    /**
65
     * Append the value to the last.
66
     *
67
     * @param mixed $value
68
     *
69
     * @return $this
70
     */
71
    public function appendValue($value)
72
    {
73
        $appendValue = $this->formatValue($value);
74
        $this->message = $this->message . $appendValue;
75
76
        return $this;
77
    }
78
79
    /**
80
     * Append the values to the last.
81
     *
82
     * @param array $values
83
     *
84
     * @return $this
85
     */
86
    public function appendValues(array $values)
87
    {
88
        $appendValues = [];
89
90
        foreach ($values as $value) {
91
            $appendValues[] = $this->formatValue($value);
92
        }
93
94
        $this->message = $this->message . implode(', ', $appendValues);
95
96
        return $this;
97
    }
98
99
    public function concat(FailedMessage $message)
100
    {
101
        $prefix = (string) $this;
102
        $suffix = (string) $message;
103
        $concatenatedMessage = trim($prefix) . "\n" . trim($suffix);
104
105
        return static::fromString($concatenatedMessage);
106
    }
107
108
    public static function fromString($value)
109
    {
110
        return new self($value);
111
    }
112
113
    private function boolToString($value)
114
    {
115
        return $value === true ? 'true' : 'false';
116
    }
117
118
    public function __toString()
119
    {
120
        return $this->message;
121
    }
122
123
    private function formatValue($value)
124
    {
125
        $text = $this->stringify($value);
126
127
        if (is_string($value)) {
128
            $text = "'" . $text . "'";
129
        }
130
131
        return $text;
132
    }
133
134
    private function stringify($value)
135
    {
136
        $appendValue = $value;
137
138
        if (is_null($value)) {
139
            $appendValue = 'null';
140
        } else if (is_bool($value)) {
141
            $appendValue = $this->boolToString($value);
142
        } else if (is_string($value) === false) {
143
            $appendValue = rtrim(print_r($value, true));
144
        }
145
146
        return $appendValue;
147
    }
148
}
149