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 'feature/4940' into develop
- Loading branch information
149 parents
d1facdb
+
1b49389
+
d7193b9
+
3fed016
+
082acdb
+
3ddc8fa
+
274bd7f
+
c9e433d
+
99f6515
+
8d1a271
+
2c2bc35
+
16359e3
+
10b9b3c
+
d05ac01
+
9628c66
+
66c8763
+
943d333
+
8342f70
+
4eb4184
+
4ea0bc2
+
860b725
+
7d29287
+
49a7844
+
3836aa0
+
de1fe83
+
929eb12
+
6e6902f
+
f823467
+
f3b3f76
+
93aff47
+
5f2adec
+
7674ec8
+
2200ed2
+
f033c86
+
d43a2da
+
4434323
+
88b21e1
+
b02b602
+
b692de7
+
7f833d2
+
2eaf4da
+
a7848de
+
5bd9f85
+
d7a26e8
+
173f53d
+
bd77e8e
+
45f017e
+
118612c
+
3a09f79
+
47d92bc
+
dbf56ad
+
a753b61
+
6467186
+
5ac4124
+
2bf68ca
+
e7cd709
+
7a552db
+
f112e0c
+
cec42fc
+
066eb37
+
3569d8b
+
00def10
+
ecae47b
+
0e552a5
+
4f854c2
+
2b17650
+
c1c0447
+
73b1f80
+
dd4a335
+
a40eb42
+
9262db1
+
bdbd950
+
6d50117
+
10b08a7
+
f692a0d
+
ebe63d3
+
528260b
+
5f04a7f
+
c0dcd12
+
224f280
+
8785f25
+
866539b
+
d711927
+
e280213
+
002f0d4
+
5ffcbbe
+
8937705
+
5803840
+
3d7cd9a
+
6b14f0e
+
24bd169
+
0eba870
+
4e4acfe
+
9e243bd
+
e156354
+
2a84563
+
3f1f758
+
4e425f4
+
7030f97
+
ea08a0f
+
213ebd9
+
5d80740
+
800c29c
+
be31ff1
+
8f989d6
+
8b02a8b
+
4cfd82c
+
c411f06
+
4229561
+
b321e3b
+
2e660f9
+
3fff65f
+
a2eb7bf
+
82469ff
+
22fa741
+
99819cb
+
8c16404
+
a7c0820
+
7d277af
+
da0bec5
+
33f214a
+
a07f208
+
963d2fe
+
1100f88
+
6713bf5
+
7421758
+
b64a638
+
772a2a1
+
dff3231
+
56bc4ca
+
463e3d7
+
14bd316
+
f8b9e58
+
ef0268c
+
3fe91ce
+
130da19
+
6d4097d
+
02fa5b3
+
927b7df
+
2fbc2a0
+
c772270
+
1cdc0cc
+
ecb5260
+
23824d1
+
56e8233
+
3c0e07f
+
8fb917d
+
575c951
+
392817d
commit ccac36b
Showing
2 changed files
with
373 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,192 @@ | ||
<?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; | ||
|
||
use Traversable; | ||
|
||
class Bitwise extends AbstractValidator | ||
{ | ||
const OP_AND = 'and'; | ||
const OP_XOR = 'xor'; | ||
|
||
const NOT_AND = 'notAnd'; | ||
const NOT_AND_STRICT = 'notAndStrict'; | ||
const NOT_XOR = 'notXor'; | ||
|
||
/** | ||
* @var integer | ||
*/ | ||
protected $control; | ||
|
||
/** | ||
* Validation failure message template definitions | ||
* | ||
* @var array | ||
*/ | ||
protected $messageTemplates = array( | ||
self::NOT_AND => "The input has no common bit set with '%control%'", | ||
self::NOT_AND_STRICT => "The input doesn't have the same bits set as '%control%'", | ||
self::NOT_XOR => "The input has common bit set with '%control%'", | ||
); | ||
|
||
/** | ||
* Additional variables available for validation failure messages | ||
* | ||
* @var array | ||
*/ | ||
protected $messageVariables = array( | ||
'control' => 'control', | ||
); | ||
|
||
/** | ||
* @var integer | ||
*/ | ||
protected $operator; | ||
|
||
/** | ||
* @var boolean | ||
*/ | ||
protected $strict = false; | ||
|
||
/** | ||
* Sets validator options | ||
* Accepts the following option keys: | ||
* 'control' => integer | ||
* 'operator' => | ||
* 'strict' => boolean | ||
* | ||
* @param array|Traversable $options | ||
*/ | ||
public function __construct($options = null) | ||
{ | ||
if ($options instanceof Traversable) { | ||
$options = iterator_to_array($options); | ||
} | ||
|
||
if (!is_array($options)) { | ||
$options = func_get_args(); | ||
|
||
$temp['control'] = array_shift($options); | ||
|
||
if (!empty($options)) { | ||
$temp['operator'] = array_shift($options); | ||
} | ||
|
||
if (!empty($options)) { | ||
$temp['strict'] = array_shift($options); | ||
} | ||
|
||
$options = $temp; | ||
} | ||
|
||
parent::__construct($options); | ||
} | ||
|
||
/** | ||
* Returns the control parameter. | ||
* | ||
* @return integer | ||
*/ | ||
public function getControl() | ||
{ | ||
return $this->control; | ||
} | ||
|
||
/** | ||
* Returns the operator parameter. | ||
* | ||
* @return string | ||
*/ | ||
public function getOperator() | ||
{ | ||
return $this->operator; | ||
} | ||
|
||
/** | ||
* Returns the strict parameter. | ||
* | ||
* @return boolean | ||
*/ | ||
public function getStrict() | ||
{ | ||
return $this->strict; | ||
} | ||
|
||
/** | ||
* Returns true if and only if $value is between min and max options, inclusively | ||
* if inclusive option is true. | ||
* | ||
* @param mixed $value | ||
* @return bool | ||
*/ | ||
public function isValid($value) | ||
{ | ||
$this->setValue($value); | ||
|
||
if (self::OP_AND === $this->operator) { | ||
if ($this->strict) { | ||
// All the bits set in value must be set in control | ||
$this->error(self::NOT_AND_STRICT); | ||
|
||
return (bool) (($this->control & $value) == $value); | ||
} else { | ||
// At least one of the bits must be common between value and control | ||
$this->error(self::NOT_AND); | ||
|
||
return (bool) ($this->control & $value); | ||
} | ||
} elseif (self::OP_XOR === $this->operator) { | ||
$this->error(self::NOT_XOR); | ||
|
||
return (bool) (($this->control ^ $value) === ($this->control | $value)); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Sets the control parameter. | ||
* | ||
* @param integer $control | ||
* @return Bitwise | ||
*/ | ||
public function setControl($control) | ||
{ | ||
$this->control = (int) $control; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Sets the operator parameter. | ||
* | ||
* @param string $operator | ||
* @return Bitwise | ||
*/ | ||
public function setOperator($operator) | ||
{ | ||
$this->operator = $operator; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Sets the strict parameter. | ||
* | ||
* @param boolean $strict | ||
* @return Bitwise | ||
*/ | ||
public function setStrict($strict) | ||
{ | ||
$this->strict = (bool) $strict; | ||
|
||
return $this; | ||
} | ||
} |
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,181 @@ | ||
<?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; | ||
|
||
use Zend\Validator\Bitwise; | ||
|
||
class BitwiseTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var \Zend\Validator\Bitwise | ||
*/ | ||
public $validator; | ||
|
||
public function setUp() | ||
{ | ||
$this->validator = new Bitwise(); | ||
} | ||
|
||
/** | ||
* @covers \Zend\Validator\Bitwise::__construct() | ||
* @dataProvider constructDataProvider | ||
* | ||
* @param array $args | ||
* @param array $options | ||
*/ | ||
public function testConstruct(array $args, array $options) | ||
{ | ||
$validator = new Bitwise($args); | ||
|
||
$this->assertSame($options['control'], $validator->getControl()); | ||
$this->assertSame($options['operator'], $validator->getOperator()); | ||
$this->assertSame($options['strict'], $validator->getStrict()); | ||
} | ||
|
||
public function constructDataProvider() | ||
{ | ||
return array( | ||
array( | ||
array(), | ||
array('control' => null, 'operator' => null, 'strict' => false), | ||
), | ||
array( | ||
array('control' => 0x1), | ||
array('control' => 0x1, 'operator' => null, 'strict' => false), | ||
), | ||
array( | ||
array('control' => 0x1, 'operator' => Bitwise::OP_AND), | ||
array('control' => 0x1, 'operator' => Bitwise::OP_AND, 'strict' => false), | ||
), | ||
array( | ||
array('control' => 0x1, 'operator' => Bitwise::OP_AND, 'strict' => true), | ||
array('control' => 0x1, 'operator' => Bitwise::OP_AND, 'strict' => true), | ||
), | ||
); | ||
|
||
} | ||
|
||
/** | ||
* @covers \Zend\Validator\Bitwise::isvalid() | ||
*/ | ||
public function testBitwiseAndNotStrict() | ||
{ | ||
$controlSum = 0x7; // (0x1 | 0x2 | 0x4) === 0x7 | ||
|
||
$validator = new Bitwise(); | ||
$validator->setControl($controlSum); | ||
$validator->setOperator(Bitwise::OP_AND); | ||
|
||
$this->assertTrue($validator->isValid(0x1)); | ||
$this->assertTrue($validator->isValid(0x2)); | ||
$this->assertTrue($validator->isValid(0x4)); | ||
$this->assertFalse($validator->isValid(0x8)); | ||
|
||
$validator->isValid(0x8); | ||
$messages = $validator->getMessages(); | ||
$this->assertArrayHasKey($validator::NOT_AND, $messages); | ||
$this->assertSame("The input has no common bit set with '$controlSum'", $messages[$validator::NOT_AND]); | ||
|
||
$this->assertTrue($validator->isValid(0x1 | 0x2)); | ||
$this->assertTrue($validator->isValid(0x1 | 0x2 | 0x4)); | ||
$this->assertTrue($validator->isValid(0x1 | 0x8)); | ||
} | ||
|
||
/** | ||
* @covers \Zend\Validator\Bitwise::isvalid() | ||
*/ | ||
public function testBitwiseAndStrict() | ||
{ | ||
$controlSum = 0x7; // (0x1 | 0x2 | 0x4) === 0x7 | ||
|
||
$validator = new Bitwise(); | ||
$validator->setControl($controlSum); | ||
$validator->setOperator(Bitwise::OP_AND); | ||
$validator->setStrict(true); | ||
|
||
$this->assertTrue($validator->isValid(0x1)); | ||
$this->assertTrue($validator->isValid(0x2)); | ||
$this->assertTrue($validator->isValid(0x4)); | ||
$this->assertFalse($validator->isValid(0x8)); | ||
|
||
$validator->isValid(0x8); | ||
$messages = $validator->getMessages(); | ||
$this->assertArrayHasKey($validator::NOT_AND_STRICT, $messages); | ||
$this->assertSame("The input doesn't have the same bits set as '$controlSum'", $messages[$validator::NOT_AND_STRICT]); | ||
|
||
$this->assertTrue($validator->isValid(0x1 | 0x2)); | ||
$this->assertTrue($validator->isValid(0x1 | 0x2 | 0x4)); | ||
$this->assertFalse($validator->isValid(0x1 | 0x8)); | ||
} | ||
|
||
/** | ||
* @covers \Zend\Validator\Bitwise::isvalid() | ||
*/ | ||
public function testBitwiseXor() | ||
{ | ||
$controlSum = 0x5; // (0x1 | 0x4) === 0x5 | ||
|
||
$validator = new Bitwise(); | ||
$validator->setControl($controlSum); | ||
$validator->setOperator(Bitwise::OP_XOR); | ||
|
||
$this->assertTrue($validator->isValid(0x2)); | ||
$this->assertTrue($validator->isValid(0x8)); | ||
$this->assertTrue($validator->isValid(0x10)); | ||
$this->assertFalse($validator->isValid(0x1)); | ||
$this->assertFalse($validator->isValid(0x4)); | ||
|
||
$validator->isValid(0x4); | ||
$messages = $validator->getMessages(); | ||
$this->assertArrayHasKey($validator::NOT_XOR, $messages); | ||
$this->assertSame("The input has common bit set with '$controlSum'", $messages[$validator::NOT_XOR]); | ||
|
||
$this->assertTrue($validator->isValid(0x8 | 0x10)); | ||
$this->assertFalse($validator->isValid(0x1 | 0x4)); | ||
$this->assertFalse($validator->isValid(0x1 | 0x8)); | ||
$this->assertFalse($validator->isValid(0x4 | 0x8)); | ||
} | ||
|
||
/** | ||
* @covers \Zend\Validator\Bitwise::setOperator() | ||
*/ | ||
public function testSetOperator() | ||
{ | ||
$validator = new Bitwise(); | ||
|
||
$validator->setOperator(Bitwise::OP_AND); | ||
$this->assertSame(Bitwise::OP_AND, $validator->getOperator()); | ||
|
||
$validator->setOperator(Bitwise::OP_XOR); | ||
$this->assertSame(Bitwise::OP_XOR, $validator->getOperator()); | ||
} | ||
|
||
/** | ||
* @covers \Zend\Validator\Bitwise::setStrict() | ||
*/ | ||
public function testSetStrict() | ||
{ | ||
$validator = new Bitwise(); | ||
|
||
$this->assertFalse($validator->getStrict(), 'Strict false by default'); | ||
|
||
$validator->setStrict(false); | ||
$this->assertFalse($validator->getStrict()); | ||
|
||
$validator->setStrict(true); | ||
$this->assertTrue($validator->getStrict()); | ||
|
||
$validator = new Bitwise(0x1, Bitwise::OP_AND, false); | ||
$this->assertFalse($validator->getStrict()); | ||
|
||
$validator = new Bitwise(0x1, Bitwise::OP_AND, true); | ||
$this->assertTrue($validator->getStrict()); | ||
} | ||
} |