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

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

08 Apr 2026 12:29PM UTC coverage: 98.249% (-0.4%) from 98.654%
24135379642

Pull #125

github

wol-soft
CI
Pull Request #125: attributes

1507 of 1537 new or added lines in 73 files covered. (98.05%)

4 existing lines in 3 files now uncovered.

4376 of 4454 relevant lines covered (98.25%)

621.28 hits per line

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

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

3
declare(strict_types=1);
4

5
namespace PHPModelGenerator\Model;
6

7
use Exception;
8
use PHPModelGenerator\Draft\AutoDetectionDraft;
9
use PHPModelGenerator\Draft\DraftFactoryInterface;
10
use PHPModelGenerator\Draft\DraftInterface;
11
use PHPModelGenerator\Exception\ErrorRegistryException;
12
use PHPModelGenerator\Exception\InvalidFilterException;
13
use PHPModelGenerator\Filter\FilterInterface;
14
use PHPModelGenerator\Filter\TransformingFilterInterface;
15
use PHPModelGenerator\Format\FormatValidatorInterface;
16
use PHPModelGenerator\Model\Attributes\PhpAttribute;
17
use PHPModelGenerator\PropertyProcessor\Filter\DateTimeFilter;
18
use PHPModelGenerator\PropertyProcessor\Filter\NotEmptyFilter;
19
use PHPModelGenerator\PropertyProcessor\Filter\TrimFilter;
20
use PHPModelGenerator\Utils\ClassNameGenerator;
21
use PHPModelGenerator\Utils\ClassNameGeneratorInterface;
22

23
/**
24
 * Class GeneratorConfiguration
25
 *
26
 * @package PHPModelGenerator\Model
27
 */
