Skip to content

Commit

Permalink
Merge branch '2.3'
Browse files Browse the repository at this point in the history
* 2.3:
  [Locale] added support for the position argument to NumberFormatter::parse()
  [Locale] added some more stubs for the number formatter
  [Yaml] fixed typo
  [Yaml] fixed a test on PHP < 5.4
  [DomCrawler]Crawler guess charset from html
  fixed PHP 5.3 compatibility
  [Yaml] reverted previous merge partially (refs #8897)
  [Security] remove unused logger
  [Security] fix typo
  [Yaml] Fixed filename in the ParseException message
  • Loading branch information
fabpot committed Sep 22, 2013
2 parents 8aa685b + 775a39c commit b1542f0
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 70 deletions.
Expand Up @@ -241,7 +241,6 @@
<argument type="service" id="security.access.decision_manager" />
<argument type="service" id="security.access_map" />
<argument type="service" id="security.authentication.manager" />
<argument type="service" id="logger" on-invalid="null" />
</service>
</services>
</container>
5 changes: 5 additions & 0 deletions src/Symfony/Component/Console/Input/InputDefinition.php
Expand Up @@ -11,6 +11,11 @@

namespace Symfony\Component\Console\Input;

if (!defined('JSON_UNESCAPED_UNICODE')) {
define('JSON_UNESCAPED_SLASHES', 64);
define('JSON_UNESCAPED_UNICODE', 256);
}

use Symfony\Component\Console\Descriptor\TextDescriptor;
use Symfony\Component\Console\Descriptor\XmlDescriptor;
use Symfony\Component\Console\Output\BufferedOutput;
Expand Down
15 changes: 12 additions & 3 deletions src/Symfony/Component/DomCrawler/Crawler.php
Expand Up @@ -96,19 +96,28 @@ public function addContent($content, $type = null)
}

// DOM only for HTML/XML content
if (!preg_match('/(x|ht)ml/i', $type, $matches)) {
if (!preg_match('/(x|ht)ml/i', $type, $xmlMatches)) {
return null;
}

$charset = 'ISO-8859-1';
$charset = null;
if (false !== $pos = strpos($type, 'charset=')) {
$charset = substr($type, $pos + 8);
if (false !== $pos = strpos($charset, ';')) {
$charset = substr($charset, 0, $pos);
}
}

if ('x' === $matches[1]) {
if (null === $charset &&
preg_match('/\<meta[^\>]+charset *= *["\']?([a-zA-Z\-0-9]+)/i', $content, $matches)) {
$charset = $matches[1];
}

if (null === $charset) {
$charset = 'ISO-8859-1';
}

if ('x' === $xmlMatches[1]) {
$this->addXmlContent($content, $charset);
} else {
$this->addHtmlContent($content, $charset);
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php
Expand Up @@ -216,6 +216,10 @@ public function testAddContent()
$crawler = new Crawler();
$crawler->addContent('foo bar', 'text/plain');
$this->assertCount(0, $crawler, '->addContent() does nothing if the type is not (x|ht)ml');

$crawler = new Crawler();
$crawler->addContent('<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><span>中文</span></html>');
$this->assertEquals('中文', $crawler->filterXPath('//span')->text(), '->addContent() guess wrong charset');
}

/**
Expand Down
43 changes: 21 additions & 22 deletions src/Symfony/Component/Intl/NumberFormatter/NumberFormatter.php
Expand Up @@ -232,6 +232,16 @@ class NumberFormatter
'negative' => -9223372036854775808
);

private static $enSymbols = array(
self::DECIMAL => array('.', ',', ';', '%', '0', '#', '-', '+', '¤', '¤¤', '.', 'E', '‰', '*', '∞', 'NaN', '@', ','),
self::CURRENCY => array('.', ',', ';', '%', '0', '#', '-', '+', '¤', '¤¤', '.', 'E', '‰', '*', '∞', 'NaN', '@', ','),
);

private static $enTextAttributes = array(
self::DECIMAL => array('', '', '-', '', '*', '', ''),
self::CURRENCY => array('¤', '', '(¤', ')', '*', ''),
);

/**
* Constructor.
*
Expand Down Expand Up @@ -453,12 +463,10 @@ public function getPattern()
* @return Boolean|string The symbol value or false on error
*
* @see http://www.php.net/manual/en/numberformatter.getsymbol.php
*
* @throws MethodNotImplementedException
*/
public function getSymbol($attr)
{
throw new MethodNotImplementedException(__METHOD__);
return array_key_exists($this->style, self::$enSymbols) && array_key_exists($attr, self::$enSymbols[$this->style]) ? self::$enSymbols[$this->style][$attr] : false;
}

/**
Expand All @@ -469,12 +477,10 @@ public function getSymbol($attr)
* @return Boolean|string The attribute value or false on error
*
* @see http://www.php.net/manual/en/numberformatter.gettextattribute.php
*
* @throws MethodNotImplementedException
*/
public function getTextAttribute($attr)
{
throw new MethodNotImplementedException(__METHOD__);
return array_key_exists($this->style, self::$enTextAttributes) && array_key_exists($attr, self::$enTextAttributes[$this->style]) ? self::$enTextAttributes[$this->style][$attr] : false;
}

/**
Expand All @@ -499,44 +505,37 @@ public function parseCurrency($value, &$currency, &$position = null)
* Parse a number
*
* @param string $value The value to parse
* @param int $type Type of the formatting, one of the format type constants.
* The only currently supported types are NumberFormatter::TYPE_DOUBLE,
* NumberFormatter::TYPE_INT32 and NumberFormatter::TYPE_INT64.
* @param int $position Not supported. Offset to begin the parsing on return this value will hold the offset at which the parsing ended
* @param string $type Type of the formatting, one of the format type constants. NumberFormatter::TYPE_DOUBLE by default
* @param int $position Offset to begin the parsing on return this value will hold the offset at which the parsing ended
*
* @return Boolean|string The parsed value of false on error
*
* @see http://www.php.net/manual/en/numberformatter.parse.php
*
* @throws MethodArgumentNotImplementedException When $position different than null, behavior not implemented
* @see http://www.php.net/manual/en/numberformatter.parse.php
*/
public function parse($value, $type = self::TYPE_DOUBLE, &$position = null)
public function parse($value, $type = self::TYPE_DOUBLE, &$position = 0)
{
if ($type == self::TYPE_DEFAULT || $type == self::TYPE_CURRENCY) {
trigger_error(__METHOD__.'(): Unsupported format type '.$type, \E_USER_WARNING);

return false;
}

// We don't calculate the position when parsing the value
if (null !== $position) {
throw new MethodArgumentNotImplementedException(__METHOD__, 'position');
}

preg_match('/^([^0-9\-]{0,})(.*)/', $value, $matches);
preg_match('/^([^0-9\-\.]{0,})(.*)/', $value, $matches);

// Any string before the numeric value causes error in the parsing
if (isset($matches[1]) && !empty($matches[1])) {
IntlGlobals::setError(IntlGlobals::U_PARSE_ERROR, 'Number parsing failed');
$this->errorCode = IntlGlobals::getErrorCode();
$this->errorMessage = IntlGlobals::getErrorMessage();
$position = 0;

return false;
}

// Remove everything that is not number or dot (.)
$value = preg_replace('/[^0-9\.\-]/', '', $value);
preg_match('/^[0-9\-\.\,]*/', $value, $matches);
$value = preg_replace('/[^0-9\.\-]/', '', $matches[0]);
$value = $this->convertValueDataType($value, $type);
$position = strlen($matches[0]);

// behave like the intl extension
$this->resetError();
Expand Down
Expand Up @@ -472,14 +472,46 @@ public function testGetLocale()
$this->assertEquals('en', $formatter->getLocale());
}

public function testGetSymbol()
{
$decimalFormatter = $this->getNumberFormatter('en', NumberFormatter::DECIMAL);
$currencyFormatter = $this->getNumberFormatter('en', NumberFormatter::CURRENCY);

$r = new \ReflectionProperty('Symfony\Component\Intl\NumberFormatter\NumberFormatter', 'enSymbols');
$r->setAccessible(true);
$expected = $r->getValue('Symfony\Component\Intl\NumberFormatter\NumberFormatter');

for ($i = 0; $i <= 17; $i++) {
$this->assertSame($expected[1][$i], $decimalFormatter->getSymbol($i));
$this->assertSame($expected[2][$i], $currencyFormatter->getSymbol($i));
}
}

public function testGetTextAttribute()
{
$decimalFormatter = $this->getNumberFormatter('en', NumberFormatter::DECIMAL);
$currencyFormatter = $this->getNumberFormatter('en', NumberFormatter::CURRENCY);

$r = new \ReflectionProperty('Symfony\Component\Intl\NumberFormatter\NumberFormatter', 'enTextAttributes');
$r->setAccessible(true);
$expected = $r->getValue('Symfony\Component\Intl\NumberFormatter\NumberFormatter');

for ($i = 0; $i <= 5; $i++) {
$this->assertSame($expected[1][$i], $decimalFormatter->getTextAttribute($i));
$this->assertSame($expected[2][$i], $currencyFormatter->getTextAttribute($i));
}
}

/**
* @dataProvider parseProvider
*/
public function testParse($value, $expected, $message = '')
public function testParse($value, $expected, $message, $expectedPosition)
{
$position = 0;
$formatter = $this->getNumberFormatter('en', NumberFormatter::DECIMAL);
$parsedValue = $formatter->parse($value, NumberFormatter::TYPE_DOUBLE);
$parsedValue = $formatter->parse($value, NumberFormatter::TYPE_DOUBLE, $position);
$this->assertSame($expected, $parsedValue, $message);
$this->assertSame($expectedPosition, $position, $message);

if ($expected === false) {
$errorCode = IntlGlobals::U_PARSE_ERROR;
Expand All @@ -500,8 +532,8 @@ public function testParse($value, $expected, $message = '')
public function parseProvider()
{
return array(
array('prefix1', false, '->parse() does not parse a number with a string prefix.'),
array('1suffix', (float) 1, '->parse() parses a number with a string suffix.'),
array('prefix1', false, '->parse() does not parse a number with a string prefix.', 0),
array('1.4suffix', (float) 1.4, '->parse() parses a number with a string suffix.', 3),
);
}

Expand Down Expand Up @@ -529,6 +561,7 @@ public function parseTypeInt32Provider()
return array(
array('1', 1),
array('1.1', 1),
array('.1', 0),
array('2,147,483,647', 2147483647),
array('-2,147,483,648', -2147483647 - 1),
array('2,147,483,648', false, '->parse() TYPE_INT32 returns false when the number is greater than the integer positive range.'),
Expand Down Expand Up @@ -663,14 +696,6 @@ public function testParseTypeCurrency()
$formatter->parse('1', NumberFormatter::TYPE_CURRENCY);
}

public function testParseWithNullPositionValue()
{
$position = null;
$formatter = $this->getNumberFormatter('en', NumberFormatter::DECIMAL);
$formatter->parse('123', NumberFormatter::TYPE_INT32, $position);
$this->assertNull($position);
}

public function testParseWithNotNullPositionValue()
{
$position = 1;
Expand Down
Expand Up @@ -149,24 +149,6 @@ public function testGetPattern()
$formatter->getPattern();
}

/**
* @expectedException \Symfony\Component\Intl\Exception\MethodNotImplementedException
*/
public function testGetSymbol()
{
$formatter = $this->getNumberFormatter('en', NumberFormatter::DECIMAL);
$formatter->getSymbol(null);
}

/**
* @expectedException \Symfony\Component\Intl\Exception\MethodNotImplementedException
*/
public function testGetTextAttribute()
{
$formatter = $this->getNumberFormatter('en', NumberFormatter::DECIMAL);
$formatter->getTextAttribute(null);
}

public function testGetErrorCode()
{
$formatter = $this->getNumberFormatter('en', NumberFormatter::DECIMAL);
Expand All @@ -182,14 +164,6 @@ public function testParseCurrency()
$formatter->parseCurrency(null, $currency);
}

/**
* @expectedException \Symfony\Component\Intl\Exception\MethodArgumentNotImplementedException
*/
public function testParseWithNotNullPositionValue()
{
parent::testParseWithNotNullPositionValue();
}

/**
* @expectedException \Symfony\Component\Intl\Exception\MethodNotImplementedException
*/
Expand Down
Expand Up @@ -15,7 +15,6 @@
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
use Symfony\Component\Security\Http\AccessMapInterface;
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
Expand All @@ -31,15 +30,13 @@ class AccessListener implements ListenerInterface
private $accessDecisionManager;
private $map;
private $authManager;
private $logger;

public function __construct(SecurityContextInterface $context, AccessDecisionManagerInterface $accessDecisionManager, AccessMapInterface $map, AuthenticationManagerInterface $authManager, LoggerInterface $logger = null)
public function __construct(SecurityContextInterface $context, AccessDecisionManagerInterface $accessDecisionManager, AccessMapInterface $map, AuthenticationManagerInterface $authManager)
{
$this->context = $context;
$this->accessDecisionManager = $accessDecisionManager;
$this->map = $map;
$this->authManager = $authManager;
$this->logger = $logger;
}

/**
Expand Down
Expand Up @@ -24,7 +24,7 @@ interface FirewallMapInterface
* Returns the authentication listeners, and the exception listener to use
* for the given request.
*
* If there are no authentication listeners, the first inner are must be
* If there are no authentication listeners, the first inner array must be
* empty.
*
* If there is no exception listener, the second element of the outer array
Expand Down
7 changes: 6 additions & 1 deletion src/Symfony/Component/Yaml/Exception/ParseException.php
Expand Up @@ -11,6 +11,11 @@

namespace Symfony\Component\Yaml\Exception;

if (!defined('JSON_UNESCAPED_UNICODE')) {
define('JSON_UNESCAPED_SLASHES', 64);
define('JSON_UNESCAPED_UNICODE', 256);
}

/**
* Exception class thrown when an error occurs during parsing.
*
Expand Down Expand Up @@ -125,7 +130,7 @@ private function updateRepr()
}

if (null !== $this->parsedFile) {
$this->message .= sprintf(' in %s', json_encode($this->parsedFile));
$this->message .= sprintf(' in %s', json_encode($this->parsedFile, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
}

if ($this->parsedLine >= 0) {
Expand Down
30 changes: 30 additions & 0 deletions src/Symfony/Component/Yaml/Tests/ParseExceptionTest.php
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Yaml\Tests;

use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml;

class ParseExceptionTest extends \PHPUnit_Framework_TestCase
{
public function testGetMessage()
{
$exception = new ParseException('Error message', 42, 'foo: bar', '/var/www/app/config.yml');
if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
$message = 'Error message in "/var/www/app/config.yml" at line 42 (near "foo: bar")';
} else {
$message = 'Error message in "\\/var\\/www\\/app\\/config.yml" at line 42 (near "foo: bar")';
}

$this->assertEquals($message, $exception->getMessage());
}
}

0 comments on commit b1542f0

Please sign in to comment.