Passed
Push — main ( d01439...b93b89 )
by Nils
02:46
created

Parameter::isDefaultForced()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Startwind\Forrest\Command\Parameters;
4
5
use Startwind\Forrest\Command\Parameters\Validation\Constraint\NotEmptyConstraint;
6
use Startwind\Forrest\Command\Parameters\Validation\SuccessfulValidationResult;
7
use Startwind\Forrest\Command\Parameters\Validation\ValidationResult;
8
use Startwind\Forrest\Enrichment\EnrichFunction\Explode\FunctionComposite;
9
10
class Parameter implements \JsonSerializable
11
{
12
    public const PARAMETER_PREFIX = '${';
13
    public const PARAMETER_POSTFIX = '}';
14
15
    public const ENUM_CUSTOM_KEY = '##custom##';
16
    public const ENUM_CUSTOM = '<custom value>';
17
18
    public const TYPE = 'mixed';
19
20
    private string $name = '';
21
    private string $description = '';
22
23
    private string $prefix = '';
24
    private string $suffix = '';
25
26
    private bool $optional = false;
27
    private string $defaultValue = '';
28
29
    private array $values = [];
30
31
    private array $rawStructure = [];
32
33
    private array $constraints = [
34
        NotEmptyConstraint::class
35
    ];
36
37
    protected bool $forceDefault = false;
38
39
    /**
40
     * @param array $rawStructure
41
     */
42
    public function setRawStructure(array $rawStructure): void
43
    {
44
        $this->rawStructure = $rawStructure;
45
    }
46
47
    public function isDefaultForced(): bool
48
    {
49
        return $this->forceDefault;
50
    }
51
52
    public function setForceDefault(bool $forceDefault): void
53
    {
54
        $this->forceDefault = $forceDefault;
55
    }
56
57
    public function setName(string $name): void
58
    {
59
        $this->name = $name;
60
    }
61
62
    public function setDescription(string $description): void
63
    {
64
        $this->description = $description;
65
    }
66
67
    public function getDescription(): string
68
    {
69
        return $this->description;
70
    }
71
72
    public function getName(): string
73
    {
74
        return $this->name;
75
    }
76
77
    public function getDefaultValue(): string
78
    {
79
        return $this->defaultValue;
80
    }
81
82
    public function setDefaultValue(string $defaultValue): void
83
    {
84
        $this->defaultValue = $defaultValue;
85
    }
86
87
    public function getValues(): array
88
    {
89
        return $this->values;
90
    }
91
92
    public function setValues(array|string $values, bool $allowCustomValue): void
93
    {
94
        if (is_array($values)) {
0 ignored issues
show
introduced by
The condition is_array($values) is always true.
Loading history...
95
            $this->values = $values;
96
        } else {
97
            $explodeFunctionComposite = new FunctionComposite();
98
            $this->values = $explodeFunctionComposite->applyFunction($values);
99
        }
100
101
        if ($allowCustomValue) {
102
            if (array_is_list($this->values)) {
103
                array_unshift($this->values, self::ENUM_CUSTOM);
104
            } else {
105
                $this->values[self::ENUM_CUSTOM_KEY] = self::ENUM_CUSTOM;
106
            }
107
        }
108
    }
109
110
    public function hasValues(): bool
111
    {
112
        return count($this->values) > 0;
113
    }
114
115
    public function getType(): string
116
    {
117
        return self::TYPE;
118
    }
119
120
    public function setConstraints(array $constraints): void
121
    {
122
        $this->constraints = $constraints;
123
    }
124
125
    /**
126
     * Check if the given value is valid.
127
     */
128
    public function validate(string $value): ValidationResult
129
    {
130
        if ($value == '' && $this->optional) {
131
            return new SuccessfulValidationResult();
132
        }
133
134
        foreach ($this->constraints as $constraint) {
135
            /** @var ValidationResult $constraintValidationResult */
136
            $constraintValidationResult = (call_user_func([$constraint, 'validate'], $value));
137
            if (!$constraintValidationResult->isValid()) {
138
                return $constraintValidationResult;
139
            }
140
        }
141
        return new SuccessfulValidationResult();
142
    }
143
144
    public function getPrefix(): string
145
    {
146
        return $this->prefix;
147
    }
148
149
    public function setPrefix(string $prefix): void
150
    {
151
        $this->prefix = $prefix;
152
    }
153
154
    public function getSuffix(): string
155
    {
156
        return $this->suffix;
157
    }
158
159
    public function setSuffix(string $suffix): void
160
    {
161
        $this->suffix = $suffix;
162
    }
163
164
    public function isOptional(): bool
165
    {
166
        return $this->optional;
167
    }
168
169
    public function setOptional(bool $optional): void
170
    {
171
        $this->optional = $optional;
172
    }
173
174
    public function jsonSerialize(): array
175
    {
176
        return $this->rawStructure;
177
    }
178
}
179