Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/5874' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Mar 4, 2014
154 parents 953572d + 4d6040d + 56df441 + 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 + 852db9e + 047576f + 1b49389 + d7193b9 + 3fed016 + 082acdb + ecb5260 + 3ec34d8 + 838bdc9 + 3c0e07f + 6ac386b + d36cffd + 15d41cd + ee3aa95 commit 69a4f37
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 37 deletions.
63 changes: 26 additions & 37 deletions src/NotEmpty.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@

class NotEmpty extends AbstractValidator
{
const BOOLEAN = 1;
const INTEGER = 2;
const FLOAT = 4;
const STRING = 8;
const ZERO = 16;
const EMPTY_ARRAY = 32;
const NULL = 64;
const PHP = 127;
const SPACE = 128;
const OBJECT = 256;
const OBJECT_STRING = 512;
const OBJECT_COUNT = 1024;
const ALL = 2047;
const BOOLEAN = 0x001;
const INTEGER = 0x002;
const FLOAT = 0x004;
const STRING = 0x008;
const ZERO = 0x010;
const EMPTY_ARRAY = 0x020;
const NULL = 0x040;
const PHP = 0x07F;
const SPACE = 0x080;
const OBJECT = 0x100;
const OBJECT_STRING = 0x200;
const OBJECT_COUNT = 0x400;
const ALL = 0x7FF;

const INVALID = 'notEmptyInvalid';
const IS_EMPTY = 'isEmpty';
Expand Down Expand Up @@ -148,9 +148,9 @@ protected function calculateTypeValue($type)
$detected = 0;
foreach ($type as $value) {
if (is_int($value)) {
$detected += $value;
$detected |= $value;
} elseif (in_array($value, $this->constants)) {
$detected += array_search($value, $this->constants);
$detected |= array_search($value, $this->constants);
}
}

Expand Down Expand Up @@ -202,8 +202,7 @@ public function isValid($value)
$object = false;

// OBJECT_COUNT (countable object)
if ($type >= self::OBJECT_COUNT) {
$type -= self::OBJECT_COUNT;
if ($type & self::OBJECT_COUNT) {
$object = true;

if (is_object($value) && ($value instanceof \Countable) && (count($value) == 0)) {
Expand All @@ -213,8 +212,7 @@ public function isValid($value)
}

// OBJECT_STRING (object's toString)
if ($type >= self::OBJECT_STRING) {
$type -= self::OBJECT_STRING;
if ($type & self::OBJECT_STRING) {
$object = true;

if ((is_object($value) && (!method_exists($value, '__toString'))) ||
Expand All @@ -225,8 +223,7 @@ public function isValid($value)
}

// OBJECT (object)
if ($type >= self::OBJECT) {
$type -= self::OBJECT;
if ($type & self::OBJECT) {
// fall trough, objects are always not empty
} elseif ($object === false) {
// object not allowed but object given -> return false
Expand All @@ -237,71 +234,63 @@ public function isValid($value)
}

// SPACE (' ')
if ($type >= self::SPACE) {
$type -= self::SPACE;
if ($type & self::SPACE) {
if (is_string($value) && (preg_match('/^\s+$/s', $value))) {
$this->error(self::IS_EMPTY);
return false;
}
}

// NULL (null)
if ($type >= self::NULL) {
$type -= self::NULL;
if ($type & self::NULL) {
if ($value === null) {
$this->error(self::IS_EMPTY);
return false;
}
}

// EMPTY_ARRAY (array())
if ($type >= self::EMPTY_ARRAY) {
$type -= self::EMPTY_ARRAY;
if ($type & self::EMPTY_ARRAY) {
if (is_array($value) && ($value == array())) {
$this->error(self::IS_EMPTY);
return false;
}
}

// ZERO ('0')
if ($type >= self::ZERO) {
$type -= self::ZERO;
if ($type & self::ZERO) {
if (is_string($value) && ($value == '0')) {
$this->error(self::IS_EMPTY);
return false;
}
}

// STRING ('')
if ($type >= self::STRING) {
$type -= self::STRING;
if ($type & self::STRING) {
if (is_string($value) && ($value == '')) {
$this->error(self::IS_EMPTY);
return false;
}
}

// FLOAT (0.0)
if ($type >= self::FLOAT) {
$type -= self::FLOAT;
if ($type & self::FLOAT) {
if (is_float($value) && ($value == 0.0)) {
$this->error(self::IS_EMPTY);
return false;
}
}

// INTEGER (0)
if ($type >= self::INTEGER) {
$type -= self::INTEGER;
if ($type & self::INTEGER) {
if (is_int($value) && ($value == 0)) {
$this->error(self::IS_EMPTY);
return false;
}
}

// BOOLEAN (false)
if ($type >= self::BOOLEAN) {
$type -= self::BOOLEAN;
if ($type & self::BOOLEAN) {
if (is_bool($value) && ($value == false)) {
$this->error(self::IS_EMPTY);
return false;
Expand Down
65 changes: 65 additions & 0 deletions test/NotEmptyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public function basicProvider()
array(array(5), true),
array(0.0, false),
array(1.0, true),
array(new stdClass(), true),
);
}

Expand Down Expand Up @@ -586,6 +587,25 @@ public function testMultiConstantNotation($value, $valid)
$this->checkValidationValue($value, $valid);
}

/**
* Ensures that the validator follows expected behavior
*
* @param mixed $value Value to test
* @param boolean $valid Expected validity of value
*
* @return void
*
* @dataProvider multiConstantNotationProvider
*/
public function testMultiConstantBooleanOrNotation($value, $valid)
{
$this->validator = new NotEmpty(
NotEmpty::ZERO | NotEmpty::STRING | NotEmpty::BOOLEAN
);

$this->checkValidationValue($value, $valid);
}

/**
* Provides values and their expected validity for boolean empty
*
Expand Down Expand Up @@ -654,6 +674,51 @@ public function stringNotationProvider()
);
}


/**
* Ensures that the validator follows expected behavior so if a string is specified more than once, it doesn't
* cause different validations to run
*
* @param string $string Array of string type values
* @param integer $expected Expected type setting value
*
* @return void
*
* @dataProvider duplicateStringSettingProvider
*/
public function testStringNotationWithDuplicate($string, $expected)
{
$type = array($string, $string);
$this->validator->setType($type);

$this->assertEquals($expected, $this->validator->getType());
}

/**
* Data provider for testStringNotationWithDuplicate method. Provides a string which will be duplicated. The test
* ensures that setting a string value more than once only turns on the appropriate bit once
*
* @return array
*/
public function duplicateStringSettingProvider()
{
return array(
array('boolean', NotEmpty::BOOLEAN),
array('integer', NotEmpty::INTEGER),
array('float', NotEmpty::FLOAT),
array('string', NotEmpty::STRING),
array('zero', NotEmpty::ZERO),
array('array', NotEmpty::EMPTY_ARRAY),
array('null', NotEmpty::NULL),
array('php', NotEmpty::PHP),
array('space', NotEmpty::SPACE),
array('object', NotEmpty::OBJECT),
array('objectstring', NotEmpty::OBJECT_STRING),
array('objectcount', NotEmpty::OBJECT_COUNT),
array('all', NotEmpty::ALL),
);
}

/**
* Ensures that the validator follows expected behavior
*
Expand Down

0 comments on commit 69a4f37

Please sign in to comment.