Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Segregated interfaces for Translator dependency of Validator component #4391

Closed
wants to merge 7 commits into from

2 participants

Matthew Weier O'Phinney Rob Allen
Matthew Weier O'Phinney

This PR introduces segregated interfaces for the Translator dependency inside the Validator component.

This allows us to remove the hard dependency on the i18n component, and allows usage of the Validator with other translator systems via interface implementation.

A bridge is provided in the Zend\Mvc\I18n subcomponent; additionally, the MVC uses a translator factory that consumes that bridge to ensure the translator service works with all components.

weierophinney added some commits
Matthew Weier O'Phinney weierophinney Segregated Translator/TranslatorAware interfaces for Validator
- Allows validator component to use I18n as an optional dependency, by defining
  TranslatorInterface and TranslatorAwareInterface internally.
- MVC component contains a bridge class that extends i18n Translator,
  implementing the validator TranslatorInterface
4bf9a34
Matthew Weier O'Phinney weierophinney Create alternate Translator factory
- that creates and returns the bridged interface
bd8376a
Matthew Weier O'Phinney weierophinney Add bridge Translator service factory to service config
- To ensure the bridge version is used, if ever retrieved.
00b81b5
Matthew Weier O'Phinney weierophinney CS fixes
- per php-cs-fixer
69ce936
Matthew Weier O'Phinney weierophinney Better descriptions for I18n and ServiceManager deps fe8ef66
Rob Allen
Collaborator

Every other factory within Zend\Mvc seems to live in Zend\Mvc\Service. Should TranslatorServiceFactory be in there too?

Matthew Weier O'Phinney

@akrabat Good point. Done!

Rob Allen akrabat closed this pull request from a commit
Rob Allen akrabat Merge branch 'pr4391'
Close #4391
edb98a6
Rob Allen akrabat closed this in edb98a6
Rob Allen akrabat referenced this pull request from a commit
Rob Allen akrabat Merge branch 'pr4391' into develop
Forward port #4391
ad05796
Deleted user Unknown referenced this pull request from a commit
Rob Allen akrabat Merge branch 'pr4391'
Close #4391
a098bca
Deleted user Unknown referenced this pull request from a commit
Rob Allen akrabat Merge branch 'pr4391' into develop
Forward port #4391
b31fbf9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 2, 2013
  1. Matthew Weier O'Phinney

    Segregated Translator/TranslatorAware interfaces for Validator

    weierophinney authored
    - Allows validator component to use I18n as an optional dependency, by defining
      TranslatorInterface and TranslatorAwareInterface internally.
    - MVC component contains a bridge class that extends i18n Translator,
      implementing the validator TranslatorInterface
  2. Matthew Weier O'Phinney

    Create alternate Translator factory

    weierophinney authored
    - that creates and returns the bridged interface
  3. Matthew Weier O'Phinney

    Add bridge Translator service factory to service config

    weierophinney authored
    - To ensure the bridge version is used, if ever retrieved.
  4. Matthew Weier O'Phinney

    CS fixes

    weierophinney authored
    - per php-cs-fixer
  5. Matthew Weier O'Phinney
  6. Matthew Weier O'Phinney

    Move TranslatorServiceFactory

    weierophinney authored
    - from Zend\Mvc\I18n to Zend\Mvc\Service (per @akrabat)
  7. Matthew Weier O'Phinney

    Update service configuration for translator

    weierophinney authored
    - due to previous commit
