Skip to content

Commit d852d34

Browse files
committed
Variable::toCode
1 parent 0e51ba0 commit d852d34

8 files changed

Lines changed: 275 additions & 114 deletions

File tree

src/Schema/AbstractSchemaInnerParse.php

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -123,33 +123,4 @@ final protected function getDataType(mixed $input): string
123123
{
124124
return \is_object($input) ? $input::class : \gettype($input);
125125
}
126-
127-
final protected function varExport(mixed $input): string
128-
{
129-
if ($input instanceof \DateTimeInterface) {
130-
return 'new \\'.$input::class.'(\''.$input->format('c').'\')';
131-
}
132-
133-
if ($input instanceof \BackedEnum) {
134-
return '\\'.$input::class.'::from('.$this->varExport($input->value).')';
135-
}
136-
137-
if ($input instanceof \stdClass) {
138-
return '(object) '.$this->varExport((array) $input);
139-
}
140-
141-
if (\is_array($input)) {
142-
return '['.implode(', ', array_map(
143-
fn (int|string $key, mixed $value) => $this->varExport($key).' => '.$this->varExport($value),
144-
array_keys($input),
145-
$input
146-
)).']';
147-
}
148-
149-
if (null === $input) {
150-
return 'null';
151-
}
152-
153-
return var_export($input, true);
154-
}
155126
}

src/Schema/ArraySchema.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Chubbyphp\Parsing\Error;
88
use Chubbyphp\Parsing\Errors;
99
use Chubbyphp\Parsing\ErrorsException;
10+
use Chubbyphp\Parsing\Variable;
1011

