@@ -41,35 +41,55 @@ protected function setUp(): void
41
41
*
42
42
* @param array<string, mixed> $context
43
43
*/
44
- public function testSerializeDeserialize (mixed $ data , string $ type , array $ context = [])
44
+ public function testSerializeDeserialize (mixed $ data , string $ type , string $ format , array $ context = [])
45
45
{
46
46
/** @var resource $resource */
47
47
$ resource = fopen ('php://memory ' , 'w+ ' );
48
48
49
- serialize ($ data , $ resource , ' json ' , ['type ' => $ type ] + $ context );
49
+ serialize ($ data , $ resource , $ format , ['type ' => $ type ] + $ context );
50
50
rewind ($ resource );
51
51
52
- $ this ->assertEquals ($ data , deserialize ($ resource , TypeFactory::createFromString ($ type ), ' json ' , $ context ));
52
+ $ this ->assertEquals ($ data , deserialize ($ resource , TypeFactory::createFromString ($ type ), $ format , $ context ));
53
53
}
54
54
55
55
/**
56
- * @return iterable<array{0: mixed, 1: string, 2: array<string, mixed>}>
56
+ * @return iterable<array{0: mixed, 1: string, 2: string, 3: array<string, mixed>}>
57
57
*/
58
58
public static function serializeDeserializeDataProvider (): iterable
59
59
{
60
- yield [1 , 'int ' ];
61
- yield [null , '?int ' ];
62
- yield ['foo ' , 'string ' ];
63
- yield [[1 , 2 , null ], 'array<int, ?int> ' ];
64
- yield [['foo ' => 1 , 'bar ' => 2 , 'baz ' => null ], 'array<string, ?int> ' ];
65
- yield [DummyBackedEnum::ONE , DummyBackedEnum::class];
66
- yield [new ClassicDummy (), ClassicDummy::class];
67
-
68
60
$ dummy = new DummyWithFormatterAttributes ();
69
61
$ dummy ->id = 200 ;
70
62
$ dummy ->name = '200 ' ;
71
63
72
- yield [$ dummy , DummyWithFormatterAttributes::class, ['hooks ' => [
64
+ yield [1 , 'int ' , 'json ' ];
65
+ yield [null , '?int ' , 'json ' ];
66
+ yield ['foo ' , 'string ' , 'json ' ];
67
+ yield [[1 , 2 , null ], 'array<int, ?int> ' , 'json ' ];
68
+ yield [['foo ' => 1 , 'bar ' => 2 , 'baz ' => null ], 'array<string, ?int> ' , 'json ' ];
69
+ yield [DummyBackedEnum::ONE , DummyBackedEnum::class, 'json ' ];
70
+ yield [new ClassicDummy (), ClassicDummy::class, 'json ' ];
71
+ yield [$ dummy , DummyWithFormatterAttributes::class, 'json ' , ['hooks ' => [
72
+ 'serialize ' => [
73
+ sprintf ('%s::$name ' , DummyWithFormatterAttributes::class) => fn (\ReflectionProperty $ p , string $ accessor ) => [
74
+ 'name ' => '@name ' ,
75
+ 'accessor ' => sprintf ('%s::divideAndCastToInt(%s, $context) ' , DummyWithFormatterAttributes::class, $ accessor ),
76
+ ],
77
+ ],
78
+ 'deserialize ' => [
79
+ sprintf ('%s[@name] ' , DummyWithFormatterAttributes::class) => fn (\ReflectionClass $ class , string $ key , callable $ value , array $ context ) => [
80
+ 'name ' => 'name ' ,
81
+ 'value_provider ' => fn () => DummyWithFormatterAttributes::doubleAndCastToString ($ value ('int ' , $ context )),
82
+ ],
83
+ ],
84
+ ]]];
85
+
86
+ yield [[1 ], 'array<int, int> ' , 'csv ' ];
87
+ yield [[1 , 2 , null ], 'array<int, ?int> ' , 'csv ' ];
88
+ yield [['foo ' ], 'array<int, string> ' , 'csv ' ];
89
+ yield [[['foo ' => 1 , 'bar ' => null ], ['foo ' => null , 'bar ' => 2 ]], 'array<int, array<string, ?int>> ' , 'csv ' ];
90
+ yield [[DummyBackedEnum::ONE ], sprintf ('array<int, %s> ' , DummyBackedEnum::class), 'csv ' ];
91
+ yield [[new ClassicDummy ()], sprintf ('array<int, %s> ' , ClassicDummy::class), 'csv ' ];
92
+ yield [[$ dummy ], sprintf ('array<int, %s> ' , DummyWithFormatterAttributes::class), 'csv ' , ['hooks ' => [
73
93
'serialize ' => [
74
94
sprintf ('%s::$name ' , DummyWithFormatterAttributes::class) => fn (\ReflectionProperty $ p , string $ accessor ) => [
75
95
'name ' => '@name ' ,
@@ -90,37 +110,57 @@ public static function serializeDeserializeDataProvider(): iterable
90
110
*
91
111
* @param array<string, mixed> $context
92
112
*/
93
- public function testDeserializeSerialize (string $ content , string $ type , array $ context = [])
113
+ public function testDeserializeSerialize (string $ content , string $ type , string $ format , array $ context = [])
94
114
{
95
115
/** @var resource $resource */
96
116
$ resource = fopen ('php://memory ' , 'w+ ' );
97
117
98
118
fwrite ($ resource , $ content );
99
119
rewind ($ resource );
100
120
101
- $ data = deserialize ($ resource , TypeFactory::createFromString ($ type ), ' json ' , $ context );
121
+ $ data = deserialize ($ resource , TypeFactory::createFromString ($ type ), $ format , $ context );
102
122
103
123
/** @var resource $resource */
104
124
$ newResource = fopen ('php://memory ' , 'w+ ' );
105
125
106
- serialize ($ data , $ newResource , ' json ' , ['type ' => TypeFactory::createFromString ($ type )] + $ context );
126
+ serialize ($ data , $ newResource , $ format , ['type ' => TypeFactory::createFromString ($ type )] + $ context );
107
127
rewind ($ newResource );
108
128
109
129
$ this ->assertEquals ($ content , stream_get_contents ($ newResource ));
110
130
}
111
131
112
132
/**
113
- * @return iterable<array{0: string, 1: string, 2: array<string, mixed>}>
133
+ * @return iterable<array{0: string, 1: string, 2: string, 3: array<string, mixed>}>
114
134
*/
115
135
public static function deserializeSerializeDataProvider (): iterable
116
136
{
117
- yield ['1 ' , 'int ' ];
118
- yield ['null ' , '?int ' ];
119
- yield ['"foo" ' , 'string ' ];
120
- yield ['[1,2,null] ' , 'array<int, ?int> ' ];
121
- yield ['{"foo":1,"bar":2,"baz":null} ' , 'array<string, ?int> ' ];
122
- yield ['{"id":100,"name":"Dummy"} ' , ClassicDummy::class];
123
- yield ['{"id":200,"@name":100} ' , DummyWithFormatterAttributes::class, ['hooks ' => [
137
+ yield ['1 ' , 'int ' , 'json ' ];
138
+ yield ['null ' , '?int ' , 'json ' ];
139
+ yield ['"foo" ' , 'string ' , 'json ' ];
140
+ yield ['[1,2,null] ' , 'array<int, ?int> ' , 'json ' ];
141
+ yield ['{"foo":1,"bar":2,"baz":null} ' , 'array<string, ?int> ' , 'json ' ];
142
+ yield ['{"id":100,"name":"Dummy"} ' , ClassicDummy::class, 'json ' ];
143
+ yield ['{"id":200,"@name":100} ' , DummyWithFormatterAttributes::class, 'json ' , ['hooks ' => [
144
+ 'serialize ' => [
145
+ sprintf ('%s::$name ' , DummyWithFormatterAttributes::class) => fn (\ReflectionProperty $ p , string $ accessor ) => [
146
+ 'name ' => '@name ' ,
147
+ 'accessor ' => sprintf ('%s::divideAndCastToInt(%s, $context) ' , DummyWithFormatterAttributes::class, $ accessor ),
148
+ ],
149
+ ],
150
+ 'deserialize ' => [
151
+ sprintf ('%s[@name] ' , DummyWithFormatterAttributes::class) => fn (\ReflectionClass $ class , string $ key , callable $ value , array $ context ) => [
152
+ 'name ' => 'name ' ,
153
+ 'value_provider ' => fn () => DummyWithFormatterAttributes::doubleAndCastToString ($ value ('int ' , $ context )),
154
+ ],
155
+ ],
156
+ ]]];
157
+
158
+ yield ["0 \n1 \n" , 'array<int, int> ' , 'csv ' ];
159
+ yield ["0 \n1 \n2 \n" , 'array<int, ?int> ' , 'csv ' ];
160
+ yield ["0 \nfoo \n" , 'array<int, string> ' , 'csv ' ];
161
+ yield ["foo,bar \n1, \n,2 \n" , 'array<int, array<string, ?int>> ' , 'csv ' ];
162
+ yield ["id,name \n100,Dummy \n" , sprintf ('array<int, %s> ' , ClassicDummy::class), 'csv ' ];
163
+ yield ["id,@name \n200,100 \n" , sprintf ('array<int, %s> ' , DummyWithFormatterAttributes::class), 'csv ' , ['hooks ' => [
124
164
'serialize ' => [
125
165
sprintf ('%s::$name ' , DummyWithFormatterAttributes::class) => fn (\ReflectionProperty $ p , string $ accessor ) => [
126
166
'name ' => '@name ' ,
0 commit comments