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

FEATURE: Support objects that implement __toString in string propType #14

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Classes/Validators/PropTypeValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Neos\Eel\ProtectedContextAwareInterface;
use Neos\Flow\Validation\Validator\ConjunctionValidator;
use Neos\Flow\Validation\Validator\DisjunctionValidator;
use Neos\Flow\Validation\Validator\StringValidator;
use Neos\Flow\Validation\Validator\RegularExpressionValidator;
use Neos\Flow\Validation\Validator\NotEmptyValidator;
use Neos\Flow\Validation\Validator\ValidatorInterface;
Expand Down
27 changes: 27 additions & 0 deletions Classes/Validators/StringValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
namespace PackageFactory\AtomicFusion\PropTypes\Validators;

use Neos\Flow\Annotations as Flow;
use Neos\Flow\Validation\Validator\AbstractValidator;

/**
* Validator for strings.
*/
class StringValidator extends AbstractValidator
{
protected $acceptsEmptyValues = false;

/**
* Checks if the given value is a string or an object that implements __toString.
*
* @param mixed $value The value that should be validated
* @return void
*/
protected function isValid($value)
{
if (is_null($value) || is_string($value) || (is_object($value) && method_exists($value, '__toString'))) {
return;
}
$this->addError('A string or an object implementing __toString is expected.', 1617809129);
}
}
71 changes: 71 additions & 0 deletions Tests/Unit/Validators/StringValidatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
namespace PackageFactory\AtomicFusion\PropTypes\Tests\Unit\Validators;

use Neos\Flow\Tests\Unit\Validation\Validator\AbstractValidatorTestcase;
use PackageFactory\AtomicFusion\PropTypes\Validators\BooleanValidator;
use PackageFactory\AtomicFusion\PropTypes\Validators\StringValidator;

/**
* Testcase for the boolean validator
*
*/
class StringValidatorTest extends AbstractValidatorTestcase
{
protected $validatorClassName = StringValidator::class;

/**
* Data provider with valid examples
*
* @return array
*/
public function validExamples()
{

return [
[null],
[""],
["hello world"],
[new class() {
public function __toString()
{
return "hello world";
}
}]
];
}

/**
* @test
* @dataProvider validExamples
*/
public function validatorReturnsNoErrorsForValidExamples($value)
{
$this->assertFalse($this->validator->validate($value)->hasErrors());
}

/**
* Data provider with invalid examples
*
* @return array
*/
public function invalidExamples()
{
return [
[123],
[false],
[true],
[[]],
[[1,2,3]],
[new class() {}]
];
}

/**
* @test
* @dataProvider invalidExamples
*/
public function validatorReturnsErrorsForInValidExamples($value)
{
$this->assertTrue($this->validator->validate($value)->hasErrors());
}
}