Skip to content

Commit ea53ed2

Browse files
sommmenStringEpsilon
authored andcommitted
Added support for PhpProperty on enums, allowing consumers to specify different field names
1 parent 60bef9e commit ea53ed2

File tree

4 files changed

+48
-5
lines changed

4 files changed

+48
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# vNext
2+
- Added support for PhpProperty on enums, allowing consumers to specify different field names
3+
14
# 1.0.0
25

36
This is just 0.11.0 packaged as a new version to mark it as stable.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
This Source Code Form is subject to the terms of the Mozilla Public
3+
License, v. 2.0. If a copy of the MPL was not distributed with this
4+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
5+
**/
6+
7+
namespace PhpSerializerNET.Test.DataTypes;
8+
9+
public enum IntEnumWithPropertyName {
10+
11+
[PhpProperty("a")]
12+
A = 1,
13+
14+
[PhpProperty("c")]
15+
B = 13,
16+
17+
// No prop name
18+
C,
19+
}

PhpSerializerNET.Test/Deserialize/EnumDeserialization.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,24 @@ public void DeserializeFromString() {
3737
);
3838
}
3939

40+
[TestMethod]
41+
public void DeserializeFromStringWithPropertyName() {
42+
Assert.AreEqual(
43+
IntEnumWithPropertyName.A,
44+
PhpSerialization.Deserialize<IntEnumWithPropertyName>("s:1:\"a\";")
45+
);
46+
47+
Assert.AreEqual(
48+
IntEnumWithPropertyName.B,
49+
PhpSerialization.Deserialize<IntEnumWithPropertyName>("s:1:\"c\";")
50+
);
51+
52+
Assert.AreEqual(
53+
IntEnumWithPropertyName.C,
54+
PhpSerialization.Deserialize<IntEnumWithPropertyName>("s:1:\"C\";")
55+
);
56+
}
57+
4058
[TestMethod]
4159
public void DeserializeToNullable() {
4260
LongEnum? result = PhpSerialization.Deserialize<LongEnum?>("i:1;");

PhpSerializerNET/PhpDeserializer.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,21 +246,24 @@ private object DeserializeTokenFromSimpleType(Type givenType, PhpSerializeToken
246246
if (targetType.IsEnum) {
247247
// Enums are converted by name if the token is a string and by underlying value if they are not
248248

249+
if (token.Value == "" && this._options.EmptyStringToDefault) {
250+
return Activator.CreateInstance(targetType);
251+
}
252+
249253
if (token.Type != PhpSerializerType.String) {
250254
return Enum.Parse(targetType, token.Value);
251255
}
256+
252257
var foundFieldInfo = targetType
253258
.GetFields()
254-
.FirstOrDefault(y => y.Name == token.Value);
259+
.Select(fieldInfo => new { fieldInfo, phpPropertyAttribute = fieldInfo.GetCustomAttribute<PhpPropertyAttribute>() })
260+
.FirstOrDefault(c => c.fieldInfo.Name == token.Value || c.phpPropertyAttribute != null && c.phpPropertyAttribute.Name == token.Value)
261+
?.fieldInfo;
255262

256263
if (foundFieldInfo == null) {
257-
if (_options.EmptyStringToDefault) {
258-
return Activator.CreateInstance(targetType);
259-
} else {
260264
throw new DeserializationException(
261265
$"Exception encountered while trying to assign '{token.Value}' to type '{targetType.Name}'. " +
262266
$"The value could not be matched to an enum member.");
263-
}
264267
}
265268

266269
return foundFieldInfo.GetRawConstantValue();

0 commit comments

Comments
 (0)