Skip to content
Permalink
Browse files

Add Validation::isScalar()

  • Loading branch information...
chinpei215 committed Jul 26, 2017
1 parent 0057bea commit 21415a29f0538074ac5ccee73d1d312c83148c0f
@@ -1423,6 +1423,17 @@ public static function isArray($value)
return is_array($value);
}
/**
* Check that the input value is a scalar.
*
* @param mixed $value The value to check
* @return bool
*/
public static function isScalar($value)
{
return is_scalar($value);
}
/**
* Check that the input value is a 6 digits hex color.
*
@@ -1618,6 +1618,24 @@ public function isArray($field, $message = null, $when = null)
]);
}
/**
* Add a validation rule to ensure that a field contains a scalar.
*
* @param string $field The field you want to apply the rule to.
* @param string|null $message The error message when the rule fails.
* @param string|callable|null $when Either 'create' or 'update' or a callable that returns
* true when the validation rule should be applied.
* @return $this
*/
public function isScalar($field, $message = null, $when = null)
{
$extra = array_filter(['on' => $when, 'message' => $message]);
return $this->add($field, 'isScalar', $extra + [
'rule' => 'isScalar'
]);
}
/**
* Add a validation rule to ensure a field is a 6 digits hex color value.
*
@@ -2878,6 +2878,18 @@ public function testIsArray()
$this->assertFalse(Validation::isArray(10));
}
/**
* Test isScalar
*
* @return void
*/
public function testIsScalar()
{
$this->assertTrue(Validation::isScalar(1));
$this->assertTrue(Validation::isScalar(''));
$this->assertFalse(Validation::isScalar([1]));
}
/**
* Test isInteger
*
@@ -1789,6 +1789,8 @@ public function testInteger()
/**
* Tests the isArray proxy method
*
* @return void
*/
public function testIsArray()
{
@@ -1798,6 +1800,19 @@ public function testIsArray()
$this->assertNotEmpty($validator->errors(['username' => 'is not an array']));
}
/**
* Tests the isScalar proxy method
*
* @return void
*/
public function testIsScalar()
{
$validator = new Validator();
$validator->isScalar('username');
$this->assertEmpty($validator->errors(['username' => 'scalar']));
$this->assertNotEmpty($validator->errors(['username' => ['array']]));
}
/**
* Tests the hexColor proxy method
*

0 comments on commit 21415a2

Please sign in to comment.
You can’t perform that action at this time.