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

Commit

Permalink
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 43 deletions.
4 changes: 1 addition & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
},
"require": {
"php": ">=5.3.3",
"zendframework/zend-stdlib": "self.version",
"zendframework/zend-i18n": "self.version",
"zendframework/zend-servicemanager": "self.version"
"zendframework/zend-stdlib": "self.version"
},
"require-dev": {
"zendframework/zend-db": "self.version",
Expand Down
27 changes: 12 additions & 15 deletions src/AbstractValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@
namespace Zend\Validator;

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

abstract class AbstractValidator implements
TranslatorAwareInterface,
Translator\TranslatorAwareInterface,
ValidatorInterface
{
/**
Expand All @@ -28,7 +25,7 @@ abstract class AbstractValidator implements

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

Expand All @@ -49,7 +46,7 @@ abstract class AbstractValidator implements
'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
'translator' => null, // Translation object to used -> Translator\TranslatorInterface
'translatorTextDomain' => null, // Translation text domain
'translatorEnabled' => true, // Is translation enabled?
'valueObscured' => false, // Flag indicating whether or not value should be obfuscated
Expand Down Expand Up @@ -103,7 +100,7 @@ public function getOption($option)
return $this->options[$option];
}

throw new InvalidArgumentException("Invalid option '$option'");
throw new Exception\InvalidArgumentException("Invalid option '$option'");
}

/**
Expand Down Expand Up @@ -212,7 +209,7 @@ public function setMessage($messageString, $messageKey = null)
}

if (!isset($this->abstractOptions['messageTemplates'][$messageKey])) {
throw new InvalidArgumentException("No message template exists for key '$messageKey'");
throw new Exception\InvalidArgumentException("No message template exists for key '$messageKey'");
}

$this->abstractOptions['messageTemplates'][$messageKey] = $messageString;
Expand Down Expand Up @@ -268,7 +265,7 @@ public function __get($property)
return $result;
}

throw new InvalidArgumentException("No property exists by the name '$property'");
throw new Exception\InvalidArgumentException("No property exists by the name '$property'");
}

/**
Expand Down Expand Up @@ -395,12 +392,12 @@ public function isValueObscured()
/**
* Set translation object
*
* @param Translator|null $translator
* @param Translator\TranslatorInterface|null $translator
* @param string $textDomain (optional)
* @return AbstractValidator
* @throws Exception\InvalidArgumentException
*/
public function setTranslator(Translator $translator = null, $textDomain = null)
public function setTranslator(Translator\TranslatorInterface $translator = null, $textDomain = null)
{
$this->abstractOptions['translator'] = $translator;
if (null !== $textDomain) {
Expand All @@ -412,7 +409,7 @@ public function setTranslator(Translator $translator = null, $textDomain = null)
/**
* Return translation object
*
* @return Translator|null
* @return Translator\TranslatorInterface|null
*/
public function getTranslator()
{
Expand Down Expand Up @@ -466,13 +463,13 @@ public function getTranslatorTextDomain()
/**
* Set default translation object for all validate objects
*
* @param Translator|null $translator
* @param Translator\TranslatorInterface|null $translator
* @param string $textDomain (optional)
* @return void
* @throws Exception\InvalidArgumentException
*/
public static function setDefaultTranslator(
Translator $translator = null, $textDomain = null
Translator\TranslatorInterface $translator = null, $textDomain = null
)
{
static::$defaultTranslator = $translator;
Expand All @@ -484,7 +481,7 @@ public static function setDefaultTranslator(
/**
* Get default translation object for all validate objects
*
* @return Translator|null
* @return Translator\TranslatorInterface|null
*/
public static function getDefaultTranslator()
{
Expand Down
69 changes: 69 additions & 0 deletions src/Translator/TranslatorAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*;
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Validator\Translator;

interface TranslatorAwareInterface
{
/**
* Sets translator to use in helper
*
* @param TranslatorInterface $translator [optional] translator.
* Default is null, which sets no translator.
* @param string $textDomain [optional] text domain
* Default is null, which skips setTranslatorTextDomain
* @return self
*/
public function setTranslator(TranslatorInterface $translator = null, $textDomain = null);

/**
* Returns translator used in object
*
* @return TranslatorInterface|null
*/
public function getTranslator();

/**
* Checks if the object has a translator
*
* @return bool
*/
public function hasTranslator();

/**
* Sets whether translator is enabled and should be used
*
* @param bool $enabled [optional] whether translator should be used.
* Default is true.
* @return self
*/
public function setTranslatorEnabled($enabled = true);

/**
* Returns whether translator is enabled and should be used
*
* @return bool
*/
public function isTranslatorEnabled();

/**
* Set translation text domain
*
* @param string $textDomain
* @return TranslatorAwareInterface
*/
public function setTranslatorTextDomain($textDomain = 'default');

/**
* Return the translation text domain
*
* @return string
*/
public function getTranslatorTextDomain();
}
21 changes: 21 additions & 0 deletions src/Translator/TranslatorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Validator\Translator;

interface TranslatorInterface
{
/**
* @param string $message
* @param string $textDomain
* @param string $locale
* @return string
*/
public function translate($message, $textDomain = 'default', $locale = null);
}
3 changes: 1 addition & 2 deletions src/ValidatorPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace Zend\Validator;

use Zend\I18n\Translator\TranslatorAwareInterface;
use Zend\ServiceManager\AbstractPluginManager;
use Zend\ServiceManager\ConfigInterface;

Expand Down Expand Up @@ -135,7 +134,7 @@ public function __construct(ConfigInterface $configuration = null)
*/
public function injectTranslator($validator)
{
if ($validator instanceof TranslatorAwareInterface) {
if ($validator instanceof Translator\TranslatorAwareInterface) {
$locator = $this->getServiceLocator();
if ($locator && $locator->has('translator')) {
$validator->setTranslator($locator->get('translator'));
Expand Down
15 changes: 7 additions & 8 deletions test/AbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
namespace ZendTest\Validator;

use ReflectionMethod;
use Zend\I18n\Translator\Translator;
use Zend\Validator\AbstractValidator;
use Zend\Validator\EmailAddress;
use Zend\Validator\Hostname;
Expand Down Expand Up @@ -53,7 +52,7 @@ public function testCanSetTranslator()
{
$this->testTranslatorNullByDefault();
set_error_handler(array($this, 'errorHandlerIgnore'));
$translator = new Translator();
$translator = new TestAsset\Translator();
restore_error_handler();
$this->validator->setTranslator($translator);
$this->assertSame($translator, $this->validator->getTranslator());
Expand All @@ -79,7 +78,7 @@ public function testErrorMessagesAreTranslatedWhenTranslatorPresent()
$loader->translations = array(
'fooMessage' => 'This is the translated message for %value%',
);
$translator = new Translator();
$translator = new TestAsset\Translator();
$translator->getPluginManager()->setService('default', $loader);
$translator->addTranslationFile('default', null);

Expand All @@ -97,7 +96,7 @@ public function testCanTranslateMessagesInsteadOfKeys()
$loader->translations = array(
'%value% was passed' => 'This is the translated message for %value%',
);
$translator = new Translator();
$translator = new TestAsset\Translator();
$translator->getPluginManager()->setService('default', $loader);
$translator->addTranslationFile('default', null);

Expand Down Expand Up @@ -147,7 +146,7 @@ public function testDoesNotFailOnObjectInput()
public function testTranslatorEnabledPerDefault()
{
set_error_handler(array($this, 'errorHandlerIgnore'));
$translator = new Translator();
$translator = new TestAsset\Translator();
$this->validator->setTranslator($translator);
$this->assertTrue($this->validator->isTranslatorEnabled());
}
Expand All @@ -158,7 +157,7 @@ public function testCanDisableTranslator()
$loader->translations = array(
'%value% was passed' => 'This is the translated message for %value%',
);
$translator = new Translator();
$translator = new TestAsset\Translator();
$translator->getPluginManager()->setService('default', $loader);
$translator->addTranslationFile('default', null);
$this->validator->setTranslator($translator);
Expand Down Expand Up @@ -202,7 +201,7 @@ public function testInvokeProxiesToIsValid()

public function testTranslatorMethods()
{
$translatorMock = $this->getMock('Zend\I18n\Translator\Translator');
$translatorMock = $this->getMock('ZendTest\Validator\TestAsset\Translator');
$this->validator->setTranslator($translatorMock, 'foo');

$this->assertEquals($translatorMock, $this->validator->getTranslator());
Expand All @@ -222,7 +221,7 @@ public function testDefaultTranslatorMethods()

$this->assertFalse($this->validator->hasTranslator());

$translatorMock = $this->getMock('Zend\I18n\Translator\Translator');
$translatorMock = $this->getMock('ZendTest\Validator\TestAsset\Translator');
AbstractValidator::setDefaultTranslator($translatorMock, 'foo');

$this->assertEquals($translatorMock, AbstractValidator::getDefaultTranslator());
Expand Down
3 changes: 1 addition & 2 deletions test/EmailAddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

namespace ZendTest\Validator;

use Zend\I18n\Translator\Translator;
use Zend\Validator\EmailAddress;
use Zend\Validator\Hostname;

Expand Down Expand Up @@ -396,7 +395,7 @@ public function testHostnameValidatorMessagesShouldBeTranslated()
);
$loader = new TestAsset\ArrayTranslator();
$loader->translations = $translations;
$translator = new Translator();
$translator = new TestAsset\Translator();
$translator->getPluginManager()->setService('test', $loader);
$translator->addTranslationFile('test', null);

Expand Down
3 changes: 1 addition & 2 deletions test/HostnameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

namespace ZendTest\Validator;

use Zend\I18n\Translator\Translator;
use Zend\Validator\Hostname;

/**
Expand Down Expand Up @@ -266,7 +265,7 @@ public function testValidatorMessagesShouldBeTranslated()
);
$loader = new TestAsset\ArrayTranslator();
$loader->translations = $translations;
$translator = new Translator();
$translator = new TestAsset\Translator();
$translator->getPluginManager()->setService('default', $loader);
$translator->addTranslationFile('default', null);
$this->validator->setTranslator($translator);
Expand Down
9 changes: 4 additions & 5 deletions test/StaticValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

namespace ZendTest\Validator;

use Zend\I18n\Translator;
use Zend\Validator\AbstractValidator;
use Zend\I18n\Validator\Alpha;
use Zend\Validator\Between;
Expand Down Expand Up @@ -70,7 +69,7 @@ public function errorHandlerIgnore($errno, $errstr, $errfile, $errline, array $e

public function testCanSetGlobalDefaultTranslator()
{
$translator = new Translator\Translator();
$translator = new TestAsset\Translator();
AbstractValidator::setDefaultTranslator($translator);
$this->assertSame($translator, AbstractValidator::getDefaultTranslator());
}
Expand All @@ -84,7 +83,7 @@ public function testGlobalDefaultTranslatorUsedWhenNoLocalTranslatorSet()
public function testLocalTranslatorPreferredOverGlobalTranslator()
{
$this->testCanSetGlobalDefaultTranslator();
$translator = new Translator\Translator();
$translator = new TestAsset\Translator();
$this->validator->setTranslator($translator);
$this->assertNotSame(AbstractValidator::getDefaultTranslator(), $this->validator->getTranslator());
}
Expand All @@ -99,7 +98,7 @@ public function testMaximumErrorMessageLength()
$loader->translations = array(
Alpha::INVALID => 'This is the translated message for %value%',
);
$translator = new Translator\Translator();
$translator = new TestAsset\Translator();
$translator->getPluginManager()->setService('default', $loader);
$translator->addTranslationFile('default', null);

Expand All @@ -123,7 +122,7 @@ public function testSetGetMessageLengthLimitation()

public function testSetGetDefaultTranslator()
{
$translator = new Translator\Translator();
$translator = new TestAsset\Translator();
AbstractValidator::setDefaultTranslator($translator);
$this->assertSame($translator, AbstractValidator::getDefaultTranslator());
}
Expand Down
6 changes: 3 additions & 3 deletions test/TestAsset/ArrayTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@

namespace ZendTest\Validator\TestAsset;

use Zend\I18n\Translator;
use Zend\I18n\Translator as I18nTranslator;

class ArrayTranslator implements Translator\Loader\FileLoaderInterface
class ArrayTranslator implements I18nTranslator\Loader\FileLoaderInterface
{
public $translations;

public function load($filename, $locale)
{
$textDomain = new Translator\TextDomain($this->translations);
$textDomain = new I18nTranslator\TextDomain($this->translations);
return $textDomain;
}
}
Loading

0 comments on commit 2fbc2a0

Please sign in to comment.