This page is out of date. Refresh to see the latest.
17 library/Zend/Mvc/I18n/Translator.php
View
@@ -0,0 +1,17 @@
+<?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\Mvc\I18n;
+
+use Zend\I18n\Translator\Translator as I18nTranslator;
+use Zend\Validator\Translator\TranslatorInterface as ValidatorTranslatorInterface;
+
+class Translator extends I18nTranslator implements ValidatorTranslatorInterface
+{
+}
1  library/Zend/Mvc/Service/ServiceListenerFactory.php
View
@@ -63,6 +63,7 @@ class ServiceListenerFactory implements FactoryInterface
'Router' => 'Zend\Mvc\Service\RouterFactory',
'RoutePluginManager' => 'Zend\Mvc\Service\RoutePluginManagerFactory',
'SerializerAdapterManager' => 'Zend\Mvc\Service\SerializerAdapterPluginManagerFactory',
+ 'Translator' => 'Zend\Mvc\Service\TranslatorServiceFactory',
'ValidatorManager' => 'Zend\Mvc\Service\ValidatorManagerFactory',
'ViewHelperManager' => 'Zend\Mvc\Service\ViewHelperManagerFactory',
'ViewFeedRenderer' => 'Zend\Mvc\Service\ViewFeedRendererFactory',
30 library/Zend/Mvc/Service/TranslatorServiceFactory.php
View
@@ -0,0 +1,30 @@
+<?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\Mvc\Service;
+
+use Zend\I18n\Translator\TranslatorServiceFactory as I18nTranslatorServiceFactory;
+use Zend\Mvc\I18n\Translator;
+use Zend\ServiceManager\ServiceLocatorInterface;
+
+/**
+ * Overrides the translator factory from the i18n component in order to
+ * replace it with the bridge class from this namespace.
+ */
+class TranslatorServiceFactory extends I18nTranslatorServiceFactory
+{
+ public function createService(ServiceLocatorInterface $serviceLocator)
+ {
+ // Configure the translator
+ $config = $serviceLocator->get('Config');
+ $trConfig = isset($config['translator']) ? $config['translator'] : array();
+ $translator = Translator::factory($trConfig);
+ return $translator;
+ }
+}
27 library/Zend/Validator/AbstractValidator.php
View
@@ -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 @@
/**
* Default translation object for all validate objects
- * @var Translator
+ * @var Translator\TranslatorInterface
*/
protected static $defaultTranslator;
@@ -49,7 +46,7 @@
'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()
{
69 library/Zend/Validator/Translator/TranslatorAwareInterface.php
View
@@ -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 library/Zend/Validator/Translator/TranslatorInterface.php
View
@@ -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  library/Zend/Validator/ValidatorPluginManager.php
View
@@ -9,7 +9,6 @@
namespace Zend\Validator;
-use Zend\I18n\Translator\TranslatorAwareInterface;
use Zend\ServiceManager\AbstractPluginManager;
use Zend\ServiceManager\ConfigInterface;
@@ -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'));
10 library/Zend/Validator/composer.json
View
@@ -14,16 +14,18 @@
"target-dir": "Zend/Validator",
"require": {
"php": ">=5.3.3",
- "zendframework/zend-i18n": "self.version",
- "zendframework/zend-servicemanager": "self.version",
"zendframework/zend-stdlib": "self.version"
},
"require-dev": {
- "zendframework/zend-math": "self.version"
+ "zendframework/zend-i18n": "self.version",
+ "zendframework/zend-math": "self.version",
+ "zendframework/zend-servicemanager": "self.version"
},
"suggest": {
"zendframework/zend-db": "Zend\\Db component",
- "zendframework/zend-math": "Zend\\Math component"
+ "zendframework/zend-i18n": "Zend\\I18n component to allow translation of validation error messages as well as to use the various Date validators",
+ "zendframework/zend-math": "Zend\\Math component",
+ "zendframework/zend-servicemanager": "Zend\\ServiceManager component to allow using the ValidatorPluginManager and validator chains"
},
"extra": {
"branch-alias": {
15 tests/ZendTest/Validator/AbstractTest.php
View
@@ -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());
3  tests/ZendTest/Validator/EmailAddressTest.php
View
@@ -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);
3  tests/ZendTest/Validator/HostnameTest.php
View
@@ -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);
9 tests/ZendTest/Validator/StaticValidatorTest.php
View
@@ -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());
}
6 tests/ZendTest/Validator/TestAsset/ArrayTranslator.php
View
@@ -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;
}
}
17 tests/ZendTest/Validator/TestAsset/Translator.php
View
@@ -0,0 +1,17 @@
+<?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 ZendTest\Validator\TestAsset;
+
+use Zend\I18n\Translator\Translator as I18nTranslator;
+use Zend\Validator\Translator\TranslatorInterface as ValidatorTranslatorInterface;
+
+class Translator extends I18nTranslator implements ValidatorTranslatorInterface
+{
+}
3  tests/ZendTest/Validator/ValidatorChainTest.php
View
@@ -10,7 +10,6 @@
namespace ZendTest\Validator;
-use Zend\I18n\Translator\Translator;
use Zend\Validator\AbstractValidator;
use Zend\Validator\Between;
use Zend\Validator\NotEmpty;
@@ -142,7 +141,7 @@ public function testSetGetMessageLengthLimitation()
public function testSetGetDefaultTranslator()
{
- $translator = new Translator();
+ $translator = new TestAsset\Translator();
AbstractValidator::setDefaultTranslator($translator);
$this->assertSame($translator, AbstractValidator::getDefaultTranslator());
}
2  tests/ZendTest/Validator/ValidatorPluginManagerTest.php
View
@@ -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');
Something went wrong with that request. Please try again.