Skip to content

Commit 1f80ee3

Browse files
committed
Replace ClassMetadata::isNormalizeAsArray() + ClassMetadata::shouldNormalizeAsArray() accessor to property
1 parent 5d67867 commit 1f80ee3

File tree

5 files changed

+23
-43
lines changed

5 files changed

+23
-43
lines changed

src/Mapping/Driver/ArrayConfigDriver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ protected function load(
108108
// @phpstan-ignore-next-line : Additional DbC invariant
109109
assert(\is_bool($classConfig['normalize_as_array']));
110110

111-
$class->shouldNormalizeAsArray($classConfig['normalize_as_array']);
111+
$class->isNormalizeAsArray = $classConfig['normalize_as_array'];
112112
}
113113

114114
// ---------------------------------------------------------------------

src/Mapping/Driver/AttributeDriver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ protected function load(
8484
$attribute = $this->findClassAttribute($reflection, NormalizeAsArray::class);
8585

8686
if ($attribute !== null) {
87-
$class->shouldNormalizeAsArray($attribute->enabled);
87+
$class->isNormalizeAsArray = $attribute->enabled;
8888
}
8989

9090
foreach ($reflection->getProperties() as $property) {

src/Mapping/Metadata/ClassMetadata.php

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,18 @@ final class ClassMetadata extends Metadata
2525
private array $properties = [];
2626

2727
/**
28-
* Contains a {@see bool} flag that is responsible for converting the
29-
* object into an associative {@see array} during normalization.
28+
* Gets information about the normalization method of an object.
3029
*
31-
* If {@see null}, then the system setting should be used.
30+
* - Contains {@see true} if the object should be normalized as
31+
* an associative {@see array}.
32+
*
33+
* - Contains {@see false} if the object should be normalized as an
34+
* anonymous {@see object}.
35+
*
36+
* - Contains {@see null} if the system settings for this option
37+
* should be used.
3238
*/
33-
private ?bool $isNormalizeAsArray = null;
39+
public ?bool $isNormalizeAsArray = null;
3440

3541
/**
3642
* Contains a {@see DiscriminatorMapMetadata} instance in case of class-like
@@ -93,35 +99,6 @@ public function getTypeStatement(Context $context): TypeStatement
9399
return new NamedTypeNode($this->name, fields: new FieldsListNode($fields));
94100
}
95101

96-
/**
97-
* Returns information about the normalization method of an object.
98-
*
99-
* - Returns {@see true} if the object should be normalized as
100-
* an associative {@see array}.
101-
*
102-
* - Returns {@see false} if the object should be normalized as an
103-
* anonymous {@see object}.
104-
*
105-
* - Returns {@see null} if the system settings for this option
106-
* should be used.
107-
*
108-
* @api
109-
*/
110-
public function isNormalizeAsArray(): ?bool
111-
{
112-
return $this->isNormalizeAsArray;
113-
}
114-
115-
/**
116-
* Forces the object normalization option.
117-
*
118-
* @api
119-
*/
120-
public function shouldNormalizeAsArray(?bool $enabled = null): void
121-
{
122-
$this->isNormalizeAsArray = $enabled;
123-
}
124-
125102
/**
126103
* Adds {@see PropertyMetadata} property information to
127104
* the {@see ClassMetadata} instance.

src/Type/ClassType/ClassTypeNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function cast(mixed $value, Context $context): object|array
6262

6363
$result = $this->normalizeObject($value, $entrance);
6464

65-
if ($this->metadata->isNormalizeAsArray() ?? $context->isObjectsAsArrays()) {
65+
if ($this->metadata->isNormalizeAsArray ?? $context->isObjectsAsArrays()) {
6666
return $result;
6767
}
6868

tests/Mapping/Metadata/ClassMetadataTest.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,16 @@ public function testNameGetter(): void
3939
public function testNormalizeAsArrayFlag(): void
4040
{
4141
$m = new ClassMetadata(\stdClass::class);
42-
self::assertNull($m->isNormalizeAsArray());
43-
$m->shouldNormalizeAsArray(true);
44-
self::assertTrue($m->isNormalizeAsArray());
45-
$m->shouldNormalizeAsArray(false);
46-
self::assertFalse($m->isNormalizeAsArray());
47-
$m->shouldNormalizeAsArray(null);
48-
self::assertNull($m->isNormalizeAsArray());
42+
self::assertNull($m->isNormalizeAsArray);
43+
44+
$m->isNormalizeAsArray = true;
45+
self::assertTrue($m->isNormalizeAsArray);
46+
47+
$m->isNormalizeAsArray = false;
48+
self::assertFalse($m->isNormalizeAsArray);
49+
50+
$m->isNormalizeAsArray = null;
51+
self::assertNull($m->isNormalizeAsArray);
4952
}
5053

5154
public function testAddAndGetProperties(): void

0 commit comments

Comments
 (0)