• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wol-soft / php-json-schema-model-generator / 14450158551

14 Apr 2025 03:57PM UTC coverage: 98.564% (-0.2%) from 98.813%
14450158551

push

github

web-flow
Merge pull request #90 from szepeviktor/patch-1

Make use of GHA features

3295 of 3343 relevant lines covered (98.56%)

569.28 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.7
/src/Model/GeneratorConfiguration.php
1
<?php
2

3
declare(strict_types = 1);
4

5
namespace PHPModelGenerator\Model;
6

7
use Exception;
8
use PHPModelGenerator\Exception\InvalidFilterException;
9
use PHPModelGenerator\Filter\FilterInterface;
10
use PHPModelGenerator\Filter\TransformingFilterInterface;
11
use PHPModelGenerator\Format\FormatValidatorInterface;
12
use PHPModelGenerator\PropertyProcessor\Filter\DateTimeFilter;
13
use PHPModelGenerator\PropertyProcessor\Filter\NotEmptyFilter;
14
use PHPModelGenerator\PropertyProcessor\Filter\TrimFilter;
15
use PHPModelGenerator\Utils\ClassNameGenerator;
16
use PHPModelGenerator\Utils\ClassNameGeneratorInterface;
17
use PHPModelGenerator\Exception\ErrorRegistryException;
18

19
/**
20
 * Class GeneratorConfiguration
21
 *
22
 * @package PHPModelGenerator\Model
23
 */
