Skip to content

Commit 516e25d

Browse files
author
Amrouche Hamza
committed
[VarDumper] add a GMP caster in order to cast GMP resources into string or integer
1 parent bf4b09f commit 516e25d

File tree

4 files changed

+152
-0
lines changed

4 files changed

+152
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\VarDumper\Caster;
13+
14+
use Symfony\Component\VarDumper\Cloner\Stub;
15+
16+
/**
17+
* Transform a GMP object to a integer or a string. It need the gmp php extension.
18+
*
19+
* @author Amrouche Hamza <hamza.simperfit@gmail.com>
20+
*/
21+
class GmpCaster
22+
{
23+
public static function castInt(\GMP $gmp, array $a, Stub $stub, $isNested, $filter): array
24+
{
25+
$a[Caster::PREFIX_VIRTUAL.'gmpInt'] = gmp_intval($gmp);
26+
27+
return $a;
28+
}
29+
30+
public static function castGmp(\GMP $gmp, array $a, Stub $stub, $isNested, $filter): array
31+
{
32+
$a[Caster::PREFIX_VIRTUAL.'gmpString'] = (string) new ConstStub(\GMP::class, gmp_strval($gmp));
33+
34+
return $a;
35+
}
36+
}

src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php

+2
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ abstract class AbstractCloner implements ClonerInterface
112112
'DateTimeZone' => array('Symfony\Component\VarDumper\Caster\DateCaster', 'castTimeZone'),
113113
'DatePeriod' => array('Symfony\Component\VarDumper\Caster\DateCaster', 'castPeriod'),
114114

115+
'GMP' => array('Symfony\Component\VarDumper\Caster\GmpCaster', 'castGmp'),
116+
115117
':curl' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castCurl'),
116118
':dba' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castDba'),
117119
':dba persistent' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castDba'),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\VarDumper\Tests\Caster;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\VarDumper\Caster\GmpCaster;
16+
use Symfony\Component\VarDumper\Cloner\Stub;
17+
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
18+
19+
class GmpCasterTest extends TestCase
20+
{
21+
use VarDumperTestTrait;
22+
23+
/**
24+
* @requires extension gmp
25+
*/
26+
public function testCastInt()
27+
{
28+
$gmpString = gmp_init('1234');
29+
$gmpOctal = gmp_init(010);
30+
$gmp = gmp_init('01101');
31+
$gmpDump = <<<EODUMP
32+
array:1 [
33+
"\\x00~\\x00gmpInt" => %s
34+
]
35+
EODUMP;
36+
$this->assertDumpEquals(sprintf($gmpDump, $gmpString), GmpCaster::castInt($gmpString, array(), new Stub(), false, 0));
37+
$this->assertDumpEquals(sprintf($gmpDump, $gmpOctal), GmpCaster::castInt($gmpOctal, array(), new Stub(), false, 0));
38+
$this->assertDumpEquals(sprintf($gmpDump, $gmp), GmpCaster::castInt($gmp, array(), new Stub(), false, 0));
39+
}
40+
41+
/**
42+
* @requires extension gmp
43+
*/
44+
public function testCastString()
45+
{
46+
$gmpString = gmp_init('1234');
47+
$gmpOctal = gmp_init(010);
48+
$gmp = gmp_init('01101');
49+
$gmpDump = <<<EODUMP
50+
array:1 [
51+
"\\x00~\\x00gmpString" => "%s"
52+
]
53+
EODUMP;
54+
$this->assertDumpEquals(sprintf($gmpDump, $gmpString), GmpCaster::castGmp($gmpString, array(), new Stub(), false, 0));
55+
$this->assertDumpEquals(sprintf($gmpDump, $gmpOctal), GmpCaster::castGmp($gmpOctal, array(), new Stub(), false, 0));
56+
$this->assertDumpEquals(sprintf($gmpDump, $gmp), GmpCaster::castGmp($gmp, array(), new Stub(), false, 0));
57+
}
58+
}

src/Symfony/Component/VarDumper/Tests/Cloner/VarClonerTest.php

+56
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\VarDumper\Cloner\VarCloner;
16+
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
1617

1718
/**
1819
* @author Nicolas Grekas <p@tchwork.com>
1920
*/
2021
class VarClonerTest extends TestCase
2122
{
23+
use VarDumperTestTrait;
24+
2225
public function testMaxIntBoundary()
2326
{
2427
$data = array(PHP_INT_MAX => 123);
@@ -379,6 +382,59 @@ public function testJsonCast()
379382
$this->assertStringMatchesFormat(\PHP_VERSION_ID >= 70200 ? str_replace('"1"', '1', $expected) : $expected, ob_get_clean());
380383
}
381384

385+
public function testGmpCast()
386+
{
387+
if (false === extension_loaded('gmp')) {
388+
$this->markTestSkipped('GMP PHP extension should be loaded to be able to parse GMP resources');
389+
}
390+
$data = gmp_init('0101');
391+
$cloner = new VarCloner();
392+
393+
$clone = $cloner->cloneVar($data);
394+
$expected = <<<'EODUMP'
395+
"""
396+
Symfony\Component\VarDumper\Cloner\Data Object\n
397+
(\n
398+
[data:Symfony\Component\VarDumper\Cloner\Data:private] => Array\n
399+
(\n
400+
[0] => Array\n
401+
(\n
402+
[0] => Symfony\Component\VarDumper\Cloner\Stub Object\n
403+
(\n
404+
[type] => 4\n
405+
[class] => GMP\n
406+
[value] => \n
407+
[cut] => 0\n
408+
[handle] => 31\n
409+
[refCount] => 0\n
410+
[position] => 1\n
411+
[attr] => Array\n
412+
(\n
413+
)\n
414+
\n
415+
)\n
416+
\n
417+
)\n
418+
\n
419+
[1] => Array\n
420+
(\n
421+
[\x00~\x00gmpString] => 65\n
422+
)\n
423+
\n
424+
)\n
425+
\n
426+
[position:Symfony\Component\VarDumper\Cloner\Data:private] => 0\n
427+
[key:Symfony\Component\VarDumper\Cloner\Data:private] => 0\n
428+
[maxDepth:Symfony\Component\VarDumper\Cloner\Data:private] => 20\n
429+
[maxItemsPerDepth:Symfony\Component\VarDumper\Cloner\Data:private] => -1\n
430+
[useRefHandles:Symfony\Component\VarDumper\Cloner\Data:private] => -1\n
431+
)\n
432+
"""
433+
EODUMP;
434+
435+
$this->assertDumpEquals($expected, print_r($clone, true));
436+
}
437+
382438
public function testCaster()
383439
{
384440
$cloner = new VarCloner(array(

0 commit comments

Comments
 (0)