RendersOptionsTrait::createOption()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace WebTheory\Saveyour\Field\Abstracts;
4
5
use WebTheory\Saveyour\Contracts\Field\Selection\OptionsProviderInterface;
6
use WebTheory\Saveyour\Field\Element\Option;
7
8
trait RendersOptionsTrait
9
{
10
    use SelectionFieldTrait;
11
12
    protected OptionsProviderInterface $selectionProvider;
13
14
    /**
15
     * Get the value of selectionProvider
16
     *
17
     * @return OptionsProviderInterface
18
     */
19
    public function getSelectionProvider(): OptionsProviderInterface
20
    {
21
        return $this->selectionProvider;
22
    }
23
24
    /**
25
     * Set the value of selectionProvider
26
     *
27
     * @param OptionsProviderInterface $selectionProvider
28
     *
29
     * @return $this
30
     */
31
    public function setSelectionProvider(OptionsProviderInterface $selectionProvider)
32
    {
33
        $this->selectionProvider = $selectionProvider;
34
35
        return $this;
36
    }
37
38
    protected function defineSelectionText($selection): string
39
    {
40
        return $this->selectionProvider->defineSelectionText($selection);
41
    }
42
43
    protected function renderSelection(): string
44
    {
45
        $html = '';
46
47
        foreach ($this->getSelectionData() as $selection) {
48
            $selected = $this->isSelectionSelected($this->defineSelectionValue($selection));
49
50
            $html .= $this->createOption($selection)->setSelected($selected);
51
        }
52
53
        return $html;
54
    }
55
56
    protected function createOption($selection): Option
57
    {
58
        return new Option(
59
            $this->defineSelectionText($selection),
60
            $this->defineSelectionValue($selection)
61
        );
62
    }
63
64
    abstract protected function isSelectionSelected(string $value): bool;
65
}
66