New Zend\Validator\Bitwise #4940

Merged
merged 15 commits into from Oct 22, 2013

Conversation

Projects
None yet
4 participants
Contributor

corentin-larose commented Aug 9, 2013

Usage:

<?php
use Zend\validator\Bitwise;

$execute    = 0x1;
$write      = 0x2;
$read       = 0x4;
$controlSum = 0x7; // 0x1 | 0x2 | 0x4 === 0x7

$validator = new Bitwise($controlSum, Bitwise::OP_AND, false);

// or
$validator = new Bitwise(array(
    'control'  => $controlSum,
    'operator' => Bitwise::OP_AND,
    'strict'   => false
));

// or
$validator = new Bitwise();
$validator->setControl($controlSum);
$validator->setOperator(Bitwise::OP_AND);
$validator->setStrict(false); // Default value, just for example

// Does my value has at least one allowed bit set ?
$validator = new Bitwise($controlSum, Bitwise::OP_AND, false);
$validator->isValid(0x1); // true
$validator->isValid(0x2); // true
$validator->isValid(0x4); // true
$validator->isValid(0x1 | 0x8); // true since strict option is set to false by default
$validator->isValid(0x8); // false

// Does my value has only allowed bits set ?
$validator = new Bitwise($controlSum, Bitwise::OP_AND, true);
$validator->isValid(0x1); // true
$validator->isValid(0x2); // true
$validator->isValid(0x4); // true
$validator->isValid(0x1 | 0x8); // false since strict option is set to true
$validator->isValid(0x8); // false

// Does my value has only not allowed bit sets ?
$validator = new Bitwise($controlSum, Bitwise::OP_XOR);
$validator->isValid(0x1); // false
$validator->isValid(0x2); // false
$validator->isValid(0x4); // false
$validator->isValid(0x1 | 0x8); // false
$validator->isValid(0x8); // true

Corentin Larose added some commits Aug 9, 2013

@samsonasik samsonasik and 1 other commented on an outdated diff Aug 13, 2013

tests/ZendTest/Validator/BitwiseTest.php
@@ -0,0 +1,132 @@
+<?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
+ * @package Zend_Validator
@samsonasik

samsonasik Sep 3, 2013

Contributor

it ( @Package ) need to be removed

@corentin-larose

corentin-larose Sep 4, 2013

Contributor

Sorry, missed this spot the first time !

@samsonasik samsonasik commented on an outdated diff Aug 13, 2013

tests/ZendTest/Validator/BitwiseTest.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
+ * @package Zend_Validator
+ */
+
+namespace ZendTest\Validator;
+
+use Zend\Validator\Bitwise;
+
+/**
+ * @category Zend
+ * @package Zend
+ * @subpackage UnitTests
@samsonasik

samsonasik Aug 13, 2013

Contributor

remove @category @Package and @subpackage

Corentin Larose Docblock cleanup d1477eb

@weierophinney weierophinney commented on an outdated diff Aug 19, 2013

library/Zend/Validator/Bitwise.php
+ */
+ 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 = ArrayUtils::iteratorToArray($options);
@weierophinney

weierophinney Aug 19, 2013

Owner

If you do not have nested options (i.e., if the options are only one level deep), just use iterator_to_array here.

@weierophinney weierophinney commented on an outdated diff Aug 19, 2013

library/Zend/Validator/Bitwise.php
+
+ $ret = false;
+
+ if (self::OP_AND === $this->operator) {
+ if ($this->strict) {
+ // All the bits set in value must be set in control
+ $ret = (boolean) (($this->control & $value) == $value);
+ } else {
+ // At least one of the bits must be common between value and control
+ $ret = (boolean) ($this->control & $value);
+ }
+ } elseif (self::OP_XOR === $this->operator) {
+ $ret = (boolean) (($this->control ^ $value) === ($this->control | $value));
+ }
+
+ return $ret;
@weierophinney

weierophinney Aug 19, 2013

Owner

I'd break this up a bit; instead of waiting until the end to return a value, return directly from within your conditionals:

if (self::OP_AND === $this->operator) {
    if ($this->strict) {
        return (bool) (($this->control & $value) == $value);
    }
    return (bool) ($this->control & $value);
}

if (self::OP_XOR === $this->operator) {
    return (bool) (($this->control ^ $value) === ($this->control | $value));
}

return false;

Note also the use of (bool) instead of (boolean); do that for consistency with the rest of the framework.

Owner

weierophinney commented Aug 19, 2013

@corentin-larose Looks good - incorporate or answer the various feedback, and I'll review to merge.

Contributor

corentin-larose commented Aug 20, 2013

Ok, I will take your changes and commit again.

Contributor

corentin-larose commented Sep 19, 2013

@weierophinney should be ok/compliant if you want to merge.

Corentin Larose Fixed constructor 353f592
Member

Maks3w commented Oct 21, 2013

@corentin-larose Your test are failing in Travis-CI

