Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 452a273

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 452a273

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed
Lines changed: 36 additions & 0 deletions
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 <[email protected]>
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 <[email protected]>
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

Lines changed: 2 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', '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'),
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\ConstStub;
16+
use Symfony\Component\VarDumper\Caster\GmpCaster;
17+
use Symfony\Component\VarDumper\Cloner\Stub;
18+
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
19+
20+
class GmpCasterTest extends TestCase
21+
{
22+
use VarDumperTestTrait;
23+
24+
/**
25+
* @requires extension gmp
26+
*/
27+
public function testCastInt()
28+
{
29+
$gmpString = gmp_init('1234');
30+
$gmpOctal = gmp_init(010);
31+
$gmp = gmp_init('01101');
32+
$gmpDump = <<<EODUMP
33+
array:1 [
34+
"\\x00~\\x00gmpInt" => %s
35+
]
36+
EODUMP;
37+
$this->assertDumpEquals(sprintf($gmpDump, $gmpString), GmpCaster::castInt($gmpString, array(), new Stub(), false, 0));
38+
$this->assertDumpEquals(sprintf($gmpDump, $gmpOctal), GmpCaster::castInt($gmpOctal, array(), new Stub(), false, 0));
39+
$this->assertDumpEquals(sprintf($gmpDump, $gmp), GmpCaster::castInt($gmp, array(), new Stub(), false, 0));
40+
}
41+
42+
/**
43+
* @requires extension gmp
44+
*/
45+
public function testCastString()
46+
{
47+
48+
$gmpString = gmp_init('1234');
49+
$gmpOctal = gmp_init(010);
50+
$gmp = gmp_init('01101');
51+
$gmpDump = <<<EODUMP
52+
array:1 [
53+
"\\x00~\\x00gmpString" => "%s"
54+
]
55+
EODUMP;
56+
$this->assertDumpEquals(sprintf($gmpDump, $gmpString), GmpCaster::castGmp($gmpString, array(), new Stub(), false, 0));
57+
$this->assertDumpEquals(sprintf($gmpDump, $gmpOctal), GmpCaster::castGmp($gmpOctal, array(), new Stub(), false, 0));
58+
$this->assertDumpEquals(sprintf($gmpDump, $gmp), GmpCaster::castGmp($gmp, array(), new Stub(), false, 0));
59+
}
60+
}

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

Lines changed: 56 additions & 0 deletions
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 <[email protected]>
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)