Skip to content

Commit 398f901

Browse files
committed
Use PascalCase for enum cases
1 parent 6aa30be commit 398f901

File tree

5 files changed

+50
-25
lines changed

5 files changed

+50
-25
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,11 @@ The `JsonMapper` constructor accepts the following options:
331331

332332
This option accepts an `OnExtraProperties` enum value, and controls how `JsonMapper` reacts if a JSON object contains a property that does not have a matching parameter in the corresponding DTO's constructor signature:
333333

334-
- **`OnExtraProperties::THROW_EXCEPTION`**
334+
- **`OnExtraProperties::ThrowException`**
335335

336336
`JsonMapper` will throw a `JsonMapperException`. This is the default value.
337337

338-
- **`OnExtraProperties::IGNORE`**
338+
- **`OnExtraProperties::Ignore`**
339339

340340
`JsonMapper` will ignore any extra properties:
341341

@@ -360,7 +360,7 @@ The `JsonMapper` constructor accepts the following options:
360360
}';
361361

362362
$mapper = new JsonMapper(
363-
onExtraProperties: OnExtraProperties::IGNORE,
363+
onExtraProperties: OnExtraProperties::Ignore,
364364
);
365365

366366
// extra properties "extraProperty" and "otherExtraProperty" are ignored,
@@ -372,11 +372,11 @@ The `JsonMapper` constructor accepts the following options:
372372

373373
This option accepts an `OnMissingProperties` enum value, and controls how `JsonMapper` reacts if a JSON object is missing a property that is declared in the corresponding DTO's constructor signature:
374374

375-
- **`OnMissingProperties::THROW_EXCEPTION`**
375+
- **`OnMissingProperties::ThrowException`**
376376

377377
`JsonMapper` will throw a `JsonMapperException`. This is the default value.
378378

379-
- **`OnMissingProperties::SET_NULL`**
379+
- **`OnMissingProperties::SetNull`**
380380

381381
`JsonMapper` will set the parameter to `null` if the JSON property is missing and the parameter is nullable:
382382

@@ -398,7 +398,7 @@ The `JsonMapper` constructor accepts the following options:
398398
}';
399399

400400
$mapper = new JsonMapper(
401-
onMissingProperties: OnMissingProperties::SET_NULL,
401+
onMissingProperties: OnMissingProperties::SetNull,
402402
);
403403

404404
$order = $mapper->map($json, Order::class);
@@ -407,10 +407,10 @@ The `JsonMapper` constructor accepts the following options:
407407

408408
If the property is missing and the parameter is not nullable, an exception will be thrown regardless of this option.
409409

410-
- **`OnMissingProperties::SET_DEFAULT`**
410+
- **`OnMissingProperties::SetDefault`**
411411

412412
`JsonMapper` will set the parameter to its default value if the JSON property is missing and the parameter has a default value:
413-
413+
414414
```php
415415
use Brick\JsonMapper\JsonMapper;
416416
use Brick\JsonMapper\OnMissingProperties;
@@ -429,7 +429,7 @@ The `JsonMapper` constructor accepts the following options:
429429
}';
430430

431431
$mapper = new JsonMapper(
432-
onMissingProperties: OnMissingProperties::SET_DEFAULT,
432+
onMissingProperties: OnMissingProperties::SetDefault,
433433
);
434434

435435
$order = $mapper->map($json, Order::class);

src/JsonMapper.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ public function __construct(
6161
* Extra properties are properties of the JSON object that do not have a corresponding constructor parameter in
6262
* the PHP class.
6363
*/
64-
private OnExtraProperties $onExtraProperties = OnExtraProperties::THROW_EXCEPTION,
64+
private OnExtraProperties $onExtraProperties = OnExtraProperties::ThrowException,
6565

6666
/**
6767
* Controls how missing properties in the JSON object are handled.
6868
* Missing properties are constructor parameters in the PHP class that do not have a corresponding property in
6969
* the JSON object.
7070
*/
71-
private OnMissingProperties $onMissingProperties = OnMissingProperties::THROW_EXCEPTION,
71+
private OnMissingProperties $onMissingProperties = OnMissingProperties::ThrowException,
7272

7373
/**
7474
* Mapper to convert JSON property names to PHP property names.
@@ -151,7 +151,7 @@ private function mapToObject(stdClass $jsonData, string $className): object
151151
);
152152
}
153153

154-
if ($this->onExtraProperties === OnExtraProperties::THROW_EXCEPTION) {
154+
if ($this->onExtraProperties === OnExtraProperties::ThrowException) {
155155
/** @psalm-suppress MixedAssignment, RawObjectIteration */
156156
foreach ($jsonData as $jsonPropertyName => $_) {
157157
/** @var string $jsonPropertyName https://github.com/vimeo/psalm/issues/9108 */
@@ -184,13 +184,13 @@ private function getParameterValue(
184184
$parameterType = $this->reflector->getParameterType($reflectionParameter);
185185

186186
if (! property_exists($jsonData, $jsonPropertyName)) {
187-
if ($this->onMissingProperties === OnMissingProperties::SET_NULL) {
187+
if ($this->onMissingProperties === OnMissingProperties::SetNull) {
188188
if ($parameterType->allowsNull) {
189189
return null;
190190
}
191191
}
192192

193-
if ($this->onMissingProperties === OnMissingProperties::SET_DEFAULT) {
193+
if ($this->onMissingProperties === OnMissingProperties::SetDefault) {
194194
if ($reflectionParameter->isDefaultValueAvailable()) {
195195
// TODO we should technically check if the default value is compatible with the parameter type,
196196
// as the type declared as @param may be more specific than the PHP type.
@@ -201,9 +201,9 @@ private function getParameterValue(
201201
throw new JsonMapperException([
202202
sprintf('Missing property "%s" in JSON data.', $jsonPropertyName),
203203
match ($this->onMissingProperties) {
204-
OnMissingProperties::SET_NULL => 'The parameter does not allow null.',
205-
OnMissingProperties::SET_DEFAULT => 'The parameter does not have a default value.',
206-
OnMissingProperties::THROW_EXCEPTION => 'If you want to allow missing properties, change the $onMissingProperties option.',
204+
OnMissingProperties::SetNull => 'The parameter does not allow null.',
205+
OnMissingProperties::SetDefault => 'The parameter does not have a default value.',
206+
OnMissingProperties::ThrowException => 'If you want to allow missing properties, change the $onMissingProperties option.',
207207
},
208208
]);
209209
}

src/OnExtraProperties.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@
66

77
enum OnExtraProperties
88
{
9-
case THROW_EXCEPTION;
10-
case IGNORE;
9+
case ThrowException;
10+
case Ignore;
11+
12+
/**
13+
* @deprecated Use OnExtraProperties::ThrowException
14+
*/
15+
public const THROW_EXCEPTION = self::ThrowException;
16+
17+
/**
18+
* @deprecated Use OnExtraProperties::Ignore
19+
*/
20+
public const IGNORE = self::Ignore;
1121
}

src/OnMissingProperties.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,22 @@
66

77
enum OnMissingProperties
88
{
9-
case THROW_EXCEPTION;
10-
case SET_NULL;
11-
case SET_DEFAULT;
9+
case ThrowException;
10+
case SetNull;
11+
case SetDefault;
12+
13+
/**
14+
* @deprecated Use OnMissingProperties::ThrowException
15+
*/
16+
public const THROW_EXCEPTION = self::ThrowException;
17+
18+
/**
19+
* @deprecated Use OnMissingProperties::SetNull
20+
*/
21+
public const SET_NULL = self::SetNull;
22+
23+
/**
24+
* @deprecated Use OnMissingProperties::SetDefault
25+
*/
26+
public const SET_DEFAULT = self::SetDefault;
1227
}

tests/JsonMapperTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public function testMapWithOnExtraPropertiesIgnore(): void
147147
}
148148
JSON;
149149

150-
$jsonMapper = new JsonMapper(onExtraProperties: OnExtraProperties::IGNORE);
150+
$jsonMapper = new JsonMapper(onExtraProperties: OnExtraProperties::Ignore);
151151
$artist = $jsonMapper->map($json, Artist::class);
152152

153153
self::assertInstanceOf(Artist::class, $artist);
@@ -186,7 +186,7 @@ public function testMapWithOnMissingPropertiesSetNull(): void
186186
}
187187
JSON;
188188

189-
$jsonMapper = new JsonMapper(onMissingProperties: OnMissingProperties::SET_NULL);
189+
$jsonMapper = new JsonMapper(onMissingProperties: OnMissingProperties::SetNull);
190190
$artist = $jsonMapper->map($json, Artist::class);
191191

192192
self::assertInstanceOf(Artist::class, $artist);
@@ -205,7 +205,7 @@ public function testMapWithOnMissingPropertiesSetDefault(): void
205205
}
206206
JSON;
207207

208-
$jsonMapper = new JsonMapper(onMissingProperties: OnMissingProperties::SET_DEFAULT);
208+
$jsonMapper = new JsonMapper(onMissingProperties: OnMissingProperties::SetDefault);
209209
$artist = $jsonMapper->map($json, Artist::class);
210210

211211
self::assertInstanceOf(Artist::class, $artist);

0 commit comments

Comments
 (0)