From ed2c1af26bd4f0e2bb6c8c73babb9cafa24139fd Mon Sep 17 00:00:00 2001 From: Amrouche Hamza Date: Fri, 1 Dec 2017 08:42:39 +0100 Subject: [PATCH] [VarDumper] add a GMP caster in order to cast GMP resources into string or integer --- .../Component/VarDumper/Caster/GmpCaster.php | 30 ++++++++++++ .../VarDumper/Cloner/AbstractCloner.php | 2 + .../VarDumper/Tests/Caster/GmpCasterTest.php | 48 +++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 src/Symfony/Component/VarDumper/Caster/GmpCaster.php create mode 100644 src/Symfony/Component/VarDumper/Tests/Caster/GmpCasterTest.php diff --git a/src/Symfony/Component/VarDumper/Caster/GmpCaster.php b/src/Symfony/Component/VarDumper/Caster/GmpCaster.php new file mode 100644 index 0000000000..504dc07886 --- /dev/null +++ b/src/Symfony/Component/VarDumper/Caster/GmpCaster.php @@ -0,0 +1,30 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\VarDumper\Caster; + +use Symfony\Component\VarDumper\Cloner\Stub; + +/** + * Casts GMP objects to array representation. + * + * @author Hamza Amrouche + * @author Nicolas Grekas + */ +class GmpCaster +{ + public static function castGmp(\GMP $gmp, array $a, Stub $stub, $isNested, $filter): array + { + $a[Caster::PREFIX_VIRTUAL.'value'] = new ConstStub(gmp_strval($gmp), gmp_strval($gmp)); + + return $a; + } +} diff --git a/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php b/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php index c8e6929c6e..da981cee8e 100644 --- a/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php +++ b/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php @@ -112,6 +112,8 @@ abstract class AbstractCloner implements ClonerInterface 'DateTimeZone' => array('Symfony\Component\VarDumper\Caster\DateCaster', 'castTimeZone'), 'DatePeriod' => array('Symfony\Component\VarDumper\Caster\DateCaster', 'castPeriod'), + 'GMP' => array('Symfony\Component\VarDumper\Caster\GmpCaster', 'castGmp'), + ':curl' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castCurl'), ':dba' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castDba'), ':dba persistent' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castDba'), diff --git a/src/Symfony/Component/VarDumper/Tests/Caster/GmpCasterTest.php b/src/Symfony/Component/VarDumper/Tests/Caster/GmpCasterTest.php new file mode 100644 index 0000000000..1ddf897461 --- /dev/null +++ b/src/Symfony/Component/VarDumper/Tests/Caster/GmpCasterTest.php @@ -0,0 +1,48 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\VarDumper\Tests\Caster; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\VarDumper\Caster\GmpCaster; +use Symfony\Component\VarDumper\Cloner\Stub; +use Symfony\Component\VarDumper\Test\VarDumperTestTrait; + +class GmpCasterTest extends TestCase +{ + use VarDumperTestTrait; + + /** + * @requires extension gmp + */ + public function testCastGmp() + { + $gmpString = gmp_init('1234'); + $gmpOctal = gmp_init(010); + $gmp = gmp_init('01101'); + $gmpDump = << %s +] +EODUMP; + $this->assertDumpEquals(sprintf($gmpDump, $gmpString), GmpCaster::castGmp($gmpString, array(), new Stub(), false, 0)); + $this->assertDumpEquals(sprintf($gmpDump, $gmpOctal), GmpCaster::castGmp($gmpOctal, array(), new Stub(), false, 0)); + $this->assertDumpEquals(sprintf($gmpDump, $gmp), GmpCaster::castGmp($gmp, array(), new Stub(), false, 0)); + + $dump = <<assertDumpEquals($dump, $gmp); + } +}