1112
final class ArraySchema extends AbstractSchemaInnerParse implements SchemaInterface
1213
{
@@ -113,7 +114,7 @@ public function maxItems(int $maxItems): static
113114
*/
114115
public function length(int $length): static
115116
{
116-
@trigger_error('Use exactItems('.$this->varExport($length).') instead', E_USER_DEPRECATED);
117+
@trigger_error('Use exactItems('.Variable::toCode($length).') instead', E_USER_DEPRECATED);
117118

118119
return $this->postParse(static function (array $array) use ($length) {
119120
$arrayLength = \count($array);
@@ -137,7 +138,7 @@ public function length(int $length): static
137138
*/
138139
public function minLength(int $minLength): static
139140
{
140-
@trigger_error('Use minItems('.$this->varExport($minLength).') instead', E_USER_DEPRECATED);
141+
@trigger_error('Use minItems('.Variable::toCode($minLength).') instead', E_USER_DEPRECATED);
141142

142143
return $this->postParse(static function (array $array) use ($minLength) {
143144
$arrayLength = \count($array);
@@ -161,7 +162,7 @@ public function minLength(int $minLength): static
161162
*/
162163
public function maxLength(int $maxLength): static
163164
{
164-
@trigger_error('Use maxItems('.$this->varExport($maxLength).') instead', E_USER_DEPRECATED);
165+
@trigger_error('Use maxItems('.Variable::toCode($maxLength).') instead', E_USER_DEPRECATED);
165166

166167
return $this->postParse(static function (array $array) use ($maxLength) {
167168
$arrayLength = \count($array);
@@ -202,7 +203,7 @@ public function contains(mixed $contains, bool $strict = true): static
202203
*/
203204
public function includes(mixed $includes, bool $strict = true): static
204205
{
205-
@trigger_error('Use contains('.$this->varExport($includes).', '.$this->varExport($strict).') instead', E_USER_DEPRECATED);
206+
@trigger_error('Use contains('.Variable::toCode($includes).', '.Variable::toCode($strict).') instead', E_USER_DEPRECATED);
206207

207208
return $this->postParse(static function (array $array) use ($includes, $strict) {
208209
if (!\in_array($includes, $array, $strict)) {

src/Schema/FloatSchema.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Chubbyphp\Parsing\Error;
88
use Chubbyphp\Parsing\ErrorsException;
9+
use Chubbyphp\Parsing\Variable;
910

1011
final class FloatSchema extends AbstractSchemaInnerParse implements SchemaInterface
1112
{
@@ -124,7 +125,7 @@ public function maximum(float $maximum): static
124125
*/
125126
public function gte(float $gte): static
126127
{
127-
@trigger_error('Use minimum('.$this->varExport($gte).') instead', E_USER_DEPRECATED);
128+
@trigger_error('Use minimum('.Variable::toCode($gte).') instead', E_USER_DEPRECATED);
128129

129130
return $this->postParse(static function (float $float) use ($gte) {
130131
if ($float >= $gte) {
@@ -146,7 +147,7 @@ public function gte(float $gte): static
146147
*/
147148
public function gt(float $gt): static
148149
{
149-
@trigger_error('Use exclusiveMinimum('.$this->varExport($gt).') instead', E_USER_DEPRECATED);
150+
@trigger_error('Use exclusiveMinimum('.Variable::toCode($gt).') instead', E_USER_DEPRECATED);
150151

151152
return $this->postParse(static function (float $float) use ($gt) {
152153
if ($float > $gt) {
@@ -168,7 +169,7 @@ public function gt(float $gt): static
168169
*/
169170
public function lt(float $lt): static
170171
{
171-
@trigger_error('Use exclusiveMaximum('.$this->varExport($lt).') instead', E_USER_DEPRECATED);
172+
@trigger_error('Use exclusiveMaximum('.Variable::toCode($lt).') instead', E_USER_DEPRECATED);
172173

173174
return $this->postParse(static function (float $float) use ($lt) {
174175
if ($float < $lt) {
@@ -190,7 +191,7 @@ public function lt(float $lt): static
190191
*/
191192
public function lte(float $lte): static
192193
{
193-
@trigger_error('Use maximum('.$this->varExport($lte).') instead', E_USER_DEPRECATED);
194+
@trigger_error('Use maximum('.Variable::toCode($lte).') instead', E_USER_DEPRECATED);
194195

195196
return $this->postParse(static function (float $float) use ($lte) {
196197
if ($float <= $lte) {

src/Schema/IntSchema.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Chubbyphp\Parsing\Error;
88
use Chubbyphp\Parsing\ErrorsException;
9+
use Chubbyphp\Parsing\Variable;
910

1011
final class IntSchema extends AbstractSchemaInnerParse implements SchemaInterface
1112
{
@@ -121,7 +122,7 @@ public function maximum(int $maximum): static
121122
*/
122123
public function gte(int $gte): static
123124
{
124-
@trigger_error('Use minimum('.$this->varExport($gte).') instead', E_USER_DEPRECATED);
125+
@trigger_error('Use minimum('.Variable::toCode($gte).') instead', E_USER_DEPRECATED);
125126

126127
return $this->postParse(static function (int $int) use ($gte) {
127128
if ($int >= $gte) {
@@ -143,7 +144,7 @@ public function gte(int $gte): static
143144
*/
144145
public function gt(int $gt): static
145146
{
146-
@trigger_error('Use exclusiveMinimum('.$this->varExport($gt).') instead', E_USER_DEPRECATED);
147+
@trigger_error('Use exclusiveMinimum('.Variable::toCode($gt).') instead', E_USER_DEPRECATED);
147148

148149
return $this->postParse(static function (int $int) use ($gt) {
149150
if ($int > $gt) {
@@ -165,7 +166,7 @@ public function gt(int $gt): static
165166
*/
166167
public function lt(int $lt): static
167168
{
168-
@trigger_error('Use exclusiveMaximum('.$this->varExport($lt).') instead', E_USER_DEPRECATED);
169+
@trigger_error('Use exclusiveMaximum('.Variable::toCode($lt).') instead', E_USER_DEPRECATED);
169170

170171
return $this->postParse(static function (int $int) use ($lt) {
171172
if ($int < $lt) {
@@ -187,7 +188,7 @@ public function lt(int $lt): static
187188
*/
188189
public function lte(int $lte): static
189190
{
190-
@trigger_error('Use maximum('.$this->varExport($lte).') instead', E_USER_DEPRECATED);
191+
@trigger_error('Use maximum('.Variable::toCode($lte).') instead', E_USER_DEPRECATED);
191192

192193
return $this->postParse(static function (int $int) use ($lte) {
193194
if ($int <= $lte) {

src/Schema/StringSchema.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Chubbyphp\Parsing\Enum\Uuid;
88
use Chubbyphp\Parsing\Error;
99
use Chubbyphp\Parsing\ErrorsException;
10+
use Chubbyphp\Parsing\Variable;
1011

1112
final class StringSchema extends AbstractSchemaInnerParse implements SchemaInterface
1213
{
@@ -175,7 +176,7 @@ public function contains(string $contains): static
175176
*/
176177
public function includes(string $includes): static
177178
{
178-
@trigger_error('Use contains('.$this->varExport($includes).') instead', E_USER_DEPRECATED);
179+
@trigger_error('Use contains('.Variable::toCode($includes).') instead', E_USER_DEPRECATED);
179180

180181
return $this->postParse(static function (string $string) use ($includes) {
181182
if (str_contains($string, $includes)) {
@@ -338,7 +339,7 @@ public function mac(): static
338339
*/
339340
public function match(string $match): static
340341
{
341-
@trigger_error('Use pattern('.$this->varExport($match).') instead', E_USER_DEPRECATED);
342+
@trigger_error('Use pattern('.Variable::toCode($match).') instead', E_USER_DEPRECATED);
342343

343344
if (false === @preg_match($match, '')) {
344345
throw new \InvalidArgumentException(\sprintf('Invalid match "%s" given', $match));
@@ -389,7 +390,7 @@ public function pattern(string $pattern): static
389390
*/
390391
public function regexp(string $regexp): static
391392
{
392-
@@trigger_error('Use pattern('.$this->varExport($regexp).') instead', E_USER_DEPRECATED);
393+
@@trigger_error('Use pattern('.Variable::toCode($regexp).') instead', E_USER_DEPRECATED);
393394

394395
if (false === @preg_match($regexp, '')) {
395396
throw new \InvalidArgumentException(\sprintf('Invalid regexp "%s" given', $regexp));

src/Variable.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Chubbyphp\Parsing;
6+
7+
final class Variable
8+
{
9+
public static function toCode(mixed $variable, int $level = 1): string
10+
{
11+
if ($variable instanceof \DateTimeInterface) {
12+
return 'new \\'.$variable::class.'(\''.$variable->format('c').'\')';
13+
}
14+
15+
if ($variable instanceof \BackedEnum) {
16+
return '\\'.$variable::class.'::from('.self::toCode($variable->value).')';
17+
}
18+
19+
if ($variable instanceof \UnitEnum) {
20+
return '\\'.$variable::class.'::'.$variable->name;
21+
}
22+
23+
if ($variable instanceof \stdClass) {
24+
return '(object) '.self::toCode((array) $variable, $level);
25+
}
26+
27+
if (\is_object($variable)) {
28+
return '<\\'.$variable::class.'>';
29+
}
30+
31+
if (\is_resource($variable)) {
32+
return '<resource>';
33+
}
34+
35+
if (\is_array($variable)) {
36+
return '['.PHP_EOL.implode('', array_map(
37+
static fn (int|string $key, mixed $value) => str_repeat(' ', $level * 4).self::toCode($key).' => '.self::toCode($value, $level + 1).','.PHP_EOL,
38+
array_keys($variable),
39+
$variable
40+
)).str_repeat(' ', ($level - 1) * 4).']';
41+
}
42+
43+
if (null === $variable) {
44+
return 'null';
45+
}
46+
47+
return var_export($variable, true);
48+
}
49+
}

tests/Unit/Schema/AbstractSchemaInnerParseTest.php

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,6 @@
99
use Chubbyphp\Parsing\Schema\AbstractSchemaInnerParse;
1010
use PHPUnit\Framework\TestCase;
1111

12-
enum AbstractSchemaInnerParseEnum: string
13-
{
14-
case Hearts = 'H';
15-
case Diamonds = 'D';
16-
case Clubs = 'C';
17-
case Spades = 'S';
18-
}
19-
2012
/**
2113
* @covers \Chubbyphp\Parsing\Schema\AbstractSchemaInnerParse
2214
*
@@ -154,66 +146,4 @@ protected function innerParse(mixed $input): mixed
154146
self::assertSame(\stdClass::class, $e->errors->jsonSerialize()[0]['error']['variables']['given']);
155147
}
156148
}
157-
158-
public function testVarExport(): void
159-
{
160-
$schema = new class extends AbstractSchemaInnerParse {
161-
protected function innerParse(mixed $input): string
162-
{
163-
return $this->varExport($input);
164-
}
165-
};
166-
167-
self::assertSame('null', $schema->parse(null));
168-
self::assertSame('true', $schema->parse(true));
169-
self::assertSame('false', $schema->parse(false));
170-
self::assertSame('1', $schema->parse(1));
171-
self::assertSame('1.234', $schema->parse(1.234));
172-
self::assertSame('1200.0', $schema->parse(1.2e3));
173-
self::assertSame('7.0E-10', $schema->parse(7E-10));
174-
self::assertSame('1234.567', $schema->parse(1_234.567));
175-
self::assertSame("'test'", $schema->parse('test'));
176-
self::assertSame(
177-
"new \\DateTimeImmutable('2024-01-20T09:15:00+00:00')",
178-
$schema->parse(new \DateTimeImmutable('2024-01-20T09:15:00+00:00'))
179-
);
180-
self::assertSame(
181-
'\Chubbyphp\Tests\Parsing\Unit\Schema\AbstractSchemaInnerParseEnum::from(\'H\')',
182-
$schema->parse(AbstractSchemaInnerParseEnum::Hearts)
183-
);
184-
185-
self::assertSame(
186-
"['null' => null, 'true' => true, 'false' => 'false', 1 => 1, '1.234' => 1.234, '1.2e3' => 1200.0, '7E-10' => 7.0E-10, '1_234.567' => 1234.567, 'test' => 'test', '2024-01-20T09:15:00+00:00' => new \\DateTimeImmutable('2024-01-20T09:15:00+00:00'), 'AbstractSchemaInnerParseEnum::Hearts' => \\Chubbyphp\\Tests\\Parsing\\Unit\\Schema\\AbstractSchemaInnerParseEnum::from('H')]",
187-
$schema->parse([
188-
'null' => null,
189-
'true' => true,
190-
'false' => 'false',
191-
'1' => 1,
192-
'1.234' => 1.234,
193-
'1.2e3' => 1.2e3,
194-
'7E-10' => 7E-10,
195-
'1_234.567' => 1_234.567,
196-
'test' => 'test',
197-
'2024-01-20T09:15:00+00:00' => new \DateTimeImmutable('2024-01-20T09:15:00+00:00'),
198-
'AbstractSchemaInnerParseEnum::Hearts' => AbstractSchemaInnerParseEnum::Hearts,
199-
])
200-
);
201-
202-
self::assertSame(
203-
"(object) ['null' => null, 'true' => true, 'false' => 'false', 1 => 1, '1.234' => 1.234, '1.2e3' => 1200.0, '7E-10' => 7.0E-10, '1_234.567' => 1234.567, 'test' => 'test', '2024-01-20T09:15:00+00:00' => new \\DateTimeImmutable('2024-01-20T09:15:00+00:00'), 'AbstractSchemaInnerParseEnum::Hearts' => \\Chubbyphp\\Tests\\Parsing\\Unit\\Schema\\AbstractSchemaInnerParseEnum::from('H')]",
204-
$schema->parse((object) [
205-
'null' => null,
206-
'true' => true,
207-
'false' => 'false',
208-
'1' => 1,
209-
'1.234' => 1.234,
210-
'1.2e3' => 1.2e3,
211-
'7E-10' => 7E-10,
212-
'1_234.567' => 1_234.567,
213-
'test' => 'test',
214-
'2024-01-20T09:15:00+00:00' => new \DateTimeImmutable('2024-01-20T09:15:00+00:00'),
215-
'AbstractSchemaInnerParseEnum::Hearts' => AbstractSchemaInnerParseEnum::Hearts,
216-
])
217-
);
218-
}
219149
}

0 commit comments

Comments
 (0)