From eab631fc454791dc144f650c780e51c32412c165 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1chym=20Tou=C5=A1ek?= Date: Wed, 20 Feb 2019 09:40:26 +0100 Subject: [PATCH] [VarDumper] Implement DsCaster --- src/Symfony/Component/VarDumper/CHANGELOG.md | 7 ++- .../Component/VarDumper/Caster/DsCaster.php | 50 +++++++++++++++++++ .../VarDumper/Cloner/AbstractCloner.php | 8 +++ 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 src/Symfony/Component/VarDumper/Caster/DsCaster.php diff --git a/src/Symfony/Component/VarDumper/CHANGELOG.md b/src/Symfony/Component/VarDumper/CHANGELOG.md index 0ef5e85bdb..5f88029c01 100644 --- a/src/Symfony/Component/VarDumper/CHANGELOG.md +++ b/src/Symfony/Component/VarDumper/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +4.3.0 +----- + + * added `DsCaster` to support dumping the contents of data structures from the Ds extension + 4.2.0 ----- @@ -34,4 +39,4 @@ CHANGELOG 2.7.0 ----- - * deprecated Cloner\Data::getLimitedClone(). Use withMaxDepth, withMaxItemsPerDepth or withRefHandles instead. + * deprecated `Cloner\Data::getLimitedClone()`. Use `withMaxDepth`, `withMaxItemsPerDepth` or `withRefHandles` instead. diff --git a/src/Symfony/Component/VarDumper/Caster/DsCaster.php b/src/Symfony/Component/VarDumper/Caster/DsCaster.php new file mode 100644 index 0000000000..1140b099e7 --- /dev/null +++ b/src/Symfony/Component/VarDumper/Caster/DsCaster.php @@ -0,0 +1,50 @@ + + * + * 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 Ds\Deque; +use Ds\Map; +use Ds\PriorityQueue; +use Ds\Queue; +use Ds\Set; +use Ds\Stack; +use Ds\Vector; +use Symfony\Component\VarDumper\Cloner\Stub; + +/** + * Casts Ds extension classes to array representation. + * + * @author Jáchym Toušek + */ +class DsCaster +{ + /** + * @param Set|Deque|Vector|Stack|Queue|PriorityQueue $c + */ + public static function castDs($c, array $a, Stub $stub, bool $isNested): array + { + $prefix = Caster::PREFIX_VIRTUAL; + $a = $c->toArray(); + $a[$prefix.'capacity'] = $c->capacity(); + + return $a; + } + + public static function castMap(Map $c, array $a, Stub $stub, bool $isNested): array + { + $prefix = Caster::PREFIX_VIRTUAL; + $a = $c->pairs()->toArray(); + $a[$prefix.'capacity'] = $c->capacity(); + + return $a; + } +} diff --git a/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php b/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php index 80b5d548fb..3b41c6781b 100644 --- a/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php +++ b/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php @@ -125,6 +125,14 @@ abstract class AbstractCloner implements ClonerInterface 'Memcached' => ['Symfony\Component\VarDumper\Caster\MemcachedCaster', 'castMemcached'], + 'Ds\Set' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castDs'], + 'Ds\Vector' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castDs'], + 'Ds\Deque' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castDs'], + 'Ds\Stack' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castDs'], + 'Ds\Queue' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castDs'], + 'Ds\PriorityQueue' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castDs'], + 'Ds\Map' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castMap'], + ':curl' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castCurl'], ':dba' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castDba'], ':dba persistent' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castDba'],