Skip to content

Commit cabe552

Browse files
feature #61023 [Serializer] add can to the accessor prefixes recognized by the AttributeLoader (Mark Schmale)
This PR was squashed before being merged into the 7.4 branch. Discussion ---------- [Serializer] add `can` to the accessor prefixes recognized by the `AttributeLoader` | Q | A | ------------- | --- | Branch? | 7.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #61021 | License | MIT The ObjectNormalizer already recognizes `canX` as an accessor and "generates" a property for that, the AttributeLoader does not, so #[Ignore] attributes on `canX` are ignored, the return value is encoded in the normalized data. We just add the `can` prefix ot the list of accepted accessor prefixes, so the AttributeLoader now also recognized these. I adapted a test that _seemd to me_ to fit the best. If you'd prefer a dedicated test for that, I can do that, too. Commits ------- fb1da0b [Serializer] add `can` to the accessor prefixes recognized by the `AttributeLoader`
2 parents af32a38 + fb1da0b commit cabe552

File tree

4 files changed

+13
-1
lines changed

4 files changed

+13
-1
lines changed

src/Symfony/Component/Serializer/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.4
5+
---
6+
7+
* Add support for `can*()` methods to `AttributeLoader`
8+
49
7.3
510
---
611

src/Symfony/Component/Serializer/Mapping/Loader/AttributeLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata): bool
114114
continue; /* matches the BC behavior in `Symfony\Component\Serializer\Normalizer\ObjectNormalizer::extractAttributes` */
115115
}
116116

117-
$accessorOrMutator = preg_match('/^(get|is|has|set)(.+)$/i', $method->name, $matches);
117+
$accessorOrMutator = preg_match('/^(get|is|has|set|can)(.+)$/i', $method->name, $matches);
118118
if ($accessorOrMutator && !ctype_lower($matches[2][0])) {
119119
$attributeName = lcfirst($matches[2]);
120120

src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/IgnoreDummy.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,10 @@ public function getIgnored2()
2828
{
2929
return $this->ignored2;
3030
}
31+
32+
#[Ignore]
33+
public function canBeIgnored(): bool
34+
{
35+
return true;
36+
}
3137
}

src/Symfony/Component/Serializer/Tests/Mapping/Loader/AttributeLoaderTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ public function testLoadIgnore()
153153
$attributesMetadata = $classMetadata->getAttributesMetadata();
154154
$this->assertTrue($attributesMetadata['ignored1']->isIgnored());
155155
$this->assertTrue($attributesMetadata['ignored2']->isIgnored());
156+
$this->assertTrue($attributesMetadata['beIgnored']->isIgnored());
156157
}
157158

158159
public function testLoadContextsPropertiesPromoted()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy