[PropertyInfo] Use rawurlencode to escape PSR-6 keys

This commit is contained in:
Kévin Dunglas 2017-07-19 21:33:00 +02:00
parent 4b4f831769
commit ab91659ad6
No known key found for this signature in database
GPG Key ID: 4D04EBEF06AAF3A6
2 changed files with 2 additions and 51 deletions

View File

@ -106,7 +106,8 @@ class PropertyInfoCacheExtractor implements PropertyInfoExtractorInterface
return call_user_func_array(array($this->propertyInfoExtractor, $method), $arguments);
}
$key = $this->escape($method.'.'.$serializedArguments);
// Calling rawurlencode escapes special characters not allowed in PSR-6's keys
$key = rawurlencode($method.'.'.$serializedArguments);
if (array_key_exists($key, $this->arrayCache)) {
return $this->arrayCache[$key];
@ -124,29 +125,4 @@ class PropertyInfoCacheExtractor implements PropertyInfoExtractorInterface
return $this->arrayCache[$key] = $value;
}
/**
* Escapes a key according to PSR-6.
*
* Replaces characters forbidden by PSR-6 and the _ char by the _ char followed by the ASCII
* code of the escaped char.
*
* @param string $key
*
* @return string
*/
private function escape($key)
{
return strtr($key, array(
'{' => '_123',
'}' => '_125',
'(' => '_40',
')' => '_41',
'/' => '_47',
'\\' => '_92',
'@' => '_64',
':' => '_58',
'_' => '_95',
));
}
}

View File

@ -61,29 +61,4 @@ class PropertyInfoCacheExtractorTest extends AbstractPropertyInfoExtractorTest
parent::testGetProperties();
parent::testGetProperties();
}
/**
* @dataProvider escapeDataProvider
*/
public function testEscape($toEscape, $expected)
{
$reflectionMethod = new \ReflectionMethod($this->propertyInfo, 'escape');
$reflectionMethod->setAccessible(true);
$this->assertSame($expected, $reflectionMethod->invoke($this->propertyInfo, $toEscape));
}
public function escapeDataProvider()
{
return array(
array('foo_bar', 'foo_95bar'),
array('foo_95bar', 'foo_9595bar'),
array('foo{bar}', 'foo_123bar_125'),
array('foo(bar)', 'foo_40bar_41'),
array('foo/bar', 'foo_47bar'),
array('foo\bar', 'foo_92bar'),
array('foo@bar', 'foo_64bar'),
array('foo:bar', 'foo_58bar'),
);
}
}