24
class GeneratorConfiguration
25
{
26
    /** @var string */
27
    protected $namespacePrefix = '';
28
    /** @var bool */
29
    protected $immutable = true;
30
    /** @var bool */
31
    protected $allowImplicitNull = false;
32
    /** @var bool */
33
    protected $defaultArraysToEmptyArray = false;
34
    /** @var bool */
35
    protected $denyAdditionalProperties = false;
36
    /** @var bool */
37
    protected $outputEnabled = true;
38
    /** @var bool */
39
    protected $collectErrors = true;
40
    /** @var string */
41
    protected $errorRegistryClass = ErrorRegistryException::class;
42
    /** @var bool */
43
    protected $serialization = false;
44

45
    /** @var ClassNameGeneratorInterface */
46
    protected $classNameGenerator;
47

48
    /** @var FilterInterface[] */
49
    protected $filter;
50
    /** @var FormatValidatorInterface[] */
51
    protected $formats;
52

53
    /**
54
     * GeneratorConfiguration constructor.
55
     */
56
    public function __construct()
2,001✔
57
    {
58
        $this->classNameGenerator = new ClassNameGenerator();
2,001✔
59

60
        // add all built-in filter and format validators
61
        $this->initFilter();
2,001✔
62
        $this->initFormatValidator();
2,001✔
63
    }
64

65
    /**
66
     * Add an additional filter
67
     *
68
     * @throws Exception
69
     * @throws InvalidFilterException
70
     */
71
    public function addFilter(FilterInterface ...$additionalFilter): self
2,001✔
72
    {
73
        foreach ($additionalFilter as $filter) {
2,001✔
74
            $this->validateFilterCallback(
2,001✔
75
                $filter->getFilter(),
2,001✔
76
                "Invalid filter callback for filter {$filter->getToken()}",
2,001✔
77
            );
2,001✔
78

79
            if ($filter instanceof TransformingFilterInterface) {
2,001✔
80
                $this->validateFilterCallback(
2,001✔
81
                    $filter->getSerializer(),
2,001✔
82
                    "Invalid serializer callback for filter {$filter->getToken()}"
2,001✔
83
                );
2,001✔
84
            }
85

86
            foreach ($filter->getAcceptedTypes() as $acceptedType) {
2,001✔
87
                if (!in_array($acceptedType, ['integer', 'number', 'boolean', 'string', 'array', 'null']) &&
2,001✔
88
                    !class_exists($acceptedType)
2,001✔
89
                ) {
90
                    throw new InvalidFilterException('Filter accepts invalid types');
1✔
91
                }
92
            }
93

94
            $this->filter[$filter->getToken()] = $filter;
2,001✔
95
        }
96

97
        return $this;
2,001✔
98
    }
99

100
    /**
101
     * Add an additional format
102
     */
103
    public function addFormat(string $formatKey, FormatValidatorInterface $format): self
5✔
104
    {
105
        $this->formats[$formatKey] = $format;
5✔
106

107
        return $this;
5✔
108
    }
109

110
    public function getFormat(string $formatKey): ?FormatValidatorInterface
6✔
111
    {
112
        return $this->formats[$formatKey] ?? null;
6✔
113
    }
114

115
    /**
116
     * @throws InvalidFilterException
117
     */
118
    private function validateFilterCallback(array $callback, string $message): void
2,001✔
119
    {
120
        if (!(count($callback) === 2) ||
2,001✔
121
            !is_string($callback[0]) ||
2,001✔
122
            !is_string($callback[1]) ||
2,001✔
123
            !is_callable($callback)
2,001✔
124
        ) {
125
            throw new InvalidFilterException($message);
14✔
126
        }
127
    }
128

129
    /**
130
     * Get a filter by the given token
131
     */
132
    public function getFilter(string $token): ?FilterInterface
128✔
133
    {
134
        return $this->filter[$token] ?? null;
128✔
135
    }
136

137
    public function getClassNameGenerator(): ClassNameGeneratorInterface
1,966✔
138
    {
139
        return $this->classNameGenerator;
1,966✔
140
    }
141

142
    public function setClassNameGenerator(ClassNameGeneratorInterface $classNameGenerator): self
1,960✔
143
    {
144
        $this->classNameGenerator = $classNameGenerator;
1,960✔
145

146
        return $this;
1,960✔
147
    }
148

149
    public function getNamespacePrefix(): string
1,891✔
150
    {
151
        return $this->namespacePrefix;
1,891✔
152
    }
153

154
    public function setNamespacePrefix(string $namespacePrefix): self
25✔
155
    {
156
        $this->namespacePrefix = trim($namespacePrefix, '\\');
25✔
157

158
        return $this;
25✔
159
    }
160

161
    public function isDefaultArraysToEmptyArrayEnabled(): bool
526✔
162
    {
163
        return $this->defaultArraysToEmptyArray;
526✔
164
    }
165

166
    public function setDefaultArraysToEmptyArray(bool $defaultArraysToEmptyArray): self
5✔
167
    {
168
        $this->defaultArraysToEmptyArray = $defaultArraysToEmptyArray;
5✔
169

170
        return $this;
5✔
171
    }
172

173
    public function isImmutable(): bool
1,903✔
174
    {
175
        return $this->immutable;
1,903✔
176
    }
177

178
    public function setImmutable(bool $immutable): self
151✔
179
    {
180
        $this->immutable = $immutable;
151✔
181

182
        return $this;
151✔
183
    }
184

185
    public function denyAdditionalProperties(): bool
1,830✔
186
    {
187
        return $this->denyAdditionalProperties;
1,830✔
188
    }
189

190
    public function setDenyAdditionalProperties(bool $denyAdditionalProperties): self
10✔
191
    {
192
        $this->denyAdditionalProperties = $denyAdditionalProperties;
10✔
193

194
        return $this;
10✔
195
    }
196

197
    public function hasSerializationEnabled(): bool
1,967✔
198
    {
199
        return $this->serialization;
1,967✔
200
    }
201

202
    public function setSerialization(bool $serialization): self
43✔
203
    {
204
        $this->serialization = $serialization;
43✔
205

206
        return $this;
43✔
207
    }
208

209
    public function setOutputEnabled(bool $outputEnabled): self
1,970✔
210
    {
211
        $this->outputEnabled = $outputEnabled;
1,970✔
212

213
        return $this;
1,970✔
214
    }
215

216
    public function isOutputEnabled(): bool
1,906✔
217
    {
218
        return $this->outputEnabled;
1,906✔
219
    }
220

221
    public function collectErrors(): bool
1,892✔
222
    {
223
        return $this->collectErrors;
1,892✔
224
    }
225

226
    public function setCollectErrors(bool $collectErrors): self
1,218✔
227
    {
228
        $this->collectErrors = $collectErrors;
1,218✔
229

230
        return $this;
1,218✔
231
    }
232

233
    public function getErrorRegistryClass(): string
492✔
234
    {
235
        return $this->errorRegistryClass;
492✔
236
    }
237

238
    public function setErrorRegistryClass(string $errorRegistryClass): self
×
239
    {
240
        $this->errorRegistryClass = $errorRegistryClass;
×
241

242
        return $this;
×
243
    }
244

245
    public function isImplicitNullAllowed(): bool
1,926✔
246
    {
247
        return $this->allowImplicitNull;
1,926✔
248
    }
249

250
    public function setImplicitNull(bool $allowImplicitNull): self
1,962✔
251
    {
252
        $this->allowImplicitNull = $allowImplicitNull;
1,962✔
253

254
        return $this;
1,962✔
255
    }
256

257
    private function initFilter(): void
2,001✔
258
    {
259
        $this
2,001✔
260
            ->addFilter(new DateTimeFilter())
2,001✔
261
            ->addFilter(new NotEmptyFilter())
2,001✔
262
            ->addFilter(new TrimFilter());
2,001✔
263
    }
264

265
    // TODO: add builtin format validators
266
    private function initFormatValidator(): void
2,001✔
267
    {
268
    }
2,001✔
269
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc