Test Failed
Push — master ( ef12af...52cb59 )
by Vincent
06:49
created

StringErrorPrinter::field()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 0
c 0
b 0
f 0
dl 0
loc 2
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Bdf\Form\Error;
4
5
use Bdf\Form\Child\Http\HttpFieldPath;
6
7
/**
8
 * Format errors as a string
9
 */
10
final class StringErrorPrinter implements FormErrorPrinterInterface
11
{
12
    /**
13
     * @var string
14
     */
15
    private $lineSeparator = PHP_EOL;
16
17
    /**
18
     * @var string
19
     */
20
    private $indentString = '  ';
21
22
    /**
23
     * @var string
24
     */
25
    private $nameSeparator = ' : ';
26
27
    /**
28
     * @var int
29
     */
30
    private $maxDepth = PHP_INT_MAX;
31
32
    /**
33
     * @var integer
34
     */
35
    private $depth = 0;
36
37
    /**
38
     * @var string
39
     */
40
    private $output = '';
41
42
    /**
43 10
     * {@inheritdoc}
44
     */
45 10
    public function field(HttpFieldPath $field): void
46 10
    {
47
        // Ignore the field name
48
    }
49
50
    /**
51 9
     * {@inheritdoc}
52
     */
53
    public function global(string $error): void
54 9
    {
55
        $this->output .= $error;
56
    }
57
58
    /**
59 9
     * {@inheritdoc}
60
     */
61 9
    public function code(string $code): void
62 1
    {
63
        // Ignore code
64
    }
65 9
66 6
    /**
67
     * {@inheritdoc}
68
     */
69 9
    public function child(string $name, FormError $error): void
70
    {
71 9
        if ($this->maxDepth <= $this->depth) {
72 9
            return;
73 9
        }
74 9
75
        if (!empty($this->output)) {
76
            $this->output .= $this->lineSeparator;
77
        }
78
79 10
        $this->output .= str_repeat($this->indentString, $this->depth).$name.$this->nameSeparator;
80
81 10
        ++$this->depth;
82
        $error->print($this);
83
        --$this->depth;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function print()
90
    {
91 1
        return $this->output;
92
    }
93 1
94
    /**
95 1
     * Define the end of line string for separation children
96
     *
97
     * @param string $lineSeparator
98
     *
99
     * @return StringErrorPrinter
100
     */
101
    public function lineSeparator(string $lineSeparator): StringErrorPrinter
102
    {
103
        $this->lineSeparator = $lineSeparator;
104
105 1
        return $this;
106
    }
107 1
108
    /**
109 1
     * Define the indentation string
110
     *
111
     * @param string $indentString
112
     *
113
     * @return StringErrorPrinter
114
     */
115
    public function indentString(string $indentString): StringErrorPrinter
116
    {
117
        $this->indentString = $indentString;
118
119 1
        return $this;
120
    }
121 1
122
    /**
123 1
     * Define the separator between the child name and its error
124
     *
125
     * @param string $nameSeparator
126
     *
127
     * @return StringErrorPrinter
128
     */
129
    public function nameSeparator(string $nameSeparator): StringErrorPrinter
130
    {
131
        $this->nameSeparator = $nameSeparator;
132
133 1
        return $this;
134
    }
135 1
136
    /**
137 1
     * Define the max depth
138
     *
139
     * @param int $maxDepth
140
     *
141
     * @return StringErrorPrinter
142
     */
143
    public function maxDepth(int $maxDepth): StringErrorPrinter
144
    {
145
        $this->maxDepth = $maxDepth;
146
147
        return $this;
148
    }
149
}
150