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

Commit

Permalink
Merge branch 'master' of git://github.com/zendframework/zf2
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 167 changed files with 2,896 additions and 4,762 deletions.
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zendframework/zend-validator",
"description": "Zend\\Validator component",
"description": "provides a set of commonly needed validators",
"license": "BSD-3-Clause",
"keywords": [
"zf2",
Expand All @@ -9,11 +9,11 @@
"homepage": "https://github.com/zendframework/zend-validator",
"autoload": {
"psr-4": {
"Zend\\Validator\\": "src/"
"Zend\\Validator": "src/"
}
},
"require": {
"php": ">=5.3.23",
"php": ">=5.3.3",
"zendframework/zend-stdlib": "self.version"
},
"require-dev": {
Expand All @@ -31,7 +31,7 @@
"suggest": {
"zendframework/zend-db": "Zend\\Db component",
"zendframework/zend-filter": "Zend\\Filter component, required by the Digits validator",
"zendframework/zend-i18n": "Zend\\I18n component to allow translation of validation error messages as well as to use the various Date validators",
"zendframework/zend-i18n": "Zend\\I18n component",
"zendframework/zend-math": "Zend\\Math component",
"zendframework/zend-resources": "Translations of validator messages",
"zendframework/zend-servicemanager": "Zend\\ServiceManager component to allow using the ValidatorPluginManager and validator chains",
Expand Down
207 changes: 129 additions & 78 deletions src/AbstractValidator.php
Original file line number Diff line number Diff line change
@@ -1,38 +1,28 @@
<?php
/**
* Zend Framework
* Zend Framework (http://framework.zend.com/)
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @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_Validator
*/

namespace Zend\Validator;

use Traversable;
use Zend\Registry;
use Zend\I18n\Translator\Translator;
use Zend\I18n\Translator\TranslatorAwareInterface;
use Zend\Stdlib\ArrayUtils;
use Zend\Translator;
use Zend\Validator\Exception\InvalidArgumentException;

/**
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
abstract class AbstractValidator implements ValidatorInterface
abstract class AbstractValidator implements
TranslatorAwareInterface,
ValidatorInterface
{
/**
* The value to be validated
Expand All @@ -43,10 +33,16 @@ abstract class AbstractValidator implements ValidatorInterface

/**
* Default translation object for all validate objects
* @var \Zend\Translator\Adapter\AbstractAdapter
* @var Translator
*/
protected static $defaultTranslator;

/**
* Default text domain to be used with translator
* @var string
*/
protected static $defaultTranslatorTextDomain = 'default';

/**
* Limits the maximum returned length of a error message
*
Expand All @@ -55,12 +51,14 @@ abstract class AbstractValidator implements ValidatorInterface
protected static $messageLength = -1;

protected $abstractOptions = array(
'messages' => array(), // Array of validation failure messages
'messageTemplates' => array(), // Array of validation failure message templates
'messageVariables' => array(), // Array of additional variables available for validation failure messages
'translator' => null, // Translation object to used -> \Zend\Translator\Translator
'translatorDisabled' => false, // Is translation disabled?
'valueObscured' => false, // Flag indicating whether or not value should be obfuscated in error messages
'messages' => array(), // Array of validation failure messages
'messageTemplates' => array(), // Array of validation failure message templates
'messageVariables' => array(), // Array of additional variables available for validation failure messages
'translator' => null, // Translation object to used -> Zend\I18n\Translator\Translator
'translatorTextDomain' => null, // Translation text domain
'translatorEnabled' => true, // Is translation enabled?
'valueObscured' => false, // Flag indicating whether or not value should be obfuscated
// in error messages
);

/**
Expand Down Expand Up @@ -212,7 +210,7 @@ public function setMessage($messageString, $messageKey = null)
{
if ($messageKey === null) {
$keys = array_keys($this->abstractOptions['messageTemplates']);
foreach($keys as $key) {
foreach ($keys as $key) {
$this->setMessage($messageString, $key);
}
return $this;
Expand Down Expand Up @@ -298,13 +296,7 @@ protected function createMessage($messageKey, $value)

$message = $this->abstractOptions['messageTemplates'][$messageKey];

if (null !== ($translator = $this->getTranslator())) {
if ($translator->isTranslated($messageKey)) {
$message = $translator->translate($messageKey);
} else {
$message = $translator->translate($message);
}
}
$message = $this->translateMessage($messageKey, $message);

if (is_object($value) &&
!in_array('__toString', get_class_methods($value))
Expand All @@ -313,7 +305,7 @@ protected function createMessage($messageKey, $value)
} elseif (is_array($value)) {
$value = '[' . implode(', ', $value) . ']';
} else {
$value = (string)$value;
$value = (string) $value;
}

if ($this->isValueObscured()) {
Expand All @@ -330,7 +322,7 @@ protected function createMessage($messageKey, $value)
} else {
$value = $this->$property;
}
$message = str_replace("%$ident%", (string)$value, $message);
$message = str_replace("%$ident%", (string) $value, $message);
}

$length = self::getMessageLength();
Expand Down Expand Up @@ -408,36 +400,33 @@ public function isValueObscured()
/**
* Set translation object
*
* @param \Zend\Translator\Translator|\Zend\Translator\Adapter\AbstractAdapter|null $translator
* @param Translator|null $translator
* @param string $textDomain (optional)
* @return AbstractValidator
* @throws Exception\InvalidArgumentException
*/
public function setTranslator($translator = null)
public function setTranslator(Translator $translator = null, $textDomain = null)
{
if ((null === $translator) || ($translator instanceof Translator\Adapter\AbstractAdapter)) {
$this->abstractOptions['translator'] = $translator;
} elseif ($translator instanceof Translator\Translator) {
$this->abstractOptions['translator'] = $translator->getAdapter();
} else {
throw new InvalidArgumentException('Invalid translator specified');
$this->abstractOptions['translator'] = $translator;
if (null !== $textDomain) {
$this->setTranslatorTextDomain($textDomain);
}

return $this;
}

/**
* Return translation object
*
* @return \Zend\Translator\Adapter\AbstractAdapter|null
* @return Translator|null
*/
public function getTranslator()
{
if ($this->isTranslatorDisabled()) {
if (! $this->isTranslatorEnabled()) {
return null;
}

if (null === $this->abstractOptions['translator']) {
return self::getDefaultTranslator();
$this->abstractOptions['translator'] = self::getDefaultTranslator();
}

return $this->abstractOptions['translator'];
Expand All @@ -450,45 +439,60 @@ public function getTranslator()
*/
public function hasTranslator()
{
return (bool)$this->abstractOptions['translator'];
return (bool) $this->abstractOptions['translator'];
}

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

/**
* Return the translation text domain
*
* @return string
*/
public function getTranslatorTextDomain()
{
if (null === $this->abstractOptions['translatorTextDomain']) {
$this->abstractOptions['translatorTextDomain'] =
self::getDefaultTranslatorTextDomain();
}
return $this->abstractOptions['translatorTextDomain'];
}

/**
* Set default translation object for all validate objects
*
* @param \Zend\Translator\Translator|\Zend\Translator\Adapter\AbstractAdapter|null $translator
* @param Translator|null $translator
* @param string $textDomain (optional)
* @return void
* @throws Exception\InvalidArgumentException
*/
public static function setDefaultTranslator($translator = null)
public static function setDefaultTranslator(
Translator $translator = null, $textDomain = null
)
{
if ((null === $translator) || ($translator instanceof Translator\Adapter\AbstractAdapter)) {
self::$defaultTranslator = $translator;
} elseif ($translator instanceof Translator\Translator) {
self::$defaultTranslator = $translator->getAdapter();
} else {
throw new InvalidArgumentException('Invalid translator specified');
self::$defaultTranslator = $translator;
if (null !== $textDomain) {
self::setDefaultTranslatorTextDomain($textDomain);
}
}

/**
* Get default translation object for all validate objects
*
* @return \Zend\Translator\Adapter\AbstractAdapter|null
* @return Translator|null
*/
public static function getDefaultTranslator()
{
if (null === self::$defaultTranslator) {
if (Registry::isRegistered('Zend_Translator')) {
$translator = Registry::get('Zend_Translator');
if ($translator instanceof Translator\Adapter\AbstractAdapter) {
return $translator;
} elseif ($translator instanceof Translator\Translator) {
return $translator->getAdapter();
}
}
}

return self::$defaultTranslator;
}

Expand All @@ -499,29 +503,50 @@ public static function getDefaultTranslator()
*/
public static function hasDefaultTranslator()
{
return (bool)self::$defaultTranslator;
return (bool) self::$defaultTranslator;
}

/**
* Indicate whether or not translation should be disabled
* Set default translation text domain for all validate objects
*
* @param string $textDomain
* @return void
*/
public static function setDefaultTranslatorTextDomain($textDomain = 'default')
{
self::$defaultTranslatorTextDomain = $textDomain;
}

/**
* Get default translation text domain for all validate objects
*
* @return string
*/
public static function getDefaultTranslatorTextDomain()
{
return self::$defaultTranslatorTextDomain;
}

/**
* Indicate whether or not translation should be enabled
*
* @param bool $flag
* @return AbstractValidator
*/
public function setTranslatorDisabled($flag)
public function setTranslatorEnabled($flag = true)
{
$this->abstractOptions['translatorDisabled'] = (bool) $flag;
$this->abstractOptions['translatorEnabled'] = (bool) $flag;
return $this;
}

/**
* Is translation disabled?
* Is translation enabled?
*
* @return bool
*/
public function isTranslatorDisabled()
public function isTranslatorEnabled()
{
return $this->abstractOptions['translatorDisabled'];
return $this->abstractOptions['translatorEnabled'];
}

/**
Expand All @@ -543,4 +568,30 @@ public static function setMessageLength($length = -1)
{
self::$messageLength = $length;
}
}

/**
* Translate a validation message
*
* @param string $messageKey
* @param string $message
* @return string
*/
protected function translateMessage($messageKey, $message)
{
$translator = $this->getTranslator();
if (!$translator) {
return $message;
}

$translated = $translator->translate(
$messageKey, $this->getTranslatorTextDomain()
);
if ($translated !== $messageKey) {
return $translated;
}

return $translator->translate(
$message, $this->getTranslatorTextDomain()
);
}
}
Loading

0 comments on commit 5803840

Please sign in to comment.