Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Replacing the magic number for a list of constants in Validator\NotEmpty #4489

Merged
merged 2 commits into from
This page is out of date. Refresh to see the latest.
View
53 library/Zend/Validator/NotEmpty.php
@@ -48,6 +48,21 @@ class NotEmpty extends AbstractValidator
);
/**
+ * Default value for types; value = 493
+ *
+ * @var array
+ */
+ protected $defaultType = array(
+ self::OBJECT,
+ self::SPACE,
+ self::NULL,
+ self::EMPTY_ARRAY,
+ self::STRING,
+ self::FLOAT,
+ self::BOOLEAN
+ );
+
+ /**
* @var array
*/
protected $messageTemplates = array(
@@ -60,9 +75,7 @@ class NotEmpty extends AbstractValidator
*
* @var array
*/
- protected $options = array(
- 'type' => 493, // Internal type to detect
- );
+ protected $options = array();
/**
* Constructor
@@ -71,6 +84,8 @@ class NotEmpty extends AbstractValidator
*/
public function __construct($options = null)
{
+ $this->setType($this->defaultType);
+
if ($options instanceof Traversable) {
$options = ArrayUtils::iteratorToArray($options);
}
@@ -116,13 +131,18 @@ public function getType()
}
/**
- * Set the types
- *
- * @param int|array $type
- * @throws Exception\InvalidArgumentException
- * @return NotEmpty
+ * @return int
*/
- public function setType($type = null)
+ public function getDefaultType()
+ {
+ return $this->calculateTypeValue($this->defaultType);
+ }
+
+ /**
+ * @param array|int|string $type
+ * @return int
+ */
+ protected function calculateTypeValue($type)
{
if (is_array($type)) {
$detected = 0;
@@ -139,11 +159,26 @@ public function setType($type = null)
$type = array_search($type, $this->constants);
}
+ return $type;
+ }
+
+ /**
+ * Set the types
+ *
+ * @param int|array $type
+ * @throws Exception\InvalidArgumentException
+ * @return NotEmpty
+ */
+ public function setType($type = null)
+ {
+ $type = $this->calculateTypeValue($type);
+
if (!is_int($type) || ($type < 0) || ($type > self::ALL)) {
throw new Exception\InvalidArgumentException('Unknown type');
}
$this->options['type'] = $type;
+
return $this;
}
View
18 tests/ZendTest/Validator/NotEmptyTest.php
@@ -483,7 +483,7 @@ public function testSettingFalseType()
*/
public function testGetType()
{
- $this->assertEquals(493, $this->validator->getType());
+ $this->assertEquals($this->validator->getDefaultType(), $this->validator->getType());
}
/**
@@ -593,7 +593,21 @@ public function testEqualsMessageTemplates()
public function testTypeAutoDetectionHasNoSideEffect()
{
$validator = new NotEmpty(array('translatorEnabled' => true));
- $this->assertEquals(493, $validator->getType());
+ $this->assertEquals($validator->getDefaultType(), $validator->getType());
+ }
+
+ public function testDefaultType()
+ {
+ $this->assertSame(
+ NotEmpty::BOOLEAN
+ | NotEmpty::FLOAT
+ | NotEmpty::STRING
+ | NotEmpty::EMPTY_ARRAY
+ | NotEmpty::NULL
+ | NotEmpty::SPACE
+ | NotEmpty::OBJECT,
+ $this->validator->getDefaultType()
+ );
}
}
Something went wrong with that request. Please try again.