Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'develop' of git://github.com/zendframework/zf2 into hot…
Browse files Browse the repository at this point in the history
…fix/cache-empty-namespace
  • Loading branch information
Show file tree
Hide file tree
Showing 22 changed files with 763 additions and 36 deletions.
86 changes: 86 additions & 0 deletions src/Translator/Loader/Ini.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_I18n
*/

namespace Zend\I18n\Translator\Loader;

use Zend\Config\Reader\Ini as IniReader;
use Zend\I18n\Exception;
use Zend\I18n\Translator\Plural\Rule as PluralRule;
use Zend\I18n\Translator\TextDomain;

/**
* PHP INI format loader.
*
* @category Zend
* @package Zend_I18n
* @subpackage Translator
*/
class Ini implements FileLoaderInterface
{
/**
* load(): defined by FileLoaderInterface.
*
* @see FileLoaderInterface::load()
* @param string $locale
* @param string $filename
* @return TextDomain|null
* @throws Exception\InvalidArgumentException
*/
public function load($locale, $filename)
{
if (!is_file($filename) || !is_readable($filename)) {
throw new Exception\InvalidArgumentException(sprintf(
'Could not open file %s for reading',
$filename
));
}

$messages = array();
$iniReader = new IniReader();
$messagesNamespaced = $iniReader->fromFile($filename);

$list = $messagesNamespaced;
if (isset($messagesNamespaced['translation'])) {
$list = $messagesNamespaced['translation'];
}

foreach ($list as $message) {
if (!is_array($message) || count($message) < 2) {
throw new Exception\InvalidArgumentException(
'Each INI row must be an array with message and translation'
);
}
if (isset($message['message']) && isset($message['translation'])) {
$messages[$message['message']] = $message['translation'];
continue;
}
$messages[array_shift($message)] = array_shift($message);
}

if (!is_array($messages)) {
throw new Exception\InvalidArgumentException(sprintf(
'Expected an array, but received %s',
gettype($messages)
));
}

$textDomain = new TextDomain($messages);

if (array_key_exists('plural', $messagesNamespaced)
&& isset($messagesNamespaced['plural']['plural_forms'])
) {
$textDomain->setPluralRule(
PluralRule::fromString($messagesNamespaced['plural']['plural_forms'])
);
}

return $textDomain;
}
}
3 changes: 2 additions & 1 deletion src/Translator/LoaderPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ class LoaderPluginManager extends AbstractPluginManager
* @var array
*/
protected $invokableClasses = array(
'phparray' => 'Zend\I18n\Translator\Loader\PhpArray',
'gettext' => 'Zend\I18n\Translator\Loader\Gettext',
'ini' => 'Zend\I18n\Translator\Loader\Ini',
'phparray' => 'Zend\I18n\Translator\Loader\PhpArray',
);

/**
Expand Down
20 changes: 10 additions & 10 deletions src/Translator/Plural/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ protected function evaluateAstPart(array $ast, $number)
*/
public static function fromString($string)
{
if (self::$parser === null) {
self::$parser = new Parser();
if (static::$parser === null) {
static::$parser = new Parser();
}

if (!preg_match('(nplurals=(?P<nplurals>\d+))', $string, $match)) {
Expand All @@ -202,8 +202,8 @@ public static function fromString($string)
));
}

$tree = self::$parser->parse($match['plural']);
$ast = self::createAst($tree);
$tree = static::$parser->parse($match['plural']);
$ast = static::createAst($tree);

return new self($numPlurals, $ast);
}
Expand All @@ -230,18 +230,18 @@ protected static function createAst(Symbol $symbol)
break;

case '!':
$ast['arguments'][] = self::createAst($symbol->first);
$ast['arguments'][] = static::createAst($symbol->first);
break;

case '?':
$ast['arguments'][] = self::createAst($symbol->first);
$ast['arguments'][] = self::createAst($symbol->second);
$ast['arguments'][] = self::createAst($symbol->third);
$ast['arguments'][] = static::createAst($symbol->first);
$ast['arguments'][] = static::createAst($symbol->second);
$ast['arguments'][] = static::createAst($symbol->third);
break;

default:
$ast['arguments'][] = self::createAst($symbol->first);
$ast['arguments'][] = self::createAst($symbol->second);
$ast['arguments'][] = static::createAst($symbol->first);
$ast['arguments'][] = static::createAst($symbol->second);
break;
}

Expand Down
120 changes: 120 additions & 0 deletions src/Translator/TranslatorAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_I18n
*/

namespace Zend\I18n\Translator;

use Zend\I18n\Translator\Translator;

