Skip to content

Commit dd04bd0

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 dd04bd0

File tree

4 files changed

+135
-0
lines changed

4 files changed

+135
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
/**
15+
* Transform a GMP object to a integer or a string. It need the gmp php extension.
16+
*
17+
* @author Amrouche Hamza <hamza.simperfit@gmail.com>
18+
*/
19+
class GmpCaster
20+
{
21+
public static function castInt(\GMP $value): array
22+
{
23+
return array('value' => gmp_intval($value));
24+
}
25+
26+
public static function castString(\GMP $value): array
27+
{
28+
return array('value' => gmp_strval($value));
29+
}
30+
}

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

Lines changed: 5 additions & 0 deletions
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', 'castInt'),
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'),
@@ -126,6 +128,9 @@ abstract class AbstractCloner implements ClonerInterface
126128
':persistent stream' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castStream'),
127129
':stream-context' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castStreamContext'),
128130
':xml' => array('Symfony\Component\VarDumper\Caster\XmlResourceCaster', 'castXml'),
131+
':gmp' => array('Symfony\Component\VarDumper\Caster\GmpCaster', 'castInt'),
132+
':gmp int' => array('Symfony\Component\VarDumper\Caster\GmpCaster', 'castInt'),
133+
':gmp string' => array('Symfony\Component\VarDumper\Caster\GmpCaster', 'castString'),
129134
);
130135

131136
protected $maxItems = 2500;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
17+
class GmpCasterTest extends TestCase
18+
{
19+
public function testCastInt()
20+
{
21+
if (false === extension_loaded('gmp')) {
22+
$this->markTestSkipped('GMP PHP extension should be loaded to run be able to parse GMP resources');
23+
}
24+
25+
$gmpString = gmp_init('1234');
26+
$gmpOctal = gmp_init(010);
27+
$gmp = gmp_init('01101');
28+
29+
$this->assertEquals(1234, GmpCaster::castInt($gmpString));
30+
$this->assertEquals(8, GmpCaster::castInt($gmpOctal));
31+
$this->assertEquals(577, GmpCaster::castInt($gmp));
32+
}
33+
34+
public function testCastString()
35+
{
36+
if (false === extension_loaded('gmp')) {
37+
$this->markTestSkipped('GMP PHP extension should be loaded to run be able to parse GMP resources');
38+
}
39+
40+
$gmpString = gmp_init('1234');
41+
$gmpOctal = gmp_init(010);
42+
$gmp = gmp_init('01101');
43+
44+
$this->assertEquals('1234', GmpCaster::castString($gmpString));
45+
$this->assertEquals('8', GmpCaster::castString($gmpOctal));
46+
$this->assertEquals('577', GmpCaster::castString($gmp));
47+
}
48+
}

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,58 @@ public function testJsonCast()
379379
$this->assertStringMatchesFormat(\PHP_VERSION_ID >= 70200 ? str_replace('"1"', '1', $expected) : $expected, ob_get_clean());
380380
}
381381

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

0 commit comments

Comments
 (0)