diff --git a/composer.json b/composer.json index a3d44833f..066709d8b 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/AbstractValidator.php b/src/AbstractValidator.php index e6bde162c..7aa2dfe55 100644 --- a/src/AbstractValidator.php +++ b/src/AbstractValidator.php @@ -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 { /** @@ -28,7 +25,7 @@ abstract class AbstractValidator implements /** * Default translation object for all validate objects - * @var Translator + * @var Translator\TranslatorInterface */ protected static $defaultTranslator; @@ -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 @@ -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'"); } /** @@ -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; @@ -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'"); } /** @@ -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) { @@ -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() { @@ -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; @@ -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() { diff --git a/src/Translator/TranslatorAwareInterface.php b/src/Translator/TranslatorAwareInterface.php new file mode 100644 index 000000000..7089da2d9 --- /dev/null +++ b/src/Translator/TranslatorAwareInterface.php @@ -0,0 +1,69 @@ +getServiceLocator(); if ($locator && $locator->has('translator')) { $validator->setTranslator($locator->get('translator')); diff --git a/test/AbstractTest.php b/test/AbstractTest.php index e1794bf77..5dd04689d 100644 --- a/test/AbstractTest.php +++ b/test/AbstractTest.php @@ -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; @@ -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()); @@ -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); @@ -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); @@ -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()); } @@ -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); @@ -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()); @@ -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()); diff --git a/test/EmailAddressTest.php b/test/EmailAddressTest.php index 5d9e21942..9f8306a01 100644 --- a/test/EmailAddressTest.php +++ b/test/EmailAddressTest.php @@ -10,7 +10,6 @@ namespace ZendTest\Validator; -use Zend\I18n\Translator\Translator; use Zend\Validator\EmailAddress; use Zend\Validator\Hostname; @@ -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); diff --git a/test/HostnameTest.php b/test/HostnameTest.php index c2cb9cacf..1f22b499e 100644 --- a/test/HostnameTest.php +++ b/test/HostnameTest.php @@ -10,7 +10,6 @@ namespace ZendTest\Validator; -use Zend\I18n\Translator\Translator; use Zend\Validator\Hostname; /** @@ -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); diff --git a/test/StaticValidatorTest.php b/test/StaticValidatorTest.php index 9285e3a0b..b1220de56 100644 --- a/test/StaticValidatorTest.php +++ b/test/StaticValidatorTest.php @@ -10,7 +10,6 @@ namespace ZendTest\Validator; -use Zend\I18n\Translator; use Zend\Validator\AbstractValidator; use Zend\I18n\Validator\Alpha; use Zend\Validator\Between; @@ -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()); } @@ -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()); } @@ -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); @@ -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()); } diff --git a/test/TestAsset/ArrayTranslator.php b/test/TestAsset/ArrayTranslator.php index f47ed1443..8b67998a6 100644 --- a/test/TestAsset/ArrayTranslator.php +++ b/test/TestAsset/ArrayTranslator.php @@ -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; } } diff --git a/test/TestAsset/Translator.php b/test/TestAsset/Translator.php new file mode 100644 index 000000000..da2c87c01 --- /dev/null +++ b/test/TestAsset/Translator.php @@ -0,0 +1,17 @@ +assertSame($translator, AbstractValidator::getDefaultTranslator()); } diff --git a/test/ValidatorPluginManagerTest.php b/test/ValidatorPluginManagerTest.php index c9a860287..4dfa882c1 100644 --- a/test/ValidatorPluginManagerTest.php +++ b/test/ValidatorPluginManagerTest.php @@ -27,7 +27,7 @@ public function setUp() public function testAllowsInjectingTranslator() { - $translator = $this->getMock("Zend\I18n\Translator\Translator"); + $translator = $this->getMock('ZendTest\Validator\TestAsset\Translator'); $slContents = array(array('translator', $translator)); $serviceLocator = $this->getMock('Zend\ServiceManager\ServiceLocatorInterface');