[concat] There were 12 errors:
[concat]
[concat] 1) ZendTest\Validator\BitwiseTest::testConstruct with data set #0 (array(), array(0, NULL, false))
[concat] Failed asserting that null is identical to 0.
[concat]
[concat] /home/travis/build/zendframework/zf2/tests/ZendTest/Validator/BitwiseTest.php:32
[concat]
[concat] 2) ZendTest\Validator\BitwiseTest::testConstruct with data set #1 (array(1), array(1, NULL, false))
[concat] Failed asserting that null is identical to 1.
[concat]
[concat] /home/travis/build/zendframework/zf2/tests/ZendTest/Validator/BitwiseTest.php:32
[concat]
[concat] 3) ZendTest\Validator\BitwiseTest::testConstruct with data set #2 (array(1, 'and'), array(1, 'and', false))
[concat] Failed asserting that null is identical to 1.
[concat]
[concat] /home/travis/build/zendframework/zf2/tests/ZendTest/Validator/BitwiseTest.php:32
[concat]
[concat] 4) ZendTest\Validator\BitwiseTest::testConstruct with data set #3 (array(1, 'and', true), array(1, 'and', true))
[concat] Failed asserting that null is identical to 1.
[concat]
[concat] /home/travis/build/zendframework/zf2/tests/ZendTest/Validator/BitwiseTest.php:32
[concat]
[concat] 5) ZendTest\Validator\BitwiseTest::testConstruct with data set #4 (array(1), array(1, NULL, false))
[concat] PHP_CodeCoverage_Exception: Trying to @cover not existing method "Bitwise::isvalid".
[concat]
[concat]
[concat] 6) ZendTest\Validator\BitwiseTest::testConstruct with data set #5 (array(1, 'and'), array(1, 'and', false))
[concat] PHP_CodeCoverage_Exception: Trying to @cover not existing method "Bitwise::isvalid".
[concat]
[concat]
[concat] 7) ZendTest\Validator\BitwiseTest::testConstruct with data set #6 (array(1, 'and', true), array(1, 'and', true))
[concat] PHP_CodeCoverage_Exception: Trying to @cover not existing method "Bitwise::isvalid".
[concat]
[concat]
[concat] 8) ZendTest\Validator\BitwiseTest::testBitwiseAndNotStrict
[concat] PHP_CodeCoverage_Exception: Trying to @cover not existing method "Bitwise::isvalid".
[concat]
[concat]
[concat] 9) ZendTest\Validator\BitwiseTest::testBitwiseAndStrict
[concat] PHP_CodeCoverage_Exception: Trying to @cover not existing method "Bitwise::isvalid".
[concat]
[concat]
[concat] 10) ZendTest\Validator\BitwiseTest::testBitwiseXor
[concat] PHP_CodeCoverage_Exception: Trying to @cover not existing method "Bitwise::isvalid".
[concat]
[concat]
[concat] 11) ZendTest\Validator\BitwiseTest::testSetOperator
[concat] PHP_CodeCoverage_Exception: Trying to @cover not existing method "Bitwise::setOperator".
[concat]
[concat]
[concat] 12) ZendTest\Validator\BitwiseTest::testSetStrict
[concat] PHP_CodeCoverage_Exception: Trying to @cover not existing method "Bitwise::setStrict".

Corentin Larose Fixed U.T 2003e49
Contributor

corentin-larose commented Oct 21, 2013

@Maks3w, my bad, fixed the constructor to work the same way than other validators, forgot to push the U.T

Corentin Larose DockBlock typo fix 9339412
Member

Maks3w commented Oct 21, 2013

@corentin-larose Tests run still failing.

Contributor

corentin-larose commented Oct 22, 2013

Tests are ok, seems that Travis didn't run the most recent code (possible ?) I'm running tests on my last commit: 9339412.

corentin@QAPA-I:/media/localhost/zf2-fork/tests$ phpunit ZendTest/Validator/BitwiseTest.php
PHPUnit 3.7.24 by Sebastian Bergmann.

Configuration read from /media/localhost/zf2-fork/tests/phpunit.xml

.........

Time: 0 seconds, Memory: 3.00Mb

OK (9 tests, 48 assertions)

Member

Maks3w commented Oct 22, 2013

@corentin-larose Please read the log and make the necessary to changes for to have a clean Travis-CI run. If you see the log Travis use even a most recent version than you.

Try using full namespace class name instead the just Bitwise in that anotations.

Contributor

corentin-larose commented Oct 22, 2013

@Maks3w doc block fixed, travis still fails, but on the usual think which is not related with Bitwise validator.

@weierophinney weierophinney added a commit that referenced this pull request Oct 22, 2013

@weierophinney weierophinney Merge pull request #4940 from qapa/zend-validator-bitwise
New Zend\Validator\Bitwise
3e4bc43

@weierophinney weierophinney added a commit that referenced this pull request Oct 22, 2013

@weierophinney weierophinney Merge branch 'feature/4940' into develop
Close #4940
ab5ac46

@weierophinney weierophinney merged commit eda0dac into zendframework:develop Oct 22, 2013

1 check failed

default The Travis CI build failed
Details

corentin-larose deleted the unknown repository branch Mar 14, 2014

@weierophinney weierophinney added a commit to zendframework/zend-validator that referenced this pull request May 15, 2015

@weierophinney weierophinney Merge pull request zendframework/zendframework#4940 from qapa/zend-va…
…lidator-bitwise

New Zend\Validator\Bitwise
392817d

@weierophinney weierophinney added a commit to zendframework/zend-validator that referenced this pull request May 15, 2015

@weierophinney weierophinney Merge branch 'feature/4940' into develop ccac36b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment