Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made Required::isValid more readable, added a test for an untested path in Validator #19

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
73 changes: 50 additions & 23 deletions src/Particle/Validator/Rule/Required.php
Expand Up @@ -109,6 +109,11 @@ public function shouldBreakChain()
*/
public function validate($value)
{
if (!$this->allowEmpty && strlen($value) === 0) {
$this->shouldBreak = true;
return $this->error(self::EMPTY_VALUE);
}

return true;
}

Expand All @@ -121,32 +126,14 @@ public function validate($value)
*/
public function isValid($key, Container $input)
{
$array = $input->getArrayCopy();

if (isset($this->requiredCallback)) {
$this->required = call_user_func_array($this->requiredCallback, [$array]);
}

if (isset($this->allowEmptyCallback)) {
$this->allowEmpty = call_user_func_array($this->allowEmptyCallback, [$array]);
}

if ($this->required && !$input->has($key)) {
$this->shouldBreak = true;
return $this->error(self::NON_EXISTENT_KEY);
}

if (!$this->required && !$input->has($key)) {
$this->shouldBreak = true;
return true;
}
$this->required = $this->isRequired($input);
$this->allowEmpty = $this->hasAllowEmpty($input);

if (!$this->allowEmpty && strlen($input->get($key)) === 0) {
$this->shouldBreak = true;
return $this->error(self::EMPTY_VALUE);
if (!$input->has($key)) {
return $this->checkRequired();
}

return true;
return $this->validate($input->get($key));
}

/**
Expand Down Expand Up @@ -176,4 +163,44 @@ public function setAllowEmpty(callable $allowEmpty)
$this->allowEmptyCallback = $allowEmpty;
return $this;
}

/**
* Determines if the value is required.
*
* @param Container $input
* @return bool
*/
protected function isRequired(Container $input)
{
if (isset($this->requiredCallback)) {
$this->required = call_user_func_array($this->requiredCallback, [$input->getArrayCopy()]);
}
return $this->required;
}

/**
* Determines if the value may be empty.
*
* @param Container $input
* @return bool
*/
public function hasAllowEmpty(Container $input)
{
if (isset($this->allowEmptyCallback)) {
$this->allowEmpty = call_user_func_array($this->allowEmptyCallback, [$input->getArrayCopy()]);
}
return $this->allowEmpty;
}

/**
* Returns an error on a required value, and true on an optional value.
*
* @return bool
*/
protected function checkRequired()
{
$this->shouldBreak = true;

return $this->required ? $this->error(self::NON_EXISTENT_KEY) : true;
}
}
7 changes: 7 additions & 0 deletions src/Particle/Validator/Validator.php
Expand Up @@ -52,6 +52,13 @@ class Validator
*/
protected $output;

/**
* Contains the name of the current context.
*
* @var string
*/
protected $context;

/**
* Construct the validator.
*/
Expand Down
6 changes: 6 additions & 0 deletions tests/Particle/Validator/ValidatorTest.php
Expand Up @@ -256,4 +256,10 @@ public function testReturnsValidatedValues()

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

public function testReturnsEmptyArrayInsteadOfValidatedValues()
{
$this->validator->required('first_name')->lengthBetween(2, 20);
$this->assertEquals([], $this->validator->getValues());
}
}