AbstractElementBuilder::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

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 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Bdf\Form;
4
5
use Bdf\Form\Registry\Registry;
6
use Bdf\Form\Registry\RegistryInterface;
7
use Bdf\Form\Transformer\TransformerInterface;
8
use Bdf\Form\Util\TransformerBuilderTrait;
9
use Bdf\Form\Util\ValidatorBuilderTrait;
10
use Bdf\Form\Validator\ValueValidatorInterface;
11
12
/**
13
 * Base builder for elements
14
 *
15
 * @template E as ElementInterface
16
 * @implements ElementBuilderInterface<E>
17
 */
18
abstract class AbstractElementBuilder implements ElementBuilderInterface
19
{
20
    use TransformerBuilderTrait;
21
    use ValidatorBuilderTrait;
22
23
    /**
24
     * @var RegistryInterface
25
     */
26
    private $registry;
27
28
    /**
29
     * @var mixed
30
     */
31
    private $value;
32
33
34
    /**
35
     * ElementBuilder constructor.
36
     *
37
     * @param RegistryInterface|null $registry
38
     */
39 383
    public function __construct(RegistryInterface $registry = null)
40
    {
41 383
        $this->registry = $registry ?: new Registry();
42 383
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 17
    final public function value($value)
48
    {
49 17
        $this->value = $value;
50
51 17
        return $this;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 379
    final public function buildElement(): ElementInterface
58
    {
59 379
        $element = $this->createElement($this->buildValidator(), $this->buildTransformer());
60
61 379
        if ($this->value !== null) {
62 17
            $element->import($this->value);
63
        }
64
65 379
        return $element;
66
    }
67
68
    /**
69
     * Creates the element
70
     *
71
     * @param ValueValidatorInterface $validator
72
     * @param TransformerInterface $transformer
73
     *
74
     * @return E
75
     */
76
    abstract protected function createElement(ValueValidatorInterface $validator, TransformerInterface $transformer): ElementInterface;
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 379
    final protected function registry(): RegistryInterface
82
    {
83 379
        return $this->registry;
84
    }
85
}
86