/**
* @category Zend
* @package Zend_I18n
* @subpackage Translator
*/
trait TranslatorAwareTrait
{
/**
* @var Translator
*/
protected $translator = null;

/**
* @var bool
*/
protected $translatorEnabled = true;

/**
* @var string
*/
protected $translatorTextDomain = 'default';

/**
* Sets translator to use in helper
*
* @param Translator $translator
* @param string $textDomain
* @return mixed
*/
public function setTranslator(Translator $translator = null, $textDomain = null)
{
$this->translator = $translator;

if (!is_null($textDomain)) {
$this->setTranslatorTextDomain($textDomain);
}

return $this;
}

/**
* Returns translator used in object
*
* @return Translator
*/
public function getTranslator()
{
return $this->translator;
}

/**
* Checks if the object has a translator
*
* @return bool
*/
public function hasTranslator()
{
return !is_null($this->translator);
}

/**
* Sets whether translator is enabled and should be used
*
* @param bool $enabled
* @return mixed
*/
public function setTranslatorEnabled($enabled = true)
{
$this->translatorEnabled = $enabled;

return $this;
}

/**
* Returns whether translator is enabled and should be used
*
* @return bool
*/
public function isTranslatorEnabled()
{
return $this->translatorEnabled;
}

/**
* Set translation text domain
*
* @param string $textDomain
* @return mixed
*/
public function setTranslatorTextDomain($textDomain = 'default')
{
$this->translatorTextDomain = $textDomain;

return $this;
}

/**
* Return the translation text domain
*
* @return string
*/
public function getTranslatorTextDomain()
{
return $this->translatorTextDomain;
}
}
8 changes: 4 additions & 4 deletions src/Validator/Alnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ public function isValid($value)
return false;
}

if (null === self::$filter) {
self::$filter = new AlnumFilter();
if (null === static::$filter) {
static::$filter = new AlnumFilter();
}

self::$filter->setAllowWhiteSpace($this->options['allowWhiteSpace']);
static::$filter->setAllowWhiteSpace($this->options['allowWhiteSpace']);

if ($value != self::$filter->filter($value)) {
if ($value != static::$filter->filter($value)) {
$this->error(self::NOT_ALNUM);
return false;
}
Expand Down
10 changes: 5 additions & 5 deletions src/Validator/Alpha.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ public function isValid($value)
return false;
}

if (null === self::$filter) {
self::$filter = new AlphaFilter();
if (null === static::$filter) {
static::$filter = new AlphaFilter();
}

//self::$filter->setAllowWhiteSpace($this->allowWhiteSpace);
self::$filter->setAllowWhiteSpace($this->options['allowWhiteSpace']);
//static::$filter->setAllowWhiteSpace($this->allowWhiteSpace);
static::$filter->setAllowWhiteSpace($this->options['allowWhiteSpace']);

if ($value !== self::$filter->filter($value)) {
if ($value !== static::$filter->filter($value)) {
$this->error(self::NOT_ALPHA);
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Validator/PostCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,8 @@ public function isValid($value)
if ('' === $region) {
throw new Exception\InvalidArgumentException("Locale must contain a region");
}
if (isset(self::$postCodeRegex[$region])) {
$format = self::$postCodeRegex[$region];
if (isset(static::$postCodeRegex[$region])) {
$format = static::$postCodeRegex[$region];
}
}
if (null === $format || '' === $format) {
Expand Down
19 changes: 11 additions & 8 deletions src/View/Helper/DateFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,32 +105,35 @@ public function getlocale()
/**
* Format a date.
*
* @param DateTime|integer|array $date
* @param integer $dateType
* @param integer $timeType
* @param string $locale
* @param DateTime|integer|array $date
* @param int $dateType
* @param int $timeType
* @param string $locale
* @param string|null $pattern
* @return string
* @throws Exception\RuntimeException
*/
public function __invoke(
$date,
$dateType = IntlDateFormatter::NONE,
$timeType = IntlDateFormatter::NONE,
$locale = null
$locale = null,
$pattern = null
) {
if ($locale === null) {
$locale = $this->getlocale();
}

$timezone = $this->getTimezone();
$formatterId = md5($dateType . "\0" . $timeType . "\0" . $locale);
$formatterId = md5($dateType . "\0" . $timeType . "\0" . $locale ."\0" . $pattern);

if (!isset($this->formatters[$formatterId])) {
$this->formatters[$formatterId] = new IntlDateFormatter(
$locale,
$dateType,
$timeType,
$timezone
$timezone,
IntlDateFormatter::GREGORIAN,
$pattern
);
}

Expand Down
Loading

0 comments on commit 28bc01e

Please sign in to comment.