Skip to content

Commit 2a4413b

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 2a4413b

File tree

4 files changed

+139
-1
lines changed

4 files changed

+139
-1
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: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,61 @@ public function testJsonCast()
379379
$this->assertStringMatchesFormat(\PHP_VERSION_ID >= 70200 ? str_replace('"1"', '1', $expected) : $expected, ob_get_clean());
380380
}
381381

382+
/**
383+
* @group gmp
384+
*/
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 = <<<EOTXT
395+
Symfony\Component\VarDumper\Cloner\Data Object
396+
(
397+
[data:Symfony\Component\VarDumper\Cloner\Data:private] => Array
398+
(
399+
[0] => Array
400+
(
401+
[0] => Symfony\Component\VarDumper\Cloner\Stub Object
402+
(
403+
[type] => 4
404+
[class] => GMP
405+
[value] =>
406+
[cut] => 0
407+
[handle] => 22
408+
[refCount] => 0
409+
[position] => 1
410+
[attr] => Array
411+
(
412+
)
413+
414+
)
415+
416+
)
417+
418+
[1] => Array
419+
(
420+
[value] => 65
421+
)
422+
423+
)
424+
425+
[position:Symfony\Component\VarDumper\Cloner\Data:private] => 0
426+
[key:Symfony\Component\VarDumper\Cloner\Data:private] => 0
427+
[maxDepth:Symfony\Component\VarDumper\Cloner\Data:private] => 20
428+
[maxItemsPerDepth:Symfony\Component\VarDumper\Cloner\Data:private] => -1
429+
[useRefHandles:Symfony\Component\VarDumper\Cloner\Data:private] => -1
430+
)
431+
432+
EOTXT;
433+
434+
$this->assertStringMatchesFormat($expected, print_r($clone, true));
435+
}
436+
382437
public function testCaster()
383438
{
384439
$cloner = new VarCloner(array(
@@ -404,7 +459,7 @@ public function testCaster()
404459
(
405460
[type] => 4
406461
[class] => %s
407-
[value] =>
462+
[value] =>
408463
[cut] => 0
409464
[handle] => %i
410465
[refCount] => 0

0 commit comments

Comments
 (0)