StringErrorPrinter::lineSeparator()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
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
     * {@inheritdoc}
44
     */
45 8
    public function field(HttpFieldPath $field): void
46
    {
47
        // Ignore the field name
48 8
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 10
    public function global(string $error): void
54
    {
55 10
        $this->output .= $error;
56 10
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 9
    public function code(string $code): void
62
    {
63
        // Ignore code
64 9
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 9
    public function child(string $name, FormError $error): void
70
    {
71 9
        if ($this->maxDepth <= $this->depth) {
72 1
            return;
73
        }
74
75 9
        if (!empty($this->output)) {
76 6
            $this->output .= $this->lineSeparator;
77
        }
78
79 9
        $this->output .= str_repeat($this->indentString, $this->depth).$name.$this->nameSeparator;
80
81 9
        ++$this->depth;
82 9
        $error->print($this);
83 9
        --$this->depth;
84 9
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 10
    public function print()
90
    {
91 10
        return $this->output;
92
    }
93
94
    /**
95
     * Define the end of line string for separation children
96
     *
97
     * @param string $lineSeparator
98
     *
99
     * @return StringErrorPrinter
100
     */
101 1
    public function lineSeparator(string $lineSeparator): StringErrorPrinter
102
    {
103 1
        $this->lineSeparator = $lineSeparator;
104
105 1
        return $this;
106
    }
107
108
    /**
109
     * Define the indentation string
110
     *
111
     * @param string $indentString
112
     *
113
     * @return StringErrorPrinter
114
     */
115 1
    public function indentString(string $indentString): StringErrorPrinter
116
    {
117 1
        $this->indentString = $indentString;
118
119 1
        return $this;
120
    }
121
122
    /**
123
     * Define the separator between the child name and its error
124
     *
125
     * @param string $nameSeparator
126
     *
127
     * @return StringErrorPrinter
128
     */
129 1
    public function nameSeparator(string $nameSeparator): StringErrorPrinter
130
    {
131 1
        $this->nameSeparator = $nameSeparator;
132
133 1
        return $this;
134
    }
135
136
    /**
137
     * Define the max depth
138
     *
139
     * @param int $maxDepth
140
     *
141
     * @return StringErrorPrinter
142
     */
143 1
    public function maxDepth(int $maxDepth): StringErrorPrinter
144
    {
145 1
        $this->maxDepth = $maxDepth;
146
147 1
        return $this;
148
    }
149
}
150