Skip to content

Validators: added isListOfType() - #123

Closed
dstrop wants to merge 6 commits into
nette:masterfrom
dstrop:master
Closed

Validators: added isListOfType()#123
dstrop wants to merge 6 commits into
nette:masterfrom
dstrop:master

Conversation

@dstrop

@dstrop dstrop commented Nov 5, 2016

Copy link
Copy Markdown
Contributor

I would like to resolve #119.

@JanTvrdik

Copy link
Copy Markdown
Contributor

You either need to rename the method to isIterableOfType($value, $type) or check whether the value is a list (by calling self::isList($list))

@TomasVotruba

TomasVotruba commented Nov 6, 2016

Copy link
Copy Markdown

I would expect this method to support objects as well:

Validators::isIterableOfType($items, Product::class);

For inspiration:

  • in PHPUnit, there is assertInternalType for string/int/bools.
  • in beberlei/assert is allIsInstanceOf

@dstrop

dstrop commented Nov 6, 2016

Copy link
Copy Markdown
Contributor Author

I thought about it but decided the object should implement Travarsable if you want to iterate through its properties. As is in new php7.1 function is_iterable.

@JanTvrdik

JanTvrdik commented Nov 6, 2016

Copy link
Copy Markdown
Contributor

@TomasVotruba I think that the example you've posted should work just fine with the current implementation.

@dg

dg commented Nov 7, 2016

Copy link
Copy Markdown
Member

What about everyIs?

@TomasVotruba

Copy link
Copy Markdown

@JanTvrdik Ah, that is missing in tests, that suggest only scalar types are supported.

@t0his Could you please add test for simple object check? Sth like:

test(function () {
    Assert::true(Validators::isIterableOfType([new Product], Product::class));
    Assert::false(Validators::isIterableOfType([new Product, new stdClass], Product::class));
});

@dstrop

dstrop commented Nov 7, 2016

Copy link
Copy Markdown
Contributor Author

@TomasVotruba Sure i can add it, but shouldnt this be covered by is() tests?

@dg Ok i can rename it once i get home.

@dg

dg commented Nov 7, 2016

Copy link
Copy Markdown
Member

It was only a suggestion, hold on.

@TomasVotruba

TomasVotruba commented Nov 7, 2016

Copy link
Copy Markdown

@t0his Most part of the test is already covered by is(), but that's not the goal.
The test would be easier to understand, if both use cases - scalar and object - would be covered.

@dstrop

dstrop commented Nov 7, 2016

Copy link
Copy Markdown
Contributor Author

@TomasVotruba Ok would you like to add some other use case?

@JanTvrdik

Copy link
Copy Markdown
Contributor

Maybe extract method isIterable + support is($value, 'iterable')?

@dstrop

dstrop commented Nov 7, 2016

Copy link
Copy Markdown
Contributor Author

But the method will soon be redundant with php7.1.

@JanTvrdik

Copy link
Copy Markdown
Contributor

Yes, the method would use is_iterable and fallback to implementation in PHP in PHP < 7.1; probably best to make the method private to allow removal in future without breaking BC.

@TomasVotruba

Copy link
Copy Markdown

@t0his Nope, I guess that's all I would expect this method to do. Thank you.

@dg dg closed this in 8b2fd88 Dec 19, 2016
Comment thread src/Utils/Validators.php
*/
private static function isIterable($value)
{
if (function_exists('is_iterable')) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use simply return is_array($value) || $value instanceof \Traversable;, function_exists is unnecessary complication.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Validators: support for types int[] and string[]

4 participants