Skip to content

Commit 883d2ab

Browse files
committed
Added PHPDoc for generic functions
1 parent 5dc83c4 commit 883d2ab

6 files changed

Lines changed: 40 additions & 5 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "interaapps/jsonplus",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"type": "library",
55
"authors": [
66
{

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,23 @@ public function __construct(
3636
];
3737
}
3838

39-
public function fromJson($json, $type=null){
39+
/**
40+
* @template T
41+
* @param string $json The input json
42+
* @param null|class-string<T> $type A class (className::class), type (example: "array", "int"...) or null (Detects type automatically)
43+
* @return T
44+
* */
45+
public function fromJson(string $json, string|null $type = null){
4046
return $this->map($this->serializationAdapter->fromJson($json), $type);
4147
}
4248

43-
public function map($o, $type = null){
49+
/**
50+
* @template T
51+
* @param $o
52+
* @param null|class-string<T> $type $type A class (className::class), type (example: "array", "int"...) or null (Detects type automatically)
53+
* @return T
54+
* */
55+
public function map($o, null|string $type = null){
4456
if ($type == null) {
4557
$type = gettype($o);
4658

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace de\interaapps\jsonplus\typemapper;
4+
5+
class EnumTypeMapper implements TypeMapper {
6+
7+
public function map(mixed $o, string $type): mixed {
8+
return $type::{$o}->value;
9+
}
10+
11+
public function mapToJson(mixed $o, string $type): mixed {
12+
return $o->name;
13+
}
14+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22
namespace de\interaapps\jsonplus\typemapper;
33

44
interface TypeMapper {
5+
/**
6+
* @template T
7+
* @param mixed $o
8+
* @param class-string<T> $type
9+
* @return T
10+
* */
511
public function map(mixed $o, string $type) : mixed;
12+
613
public function mapToJson(mixed $o, string $type) : mixed;
714
}

src/test/testbootstrap.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,6 @@ public function setName(string $name): Test {
4949

5050
//echo Test::fromJson(JSON)->toJson();
5151
$json = new JSONPlus(new JsonSerializationAdapter());
52-
echo $json->toJson($json->fromJson(JSON, Test::class));
52+
$var = $json->fromJson(JSON, Test::class);
53+
echo $var->test."\n";
54+
echo $json->toJson($var);

uppm.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "interaapps/jsonplus",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"phpVersion": ">8.0",
55
"repositories": [],
66
"run": {

0 commit comments

Comments
 (0)