From aa24e4ce15e2ac5e66c2100d95586d823b17625a Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Sat, 22 Sep 2018 10:46:58 +0200 Subject: [PATCH] [VarDumper] add caster for MessageFormatter --- .../Component/VarDumper/Caster/IntlCaster.php | 37 +++++++++++++++++++ .../VarDumper/Cloner/AbstractCloner.php | 2 + .../VarDumper/Tests/Caster/IntlCasterTest.php | 36 ++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 src/Symfony/Component/VarDumper/Caster/IntlCaster.php create mode 100644 src/Symfony/Component/VarDumper/Tests/Caster/IntlCasterTest.php diff --git a/src/Symfony/Component/VarDumper/Caster/IntlCaster.php b/src/Symfony/Component/VarDumper/Caster/IntlCaster.php new file mode 100644 index 000000000000..ba2a70e34ed3 --- /dev/null +++ b/src/Symfony/Component/VarDumper/Caster/IntlCaster.php @@ -0,0 +1,37 @@ + + * + * 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; + +/** + * @author Nicolas Grekas + */ +class IntlCaster +{ + public static function castMessageFormatter(\MessageFormatter $c, array $a, Stub $stub, $isNested) + { + $a += array( + Caster::PREFIX_VIRTUAL.'locale' => $c->getLocale(), + Caster::PREFIX_VIRTUAL.'pattern' => $c->getPattern(), + ); + + if ($errorCode = $c->getErrorCode()) { + $a += array( + Caster::PREFIX_VIRTUAL.'error_code' => $errorCode, + Caster::PREFIX_VIRTUAL.'error_message' => $c->getErrorMessage(), + ); + } + + return $a; + } +} diff --git a/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php b/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php index 0a8311a1f7de..04662152eb25 100644 --- a/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php +++ b/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php @@ -117,6 +117,8 @@ abstract class AbstractCloner implements ClonerInterface 'GMP' => array('Symfony\Component\VarDumper\Caster\GmpCaster', 'castGmp'), + 'MessageFormatter' => array('Symfony\Component\VarDumper\Caster\IntlCaster', 'castMessageFormatter'), + ':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/IntlCasterTest.php b/src/Symfony/Component/VarDumper/Tests/Caster/IntlCasterTest.php new file mode 100644 index 000000000000..cfa611085c7c --- /dev/null +++ b/src/Symfony/Component/VarDumper/Tests/Caster/IntlCasterTest.php @@ -0,0 +1,36 @@ + + * + * 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\Test\VarDumperTestTrait; + +/** + * @requires extension intl + */ +class IntlCasterTest extends TestCase +{ + use VarDumperTestTrait; + + public function testArrayIterator() + { + $var = new \MessageFormatter('en', 'Hello {name}'); + + $expected = <<assertDumpEquals($expected, $var); + } +}