public function validate(array $attributes) { if (empty($attributes[$this->_attributeName])) { return true; } $attributeValues = $attributes[$this->_attributeName]; switch ($this->_options) { case 'URN': $urnValidator = new EngineBlock_Validator_Urn(); foreach ($attributeValues as $attributeValue) { if (!$urnValidator->validate($attributeValue)) { $this->_messages[] = array(self::ERROR_ATTRIBUTE_VALIDATOR_URN, $this->_attributeName, $this->_options, $attributeValue); return false; } } break; case 'HostName': $hostnameValidator = new Zend_Validate_Hostname(); foreach ($attributeValues as $attributeValue) { if (!$hostnameValidator->isValid($attributeValue)) { $this->_messages[] = array(self::ERROR_ATTRIBUTE_VALIDATOR_HOSTNAME, $this->_attributeName, $this->_options, $attributeValue); return false; } } break; case 'URL': foreach ($attributeValues as $attributeValue) { if (!Zend_Uri::check($attributeValue)) { $this->_messages[] = array(self::ERROR_ATTRIBUTE_VALIDATOR_URL, $this->_attributeName, $this->_options, $attributeValue); return false; } } break; case 'URI': $uriValidator = new EngineBlock_Validator_Uri(); foreach ($attributeValues as $attributeValue) { if (!$uriValidator->validate($attributeValue)) { $this->_messages[] = array(self::ERROR_ATTRIBUTE_VALIDATOR_URI, $this->_attributeName, $this->_options, $attributeValue); return false; } } break; case 'EmailAddress': $emailValidator = new Zend_Validate_EmailAddress(); foreach ($attributeValues as $attributeValue) { if (!$emailValidator->isValid($attributeValue)) { $this->_messages[] = array(self::ERROR_ATTRIBUTE_VALIDATOR_EMAIL, $this->_attributeName, $this->_options, $attributeValue); return false; } } break; default: throw new EngineBlock_Exception("Unknown validate option '{$this->_options}' for attribute validation"); } return true; }
/** * @dataProvider validUriProvider */ public function testUriValidates($uri) { $this->assertTrue($this->validator->validate($uri)); }
/** * @dataProvider invalidUrnProvider */ public function testUrnValidationFails($invalidUrn) { $this->assertFalse($this->validator->validate($invalidUrn)); }