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

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

03 Apr 2026 01:13AM UTC coverage: 98.248% (-0.4%) from 98.654%
23929375043

Pull #125

github

Enno Woortmann
attribute tests
Pull Request #125: attributes

1496 of 1526 new or added lines in 66 files covered. (98.03%)

4 existing lines in 3 files now uncovered.

4374 of 4452 relevant lines covered (98.25%)

620.23 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,233✔
70
    {
71
        $this->draft = new AutoDetectionDraft();
2,233✔
72
        $this->classNameGenerator = new ClassNameGenerator();
2,233✔
73

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

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

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

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

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

112
        return $this;
2,233✔
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,233✔
134
    {
135
        if (
136
            !(count($callback) === 2) ||
2,233✔
137
            !is_string($callback[0]) ||
2,233✔
138
            !is_string($callback[1]) ||
2,233✔
139
            !is_callable($callback)
2,233✔
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,182✔
154
    {
155
        return $this->classNameGenerator;
2,182✔
156
    }
157

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

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

165
    public function getNamespacePrefix(): string
2,105✔
166
    {
167
        return $this->namespacePrefix;
2,105✔
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,158✔
190
    {
191
        return $this->immutable;
2,158✔
192
    }
193

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

198
        return $this;
189✔
199
    }
200

201
    public function denyAdditionalProperties(): bool
2,042✔
202
    {
203
        return $this->denyAdditionalProperties;
2,042✔
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,183✔
214
    {
215
        return $this->serialization;
2,183✔
216
    }
217

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

222
        return $this;
53✔
223
    }
224

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

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

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

237
    public function collectErrors(): bool
2,097✔
238
    {
239
        return $this->collectErrors;
2,097✔
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
539✔
250
    {
251
        return $this->errorRegistryClass;
539✔
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,185✔
262
    {
263
        return $this->draft;
2,185✔
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,150✔
274
    {
275
        return $this->allowImplicitNull;
2,150✔
276
    }
277

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

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

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

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

NEW
298
    public function setEnabledAttributes(int $enabledAttributes): self
×
299
    {
NEW
300
        $this->enabledAttributes = $enabledAttributes;
×
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;
1✔
315

316
        return $this;
1✔
317
    }
318

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