Completed
Push — master ( 53823c...8f6bb6 )
by
unknown
13s
created

OtherChoiceOption::transformToActualChoiceOption()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace SumoCoders\FrameworkCoreBundle\Entity;
4
5
use Doctrine\ORM\Mapping as ORM;
6
use InvalidArgumentException;
7
use RuntimeException;
8
9
/**
10
 * @ORM\Entity(repositoryClass="SumoCoders\FrameworkCoreBundle\Repository\OtherChoiceOptionRepository")
11
 * @ORM\Table(
12
 *     name="OtherChoiceOption",
13
 *     indexes={@ORM\Index(name="category_index", columns={"category"})},
14
 *     uniqueConstraints={@ORM\UniqueConstraint(name="unique_name_index", columns={"category", "label"})}
15
 * )
16
 */
17
class OtherChoiceOption
18
{
19
    /**
20
     * @var string
21
     *
22
     * @ORM\Column(type="guid")
23
     * @ORM\Id
24
     * @ORM\GeneratedValue(strategy="UUID")
25
     */
26
    private $id;
27
28
    /**
29
     * @var string
30
     *
31
     * @ORM\Column(type="string")
32
     */
33
    private $category;
34
35
    /**
36
     * @var string
37
     *
38
     * @ORM\Column(type="string")
39
     */
40
    private $label;
41
42
    /**
43
     * @var string
44
     *
45
     * @ORM\Column(type="string", length=2)
46
     */
47
    private $locale;
48
49
    /**
50
     * Used to create new choice options in the form type
51
     *
52
     * @var string
53
     */
54
    private $newChoiceOptionCategory;
55
56
    public function __construct(string $category, string $label, string $locale)
57
    {
58
        if ($category === 'other') {
59
            throw new InvalidArgumentException('The category "other" is reserved');
60
        }
61
62
        $this->category = $category;
63
        $this->label = $label;
64
        $this->locale = $locale;
65
    }
66
67
    public function getId(): string
68
    {
69
        return $this->id;
70
    }
71
72
    public function getCategory(): string
73
    {
74
        return $this->category;
75
    }
76
77
    public function getValue(): string
78
    {
79
        if ($this->category === 'other') {
80
            return $this->category;
81
        }
82
83
        return $this->label;
84
    }
85
86
    public function getLabel(): string
87
    {
88
        return $this->label;
89
    }
90
91
    public function getLocale(): string
92
    {
93
        return $this->locale;
94
    }
95
96
    public function __toString(): string
97
    {
98
        return $this->label;
99
    }
100
101
    public static function getOtherOption(string $label, string $locale): self
102
    {
103
        $otherOption = new self('tmp', $label, $locale);
104
        // hack to be able to set other while it is restricted;
105
        $otherOption->category = 'other';
106
107
        return $otherOption;
108
    }
109
110
    public function getWithNewChoiceOptionCategory(string $newChoiceOptionCategory): self
111
    {
112
        if ($this->category !== 'other') {
113
            throw new RuntimeException('This method can only be called when the category is other');
114
        }
115
116
        $choiceOption = clone $this;
117
        $choiceOption->newChoiceOptionCategory = $newChoiceOptionCategory;
118
119
        return $choiceOption;
120
    }
121
122
    /**
123
     * This method is used to transform an other choice option into the actual choice option
124
     *
125
     * @param string $label
126
     */
127
    public function transformToActualChoiceOption(string $label): void
128
    {
129
        if ($this->category !== 'other') {
130
            throw new RuntimeException('This method can only be called when the category is other');
131
        }
132
133
        $this->category = $this->newChoiceOptionCategory;
134
        $this->label = $label;
135
        $this->newChoiceOptionCategory = null;
136
    }
137
}
138