ArrayElementView::defaultRenderer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Bdf\Form\Aggregate\View;
4
5
use Bdf\Form\Aggregate\ArrayElement;
6
use Bdf\Form\Choice\ChoiceView;
7
use Bdf\Form\View\ElementViewInterface;
8
use Bdf\Form\View\ElementViewTrait;
9
use Bdf\Form\View\FieldSetViewInterface;
10
use Bdf\Form\View\FieldSetViewTrait;
11
use Bdf\Form\View\FieldViewInterface;
12
use Bdf\Form\View\FieldViewRendererInterface;
13
use Bdf\Form\View\FieldViewTrait;
14
use Countable;
15
use IteratorAggregate;
16
17
/**
18
 * View object for the ArrayElement
19
 *
20
 * <code>
21
 *  <!-- Array of elements : use as array -->
22
 *  <?php if (count($view) === 0): ?>
23
 *      <span>There is no elements</span>
24
 *  <?php endif; ?>
25
 *  <?php foreach ($view as $item): ?>
26
 *      <?php echo $item->class('form-control'); ?>
27
 *  <?php endforeach; ?>
28
 *
29
 *  <!-- CSV element : use as simple element -->
30
 *  <?php echo $view->class('form-control'); ?>
31
 * </code>
32
 *
33
 * @see ArrayElement::view()
34
 */
35
final class ArrayElementView implements IteratorAggregate, FieldViewInterface, FieldSetViewInterface, Countable
36
{
37
    use ElementViewTrait;
38
    use FieldViewTrait;
39
    use FieldSetViewTrait {
40
        FieldSetViewTrait::hasError insteadof ElementViewTrait;
41
    }
42
43
    /**
44
     * ArrayElementView constructor.
45
     *
46
     * @param string $type
47
     * @param string $name
48
     * @param mixed $value
49
     * @param string|null $error
50
     * @param ElementViewInterface[] $elements
51
     * @param bool $required
52
     * @param array $constraints
53
     * @param ChoiceView[]|null $choices
54
     */
55 32
    public function __construct(string $type, string $name, $value, ?string $error, array $elements, bool $required, array $constraints, ?array $choices = [])
56
    {
57 32
        $this->type = $type;
58 32
        $this->name = $name;
59 32
        $this->error = $error;
60 32
        $this->value = $value;
61 32
        $this->elements = $elements;
62 32
        $this->required = $required;
63 32
        $this->constraints = $constraints;
64 32
        $this->choices = $choices;
65 32
    }
66
67
    /**
68
     * Check if the current element value is a CSV
69
     * If true, the element can be used as simple HTTP field
70
     *
71
     * @return bool
72
     */
73 17
    public function isCsv(): bool
74
    {
75 17
        return is_scalar($this->value);
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 4
    public function count(): int
82
    {
83 4
        return count($this->elements);
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 3
    protected function defaultRenderer(): FieldViewRendererInterface
90
    {
91 3
        return ArrayElementViewRenderer::instance();
92
    }
93
94
    /**
95
     * Ignore property "attributes"
96
     *
97
     * @return array
98
     */
99 1
    public function __sleep()
100
    {
101 1
        return ['type', 'name', 'error', 'value', 'elements', 'required', 'constraints', 'choices'];
102
    }
103
}
104