Skip to content

Commit

Permalink
Introduce InArrayValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
sukhwinder33445 committed Nov 17, 2022
1 parent 6a676c6 commit 8540f3d
Show file tree
Hide file tree
Showing 2 changed files with 217 additions and 0 deletions.
125 changes: 125 additions & 0 deletions src/InArrayValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

namespace ipl\Validator;

use ipl\I18n\Translation;

class InArrayValidator extends BaseValidator
{
use Translation;

/** @var array The array */
protected $haystack;

/** @var bool Whether the types of the needle in the haystack should also match */
protected $strict = false;

/**
* Validates whether the value exists in given haystack
*
* Optional options:
*
* - haystack: (array) The array
* - strict: (bool) Whether the types of the needle in the haystack should also match, default false
*
* @param array $options
*/
public function __construct(array $options = [])
{
if (isset($options['haystack'])) {
$this->setHaystack($options['haystack']);
}

$this->setStrict($options['strict'] ?? false);
}

/**
* Returns the haystack
*
* @return array
*/
public function getHaystack(): array
{
return $this->haystack ?? [];
}

/**
* Set the haystack
*
* @param array $haystack
*
* @return self
*/
public function setHaystack(array $haystack): self
{
$this->haystack = $haystack;

return $this;
}

/**
* Whether the types of the needle in the haystack should also match
*
* @return bool
*/
public function isStrict(): bool
{
return $this->strict;
}

/**
* Set whether the types of the needle in the haystack should also match
*
* @param bool $strict
*
* @return self
*/
public function setStrict(bool $strict = true): self
{
$this->strict = $strict;

return $this;
}

public function isValid($value)
{
// Multiple isValid() calls must not stack validation messages
$this->clearMessages();

$haystack = $this->getHaystack();
if (is_array($value)) {
$notInArrayValues = [];
foreach ($value as $val) {
if (! in_array($val, $haystack, $this->isStrict())) {
$notInArrayValues[] = $val;
}
}

if (empty($notInArrayValues)) {
return true;
}

$this->addMessage(sprintf(
$this->translatePlural(
"%s was not found in the haystack",
"%s were not found in the haystack",
count($notInArrayValues)
),
implode(', ', $notInArrayValues)
));

return false;
}

if (in_array($value, $haystack, $this->isStrict())) {
return true;
}

$this->addMessage(sprintf(
$this->translate("%s was not found in the haystack"),
$value
));

return false;
}
}
92 changes: 92 additions & 0 deletions tests/InArrayValidatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace ipl\Tests\Validator;

use ipl\I18n\NoopTranslator;
use ipl\I18n\StaticTranslator;
use ipl\Validator\InArrayValidator;

class InArrayValidatorTest extends TestCase
{
public function testWithValidValue()
{
StaticTranslator::$instance = new NoopTranslator();

$validator = new InArrayValidator([
'haystack' => ['test', 'foo', 'bar', 5, [], 0.008]
]);

$this->assertTrue($validator->isValid('foo'), 'foo was not found in the haystack');
$this->assertTrue($validator->isValid(5), '5 was not found in the haystack');
$this->assertTrue($validator->isValid(0.008), '00.8 was not found in the haystack');
$this->assertTrue($validator->isValid([]), '[] (empty array) was not found in the haystack');
}

public function testWithInvalidValue()
{
StaticTranslator::$instance = new NoopTranslator();

$validator = new InArrayValidator([
'haystack' => ['test', 'foo', 'bar', 5, [], 0.008]
]);

$this->assertFalse($validator->isValid('bear'), 'bear was found in the haystack');
$this->assertFalse($validator->isValid(60), '60 was found in the haystack');
$this->assertFalse($validator->isValid(0.09), '0.09 was found in the haystack');
}

public function testWithStrictOption()
{
StaticTranslator::$instance = new NoopTranslator();

$validator = new InArrayValidator([
'haystack' => ['test', 'foo', 'bar', 5, [], 0.008, '295'],
'strict' => true
]);

$this->assertTrue($validator->isValid('295'), '"295" was not found in the haystack');
$this->assertFalse($validator->isValid(295), '295 (int) was found in the haystack');
$this->assertFalse($validator->isValid('0.008'), '"0.008" was not found in the haystack');
}

public function testWithArrayAsValue()
{
StaticTranslator::$instance = new NoopTranslator();

$validator = new InArrayValidator([
'haystack' => ['test', 'foo', 'bar', 5, ['deep', ['deeper', '66']], 0.008, '295'],
]);

$this->assertFalse(
$validator->isValid(['car', 'cat', 55]),
'"car", "cat", 55 were found in the haystack'
);

$this->assertSame(['car, cat, 55 were not found in the haystack'], $validator->getMessages());

$this->assertTrue(
$validator->isValid(['test', '295', 0.008]),
'"test", "295", 0.008 were not found in the haystack'
);
}

public function testOptions()
{
StaticTranslator::$instance = new NoopTranslator();

$validator = new InArrayValidator([
'haystack' => ['test', 'foo', 'bar', 5, [], 0.008],
'strict' => true
]);

$this->assertTrue($validator->isStrict());

$validator->setStrict(false);
$this->assertFalse($validator->isStrict());

$this->assertSame(['test', 'foo', 'bar', 5, [], 0.008], $validator->getHaystack());

$validator->setHaystack([]);
$this->assertSame([], $validator->getHaystack());
}
}

0 comments on commit 8540f3d

Please sign in to comment.