This repository has been archived by the owner on Jan 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'form-url' of https://github.com/cgmartin/zf2 into featu…
…re/form-url Conflicts: library/Zend/Form/View/HelperConfiguration.php
- Loading branch information
Showing
2 changed files
with
352 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
<?php | ||
/** | ||
* Zend Framework | ||
* | ||
* 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 license@zend.com 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 | ||
*/ | ||
|
||
namespace Zend\Validator; | ||
|
||
use Traversable; | ||
use Zend\Uri\Uri as UriHandler; | ||
use Zend\Uri\Exception\ExceptionInterface as UriException; | ||
|
||
/** | ||
* @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 | ||
*/ | ||
class Uri extends AbstractValidator | ||
{ | ||
const INVALID = 'uriInvalid'; | ||
const NOT_URI = 'notUri'; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $messageTemplates = array( | ||
self::INVALID => "Invalid type given. String expected", | ||
self::NOT_URI => "'%value%' does not appear to be a valid Uri", | ||
); | ||
|
||
/** | ||
* @var UriHandler | ||
*/ | ||
protected $uriHandler; | ||
|
||
/** | ||
* @var boolean | ||
*/ | ||
protected $allowRelative = true; | ||
|
||
/** | ||
* @var boolean | ||
*/ | ||
protected $allowAbsolute = true; | ||
|
||
/** | ||
* Sets default option values for this instance | ||
* | ||
* @param array|\Traversable $options | ||
*/ | ||
public function __construct($options = array()) | ||
{ | ||
if ($options instanceof Traversable) { | ||
$options = iterator_to_array($options); | ||
} elseif (!is_array($options)) { | ||
$options = func_get_args(); | ||
$temp['uriHandler'] = array_shift($options); | ||
if (!empty($options)) { | ||
$temp['allowRelative'] = array_shift($options); | ||
} | ||
if (!empty($options)) { | ||
$temp['allowAbsolute'] = array_shift($options); | ||
} | ||
|
||
$options = $temp; | ||
} | ||
|
||
if (isset($options['uriHandler'])) { | ||
$this->setUriHandler($options['uriHandler']); | ||
} | ||
if (isset($options['allowRelative'])) { | ||
$this->setAllowRelative($options['allowRelative']); | ||
} | ||
if (isset($options['allowAbsolute'])) { | ||
$this->setAllowAbsolute($options['allowAbsolute']); | ||
} | ||
|
||
parent::__construct($options); | ||
} | ||
|
||
/** | ||
* @return UriHandler | ||
*/ | ||
public function getUriHandler() | ||
{ | ||
if (null === $this->uriHandler) { | ||
// Lazy load the base Uri handler | ||
$this->uriHandler = new UriHandler(); | ||
} | ||
return $this->uriHandler; | ||
} | ||
|
||
/** | ||
* @param UriHandler $uriHandler | ||
* @return Uri | ||
*/ | ||
public function setUriHandler($uriHandler) | ||
{ | ||
$this->uriHandler = $uriHandler; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Returns the allowAbsolute option | ||
* | ||
* @return boolean | ||
*/ | ||
public function getAllowAbsolute() | ||
{ | ||
return $this->allowAbsolute; | ||
} | ||
|
||
/** | ||
* Sets the allowAbsolute option | ||
* | ||
* @param boolean $allowWhiteSpace | ||
* @return Uri | ||
*/ | ||
public function setAllowAbsolute($allowAbsolute) | ||
{ | ||
$this->allowAbsolute = (boolean) $allowAbsolute; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Returns the allowRelative option | ||
* | ||
* @return boolean | ||
*/ | ||
public function getAllowRelative() | ||
{ | ||
return $this->allowRelative; | ||
} | ||
|
||
/** | ||
* Sets the allowRelative option | ||
* | ||
* @param boolean $allowRelative | ||
* @return Uri | ||
*/ | ||
public function setAllowRelative($allowRelative) | ||
{ | ||
$this->allowRelative = (boolean) $allowRelative; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Returns true if and only if $value validates as a Uri | ||
* | ||
* @param string $value | ||
* @return boolean | ||
*/ | ||
public function isValid($value) | ||
{ | ||
if (!is_string($value)) { | ||
$this->error(self::INVALID); | ||
return false; | ||
} | ||
|
||
$uriHandler = $this->getUriHandler(); | ||
try { | ||
$uriHandler->parse($value); | ||
if ($uriHandler->isValid()) { | ||
// It will either be a valid absolute or relative URI | ||
if (($this->allowRelative && $this->allowAbsolute) | ||
|| ($this->allowAbsolute && $uriHandler->isAbsolute()) | ||
|| ($this->allowRelative && $uriHandler->isValidRelative()) | ||
) { | ||
return true; | ||
} | ||
} | ||
} catch (UriException $ex) { | ||
// Error parsing URI, it must be invalid | ||
} | ||
|
||
$this->error(self::NOT_URI); | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
<?php | ||
/** | ||
* Zend Framework | ||
* | ||
* 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 license@zend.com so we can send you a copy immediately. | ||
* | ||
* @category Zend | ||
* @package Zend_Validator | ||
* @subpackage UnitTests | ||
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace ZendTest\Validator; | ||
|
||
use Zend\Validator; | ||
use Zend\Uri\Exception\InvalidArgumentException; | ||
|
||
/** | ||
* Test helper | ||
*/ | ||
|
||
/** | ||
* @see \Zend\Validator\Uri | ||
*/ | ||
|
||
/** | ||
* @category Zend | ||
* @package Zend_Validator | ||
* @subpackage UnitTests | ||
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
* @group Zend_Validator | ||
*/ | ||
class UriTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var \Zend\Validator\Uri | ||
*/ | ||
protected $validator; | ||
|
||
/** | ||
* Creates a new Uri Validator object for each test method | ||
* | ||
* @return void | ||
*/ | ||
public function setUp() | ||
{ | ||
$this->validator = new Validator\Uri(); | ||
} | ||
|
||
public function testHasDefaultSettingsAndLazyLoadsUriHandler() | ||
{ | ||
$validator = $this->validator; | ||
$uriHandler = $validator->getUriHandler(); | ||
$this->assertInstanceOf('Zend\Uri\Uri', $uriHandler); | ||
$this->assertTrue($validator->getAllowRelative()); | ||
$this->assertTrue($validator->getAllowAbsolute()); | ||
} | ||
|
||
public function testConstructorWithArraySetsOptions() | ||
{ | ||
$uriMock = $this->getMock('Zend\Uri\Uri'); | ||
$validator = new Validator\Uri(array( | ||
'uriHandler' => $uriMock, | ||
'allowRelative' => false, | ||
'allowAbsolute' => false, | ||
)); | ||
$this->assertEquals($uriMock, $validator->getUriHandler()); | ||
$this->assertFalse($validator->getAllowRelative()); | ||
$this->assertFalse($validator->getAllowAbsolute()); | ||
} | ||
|
||
public function testConstructorWithArgsSetsOptions() | ||
{ | ||
$uriMock = $this->getMock('Zend\Uri\Uri'); | ||
$validator = new Validator\Uri($uriMock, false, false); | ||
$this->assertEquals($uriMock, $validator->getUriHandler()); | ||
$this->assertFalse($validator->getAllowRelative()); | ||
$this->assertFalse($validator->getAllowAbsolute()); | ||
} | ||
|
||
public function allowOptionsDataProvider() | ||
{ | ||
return array( | ||
// allowAbsolute allowRelative isAbsolute isRelative isValid expects | ||
array(true, true, true, false, true, true), | ||
array(true, true, false, true, true, true), | ||
array(false, true, true, false, true, false), | ||
array(false, true, false, true, true, true), | ||
array(true, false, true, false, true, true), | ||
array(true, false, false, true, true, false), | ||
array(false, false, true, false, true, false), | ||
array(false, false, false, true, true, false), | ||
array(true, true, false, false, false, false), | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider allowOptionsDataProvider | ||
*/ | ||
public function testUriHandlerBehaviorWithAllowSettings( | ||
$allowAbsolute, $allowRelative, $isAbsolute, $isRelative, $isValid, $expects | ||
) { | ||
$uriMock = $this->getMock( | ||
'Zend\Uri\Uri', | ||
array('parse', 'isValid', 'isAbsolute', 'isValidRelative') | ||
); | ||
$uriMock->expects($this->once()) | ||
->method('isValid')->will($this->returnValue($isValid)); | ||
$uriMock->expects($this->any()) | ||
->method('isAbsolute')->will($this->returnValue($isAbsolute)); | ||
$uriMock->expects($this->any()) | ||
->method('isValidRelative')->will($this->returnValue($isRelative)); | ||
|
||
$this->validator->setUriHandler($uriMock) | ||
->setAllowAbsolute($allowAbsolute) | ||
->setAllowRelative($allowRelative); | ||
|
||
$this->assertEquals($expects, $this->validator->isValid('uri')); | ||
} | ||
|
||
public function testUriHandlerThrowsExceptionInParseMethodNotValid() | ||
{ | ||
$uriMock = $this->getMock('Zend\Uri\Uri'); | ||
$uriMock->expects($this->once()) | ||
->method('parse') | ||
->will($this->throwException(new InvalidArgumentException())); | ||
|
||
$this->validator->setUriHandler($uriMock); | ||
$this->assertFalse($this->validator->isValid('uri')); | ||
} | ||
|
||
/** | ||
* Ensures that getMessages() returns expected default value | ||
* | ||
* @return void | ||
*/ | ||
public function testGetMessages() | ||
{ | ||
$this->assertEquals(array(), $this->validator->getMessages()); | ||
} | ||
|
||
public function testEqualsMessageTemplates() | ||
{ | ||
$validator = $this->validator; | ||
$this->assertObjectHasAttribute('messageTemplates', $validator); | ||
$this->assertAttributeEquals($validator->getOption('messageTemplates'), 'messageTemplates', $validator); | ||
} | ||
} |