Skip to content

Commit 53602b7

Browse files
committed
Added enum support
1 parent 1573db7 commit 53602b7

7 files changed

Lines changed: 54 additions & 16 deletions

File tree

README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,29 @@ class Organisation {
3737
public string $name;
3838
}
3939

40+
enum UserType {
41+
case ADMIN;
42+
case MEMBER;
43+
}
44+
4045
class User {
4146
use JSONModel;
4247

43-
public int id;
48+
public int $id;
4449

4550
#[Serialize("first_name")]
46-
public string firstName;
51+
public string $firstName;
4752

4853
#[Serialize(hidden: true)]
49-
public string password;
54+
public string $password;
5055

5156
public ?Organisation $organisation;
5257

5358
// Set Type for array:
5459
#[ArrayType(User::class)]
5560
public array $friends;
61+
62+
public UserType $type = UserType::MEMBER;
5663
}
5764

5865
$json = '{
@@ -67,7 +74,8 @@ $json = '{
6774
"first_name": "John",
6875
"friends": []
6976
}
70-
]
77+
],
78+
"type": "USER"
7179
}';
7280

7381
// Decoding the JSON
@@ -95,6 +103,7 @@ For Typed Arrays:
95103
/**
96104
* @var array<User>
97105
*/
106+
private array $myArray;
98107
```
99108

100109
## Installation

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "interaapps/jsonplus",
3-
"version": "1.0.4",
3+
"version": "1.0.5",
44
"type": "library",
55
"authors": [
66
{
@@ -9,7 +9,7 @@
99
}
1010
],
1111
"require": {
12-
"php": ">=8.0"
12+
"php": ">=8.1"
1313
},
1414
"autoload": {
1515
"psr-4": {

src/main/de/interaapps/jsonplus/JSONPlus.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use de\interaapps\jsonplus\serializationadapter\impl\phpjson\PHPJsonSerializationAdapter;
66
use de\interaapps\jsonplus\serializationadapter\SerializationAdapter;
77
use de\interaapps\jsonplus\typemapper\ArrayTypeMapper;
8+
use de\interaapps\jsonplus\typemapper\EnumTypeMapper;
89
use de\interaapps\jsonplus\typemapper\ObjectTypeMapper;
910
use de\interaapps\jsonplus\typemapper\PassThroughTypeMapper;
1011
use de\interaapps\jsonplus\typemapper\StdClassObjectTypeMapper;
@@ -21,7 +22,7 @@ class JSONPlus {
2122
public function __construct(
2223
private SerializationAdapter $serializationAdapter
2324
){
24-
$this->defaultTypeMapper = new ObjectTypeMapper($this);
25+
$this->defaultTypeMapper = new ObjectTypeMapper($this, new EnumTypeMapper());
2526
$this->passThroughTypeMapper = new PassThroughTypeMapper();
2627
$this->typeMapper = [
2728
"object" => $this->passThroughTypeMapper,

src/main/de/interaapps/jsonplus/typemapper/EnumTypeMapper.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22

33
namespace de\interaapps\jsonplus\typemapper;
44

5-
class EnumTypeMapper implements TypeMapper {
5+
class EnumTypeMapper {
66

7-
public function map(mixed $o, string $type): mixed {
8-
return $type::{$o}->value;
7+
public function map(\ReflectionClass $enum, string $name): mixed {
8+
foreach ($enum->getMethod("cases")->invoke(null) as $entry) {
9+
if ($entry->name == $name)
10+
return $entry;
11+
}
12+
13+
return null;
914
}
1015

11-
public function mapToJson(mixed $o, string $type): mixed {
12-
return $o->name;
16+
public function mapToJson(\ReflectionClass $enum, mixed $type): mixed {
17+
return $type->name;
1318
}
1419
}

src/main/de/interaapps/jsonplus/typemapper/ObjectTypeMapper.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
class ObjectTypeMapper implements TypeMapper {
1111
public function __construct(
12-
private JSONPlus $jsonPlus
12+
private JSONPlus $jsonPlus,
13+
private EnumTypeMapper $enumTypeMapper
1314
){
1415
}
1516

@@ -18,6 +19,10 @@ public function map(mixed $o, string $type): mixed {
1819
return null;
1920

2021
$class = new ReflectionClass(str_replace("?", "", $type));
22+
if ($class->isEnum()) {
23+
return $this->enumTypeMapper->map($class, $o);
24+
}
25+
2126
$oo = $class->newInstance();
2227

2328
foreach ($class->getProperties() as $property) {
@@ -53,13 +58,19 @@ public function mapToJson(mixed $o, string $type): mixed {
5358
if ($o === null)
5459
return null;
5560
$class = new ReflectionClass(str_replace("?", "", $type));
61+
62+
if ($class->isEnum()) {
63+
return $this->enumTypeMapper->mapToJson($class, $o);
64+
}
65+
5666
$oo = [];
5767
foreach ($class->getProperties() as $property) {
5868
if (!$property->isStatic()) {
5969
$name = $property?->getName();
6070

6171
$overrideName = $property?->getName();
6272
$serializeAttribs = $property->getAttributes(Serialize::class);
73+
6374
foreach ($serializeAttribs as $attrib) {
6475
$attrib = $attrib->newInstance();
6576
$overrideName = $attrib->value;

src/test/testbootstrap.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ class Test2 {
2222
public string $sheesh;
2323
}
2424

25+
26+
27+
enum MyEnum {
28+
case HEY;
29+
case WORLD;
30+
}
31+
2532
class Test {
2633
use JSONModel;
2734
#[Serialize("name_")]
@@ -33,6 +40,9 @@ class Test {
3340
public Test2 $test2;
3441
public $aaaa;
3542
public $aa;
43+
44+
public ?MyEnum $myEnum = null;
45+
3646
public function __construct(){
3747
}
3848

@@ -57,7 +67,7 @@ public function setName(string $name): Test {
5767
echo Test::fromJson(JSON)->toJson();
5868
$json = new JSONPlus(new JsonSerializationAdapter());
5969
$var = $json->fromJson(JSON, Test::class);
60-
70+
echo $var->myEnum;
6171
echo $var->name."\n";
6272
echo $json->toJson($var);
6373

@@ -80,6 +90,8 @@ class Test3 {
8090
}
8191
]', Test2::class);
8292

93+
94+
8395
foreach ($arr as $val) {
8496
echo $val->sheesh;
8597
}

uppm.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "interaapps/jsonplus",
3-
"version": "1.0.4",
4-
"phpVersion": ">8.0",
3+
"version": "1.0.5",
4+
"phpVersion": ">8.1",
55
"repositories": [],
66
"run": {
77
"test": "src\/test\/testbootstrap.php"

0 commit comments

Comments
 (0)