28
class GeneratorConfiguration
29
{
30
    /** @var string */
31
    protected $namespacePrefix = '';
32
    /** @var bool */
33
    protected $immutable = true;
34
    /** @var bool */
35
    protected $allowImplicitNull = false;
36
    /** @var bool */
37
    protected $defaultArraysToEmptyArray = false;
38
    /** @var bool */
39
    protected $denyAdditionalProperties = false;
40
    /** @var bool */
41
    protected $outputEnabled = true;
42
    /** @var bool */
43
    protected $collectErrors = true;
44
    /** @var string */
45
    protected $errorRegistryClass = ErrorRegistryException::class;
46
    /** @var bool */
47
    protected $serialization = false;
48
    /** @var int */
49
    protected $enabledAttributes = PhpAttribute::JSON_POINTER
50
        | PhpAttribute::SCHEMA_NAME
51
        | PhpAttribute::REQUIRED
52
        | PhpAttribute::READ_WRITE_ONLY
53
        | PhpAttribute::DEPRECATED;
54

55
    /** @var DraftInterface | DraftFactoryInterface */
56
    protected $draft;
57

58
    /** @var ClassNameGeneratorInterface */
59
    protected $classNameGenerator;
60

61
    /** @var FilterInterface[] */
62
    protected $filter;
63
    /** @var FormatValidatorInterface[] */
64
    protected $formats;
65

66
    /**
67
     * GeneratorConfiguration constructor.
68
     */
69
    public function __construct()
2,237✔
70
    {
71
        $this->draft = new AutoDetectionDraft();
2,237✔
72
        $this->classNameGenerator = new ClassNameGenerator();
2,237✔
73

74
        // add all built-in filter and format validators
75
        $this->initFilter();
2,237✔
76
        $this->initFormatValidator();
2,237✔
77
    }
78

79
    /**
80
     * Add an additional filter
81
     *
82
     * @throws Exception
83
     * @throws InvalidFilterException
84
     */
85
    public function addFilter(FilterInterface ...$additionalFilter): self
2,237✔
86
    {
87
        foreach ($additionalFilter as $filter) {
2,237✔
88
            $this->validateFilterCallback(
2,237✔
89
                $filter->getFilter(),
2,237✔
90
                "Invalid filter callback for filter {$filter->getToken()}",
2,237✔
91
            );
2,237✔
92

93
            if ($filter instanceof TransformingFilterInterface) {
2,237✔
94
                $this->validateFilterCallback(
2,237✔
95
                    $filter->getSerializer(),
2,237✔
96
                    "Invalid serializer callback for filter {$filter->getToken()}"
2,237✔
97
                );
2,237✔
98
            }
99

100
            foreach ($filter->getAcceptedTypes() as $acceptedType) {
2,237✔
101
                if (
102
                    !in_array($acceptedType, ['integer', 'number', 'boolean', 'string', 'array', 'null']) &&
2,237✔
103
                    !class_exists($acceptedType)
2,237✔
104
                ) {
105
                    throw new InvalidFilterException('Filter accepts invalid types');
1✔
106
                }
107
            }
108

109
            $this->filter[$filter->getToken()] = $filter;
2,237✔
110
        }
111

112
        return $this;
2,237✔
113
    }
114

115
    /**
116
     * Add an additional format
117
     */
118
    public function addFormat(string $formatKey, FormatValidatorInterface $format): self
5✔
119
    {
120
        $this->formats[$formatKey] = $format;
5✔
121

122
        return $this;
5✔
123
    }
124

125
    public function getFormat(string $formatKey): ?FormatValidatorInterface
6✔
126
    {
127
        return $this->formats[$formatKey] ?? null;
6✔
128
    }
129

130
    /**
131
     * @throws InvalidFilterException
132
     */
133
    private function validateFilterCallback(array $callback, string $message): void
2,237✔
134
    {
135
        if (
136
            !(count($callback) === 2) ||
2,237✔
137
            !is_string($callback[0]) ||
2,237✔
138
            !is_string($callback[1]) ||
2,237✔
139
            !is_callable($callback)
2,237✔
140
        ) {
141
            throw new InvalidFilterException($message);
14✔
142
        }
143
    }
144

145
    /**
146
     * Get a filter by the given token
147
     */
148
    public function getFilter(string $token): ?FilterInterface
135✔
149
    {
150
        return $this->filter[$token] ?? null;
135✔
151
    }
152

153
    public function getClassNameGenerator(): ClassNameGeneratorInterface
2,186✔
154
    {
155
        return $this->classNameGenerator;
2,186✔
156
    }
157

158
    public function setClassNameGenerator(ClassNameGeneratorInterface $classNameGenerator): self
2,162✔
159
    {
160
        $this->classNameGenerator = $classNameGenerator;
2,162✔
161

162
        return $this;
2,162✔
163
    }
164

165
    public function getNamespacePrefix(): string
2,109✔
166
    {
167
        return $this->namespacePrefix;
2,109✔
168
    }
169

170
    public function setNamespacePrefix(string $namespacePrefix): self
42✔
171
    {
172
        $this->namespacePrefix = trim($namespacePrefix, '\\');
42✔
173

174
        return $this;
42✔
175
    }
176

177
    public function isDefaultArraysToEmptyArrayEnabled(): bool
538✔
178
    {
179
        return $this->defaultArraysToEmptyArray;
538✔
180
    }
181

182
    public function setDefaultArraysToEmptyArray(bool $defaultArraysToEmptyArray): self
5✔
183
    {
184
        $this->defaultArraysToEmptyArray = $defaultArraysToEmptyArray;
5✔
185

186
        return $this;
5✔
187
    }
188

189
    public function isImmutable(): bool
2,162✔
190
    {
191
        return $this->immutable;
2,162✔
192
    }
193

194
    public function setImmutable(bool $immutable): self
193✔
195
    {
196
        $this->immutable = $immutable;
193✔
197

198
        return $this;
193✔
199
    }
200

201
    public function denyAdditionalProperties(): bool
2,046✔
202
    {
203
        return $this->denyAdditionalProperties;
2,046✔
204
    }
205

206
    public function setDenyAdditionalProperties(bool $denyAdditionalProperties): self
10✔
207
    {
208
        $this->denyAdditionalProperties = $denyAdditionalProperties;
10✔
209

210
        return $this;
10✔
211
    }
212

213
    public function hasSerializationEnabled(): bool
2,187✔
214
    {
215
        return $this->serialization;
2,187✔
216
    }
217

218
    public function setSerialization(bool $serialization): self
57✔
219
    {
220
        $this->serialization = $serialization;
57✔
221

222
        return $this;
57✔
223
    }
224

225
    public function setOutputEnabled(bool $outputEnabled): self
2,189✔
226
    {
227
        $this->outputEnabled = $outputEnabled;
2,189✔
228

229
        return $this;
2,189✔
230
    }
231

232
    public function isOutputEnabled(): bool
2,125✔
233
    {
234
        return $this->outputEnabled;
2,125✔
235
    }
236

237
    public function collectErrors(): bool
2,101✔
238
    {
239
        return $this->collectErrors;
2,101✔
240
    }
241

242
    public function setCollectErrors(bool $collectErrors): self
1,384✔
243
    {
244
        $this->collectErrors = $collectErrors;
1,384✔
245

246
        return $this;
1,384✔
247
    }
248

249
    public function getErrorRegistryClass(): string
543✔
250
    {
251
        return $this->errorRegistryClass;
543✔
252
    }
253

254
    public function setErrorRegistryClass(string $errorRegistryClass): self
×
255
    {
256
        $this->errorRegistryClass = $errorRegistryClass;
×
257

258
        return $this;
×
259
    }
260

261
    public function getDraft(): DraftInterface | DraftFactoryInterface
2,189✔
262
    {
263
        return $this->draft;
2,189✔
264
    }
265

266
    public function setDraft(DraftInterface | DraftFactoryInterface $draft): self
4✔
267
    {
268
        $this->draft = $draft;
4✔
269

270
        return $this;
4✔
271
    }
272

273
    public function isImplicitNullAllowed(): bool
2,154✔
274
    {
275
        return $this->allowImplicitNull;
2,154✔
276
    }
277

278
    public function setImplicitNull(bool $allowImplicitNull): self
2,166✔
279
    {
280
        $this->allowImplicitNull = $allowImplicitNull;
2,166✔
281

282
        return $this;
2,166✔
283
    }
284

285
    private function initFilter(): void
2,237✔
286
    {
287
        $this
2,237✔
288
            ->addFilter(new DateTimeFilter())
2,237✔
289
            ->addFilter(new NotEmptyFilter())
2,237✔
290
            ->addFilter(new TrimFilter());
2,237✔
291
    }
292

293
    public function getEnabledAttributes(): int
2,186✔
294
    {
295
        return $this->enabledAttributes;
2,186✔
296
    }
297

NEW
298
    public function setEnabledAttributes(int $enabledAttributes): self
×
299
    {
NEW
300
        $this->enabledAttributes = $enabledAttributes | PhpAttribute::ALWAYS_ENABLED_ATTRIBUTES;
×
301

NEW
302
        return $this;
×
303
    }
304

305
    public function enableAttributes(int $attributes): self
1✔
306
    {
307
        $this->enabledAttributes = $this->enabledAttributes | $attributes;
1✔
308

309
        return $this;
1✔
310
    }
311

312
    public function disableAttributes(int $attributes): self
1✔
313
    {
314
        $this->enabledAttributes = $this->enabledAttributes & ~$attributes | PhpAttribute::ALWAYS_ENABLED_ATTRIBUTES;
1✔
315

316
        return $this;
1✔
317
    }
318

319
    // TODO: add builtin format validators
320
    private function initFormatValidator(): void
2,237✔
321
    {
322
    }
2,237✔
323
}
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

© 2026 Coveralls, Inc