Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

add IsCountable validator, to enforce type and count elements. #157

Closed
wants to merge 51 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
0ca7d66
Merge branch 'hotfix/150' into develop
Xerkus Mar 17, 2017
dfca59b
2.9.0 readiness
Xerkus Mar 17, 2017
0cfbeba
Add changelog for next maintainance version
Xerkus Mar 17, 2017
55e55e0
Bumped to next dev version (2.10.0)
Xerkus Mar 17, 2017
479f57c
add IsCountable validator, to enforce type and count elements.
abacaphiliac Mar 25, 2017
5ba3cdd
Merge branch 'hotfix/validators-service-config' into develop
weierophinney May 17, 2017
6bb4397
Merge branch 'hotfix/159' into develop
weierophinney May 17, 2017
9ec7f1a
Merge branch 'hotfix/94' into develop
weierophinney May 17, 2017
149f6af
Merge branch 'hotfix/105' into develop
weierophinney May 17, 2017
979bd6d
Merge branch 'hotfix/132' into develop
weierophinney May 17, 2017
b23cd62
Merge branch 'hotfix/154' into develop
weierophinney May 17, 2017
7b52198
Merge branch 'hotfix/162' into develop
weierophinney May 17, 2017
fd8c3ed
Merge branch 'hotfix/163' into develop
weierophinney May 17, 2017
687eb4a
CS fixes
weierophinney May 17, 2017
7c1ee5e
Merge branch 'hotfix/166' into develop
weierophinney May 17, 2017
6c60c41
Merge branch 'hotfix/code128' into develop
weierophinney May 17, 2017
af39b1a
Merge branch 'hotfix/update-tld' into develop
weierophinney May 17, 2017
609c866
2.9.1 readiness
weierophinney May 17, 2017
388400f
Bumped version
weierophinney May 17, 2017
33591b6
Fix #165 - Use is_readable() instead of stream_resolve_include_path()
larsnystrom May 23, 2017
9d26add
Adapt .travis.yml to recent ZF standards
Slamdunk Jun 23, 2017
92035a4
Travis: drop HVVM & add PHP 7.2
Slamdunk Jun 23, 2017
1a0717c
Prepare to PHP 7.2
Slamdunk Jun 23, 2017
407ee84
In PHP < 7.2 `0 === count(null)`
Slamdunk Jun 23, 2017
6500014
Merge branch 'hotfix/173' into develop
weierophinney Jul 19, 2017
bf7be0d
Merge branch 'hotfix/hostname-idn-update' into develop
weierophinney Jul 19, 2017
f012441
Merge branch 'hotfix/180' into develop
weierophinney Jul 19, 2017
d7b8d55
Merge branch 'hotfix/travis' into develop
weierophinney Jul 19, 2017
9a7c40c
Merge branch 'hotfix/174' into develop
weierophinney Jul 20, 2017
13f88a4
2.9.2 readiness
weierophinney Jul 20, 2017
58da575
Updated hostname validator TLD list
weierophinney Jul 20, 2017
83aa8e5
Merge pull request #169 from larsnystrom/hotfix/165
weierophinney Jul 25, 2017
4416265
Updates `@return` annotations for fluent interfaces
weierophinney Jul 25, 2017
ddcb7cd
Adds CHANGELOG for #169
weierophinney Jul 25, 2017
e44ce14
Merge branch 'feature/169' into develop
weierophinney Jul 25, 2017
7b3c9ac
Updated Hostname validator TLD list
weierophinney Jul 25, 2017
8d4989d
Merge branch 'hotfix/hostname-tld' into develop
weierophinney Jul 25, 2017
809093c
Merge pull request #175 from Slamdunk/php_72
weierophinney Jul 25, 2017
d0316a1
Provides CS fix for update_hostname_validator
weierophinney Jul 25, 2017
6120357
Provides formatting of complex conditional
weierophinney Jul 25, 2017
af974c9
Adds CHANGELOG for #175
weierophinney Jul 25, 2017
93601c7
Merge branch 'feature/175' into develop
weierophinney Jul 25, 2017
e7355ab
Updates zend-session requirement
weierophinney Jul 25, 2017
f527802
address upstream feedback: remove setters, prefer count over min and …
abacaphiliac Jul 26, 2017
9924700
Adds CHANGELOG for #185
weierophinney Jul 26, 2017
b83a91f
Merge branch 'feature/zend-session-php-7.2-compat' into develop
weierophinney Jul 26, 2017
1dc79e6
Merge pull request #157 from abacaphiliac/master
weierophinney Jul 26, 2017
74b4711
Adds license docblocks to new class files
weierophinney Jul 26, 2017
88831e1
Imports `Countable` into `IsCountable` class file
weierophinney Jul 26, 2017
97d297c
Updates IsCountable to not allow passing both a count and a min or ma…
weierophinney Jul 26, 2017
03c12fd
Adds documentation for #157
weierophinney Jul 26, 2017
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
12 changes: 6 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ branches:

cache:
directories:
- $HOME/.composer/cache
- $HOME/.composer/

env:
global:
Expand Down Expand Up @@ -50,17 +50,17 @@ matrix:
- php: 7.1
env:
- DEPS=latest
- php: hhvm
- php: nightly
env:
- DEPS=lowest
- php: hhvm
- php: nightly
env:
- DEPS=locked
- php: hhvm
- php: nightly
env:
- DEPS=latest
allow_failures:
- php: hhvm
- php: nightly

before_install:
- if [[ $TEST_COVERAGE != 'true' ]]; then phpenv config-rm xdebug.ini || return 0 ; fi
Expand All @@ -81,7 +81,7 @@ script:
- if [[ $EXECUTE_HOSTNAME_CHECK == "true" && $TRAVIS_PULL_REQUEST == "false" ]]; then php bin/update_hostname_validator.php --check-only; fi

after_script:
- if [[ $TEST_COVERAGE == 'true' ]]; then composer upload-coverage ; fi
- if [[ $TEST_COVERAGE == 'true' ]]; then travis_retry composer upload-coverage ; fi

notifications:
email: false
32 changes: 32 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,38 @@

All notable changes to this project will be documented in this file, in reverse chronological order by release.

## 2.10.0 - TBD

### Added

- [#175](https://github.com/zendframework/zend-validator/pull/175) adds support
for PHP 7.2 (conditionally, as PHP 7.2 is currently in beta1).

### Changed

- [#169](https://github.com/zendframework/zend-validator/pull/169) modifies how
the various `File` validators check for readable files. Previously, they used
`stream_resolve_include_path`, which led to false negative checks when the
files did not exist within an `include_path` (which is often the case within a
web application). These now use `is_readable()` instead.

- [#185](https://github.com/zendframework/zend-validator/pull/185) updates the
zend-session requirement (during development, and in the suggestions) to 2.8+,
to ensure compatibility with the upcoming PHP 7.2 release.

### Deprecated

- Nothing.

### Removed

- [#175](https://github.com/zendframework/zend-validator/pull/175) removes
support for HHVM.

### Fixed

- Nothing.

## 2.9.2 - 2017-07-20

### Added
Expand Down
4 changes: 3 additions & 1 deletion bin/update_hostname_validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,9 @@ function getNewValidTlds($string)
function getPunycodeDecoder()
{
if (function_exists('idn_to_utf8')) {
return 'idn_to_utf8';
return function ($domain) {
return idn_to_utf8($domain, 0, INTL_IDNA_VARIANT_UTS46);
};
}

$hostnameValidator = new Hostname();
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"zendframework/zend-i18n": "^2.6",
"zendframework/zend-math": "^2.6",
"zendframework/zend-servicemanager": "^2.7.5 || ^3.0.3",
"zendframework/zend-session": "^2.6.2",
"zendframework/zend-session": "^2.8",
"zendframework/zend-uri": "^2.5",
"phpunit/PHPUnit": "^6.0.8 || ^5.7.15",
"zendframework/zend-coding-standard": "~1.0.0"
Expand All @@ -38,7 +38,7 @@
"zendframework/zend-math": "Zend\\Math component, required by the Csrf validator",
"zendframework/zend-i18n-resources": "Translations of validator messages",
"zendframework/zend-servicemanager": "Zend\\ServiceManager component to allow using the ValidatorPluginManager and validator chains",
"zendframework/zend-session": "Zend\\Session component, required by the Csrf validator",
"zendframework/zend-session": "Zend\\Session component, ^2.8; required by the Csrf validator",
"zendframework/zend-uri": "Zend\\Uri component, required by the Uri and Sitemap\\Loc validators"
},
"minimum-stability": "dev",
Expand Down
22 changes: 11 additions & 11 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

107 changes: 107 additions & 0 deletions doc/book/validators/is-countable.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# IsCountable Validator

- **Since 2.10.0**

`Zend\Validator\IsCountable` allows you to validate that a value can be counted
(i.e., it's an array or implements `Countable`), and, optionally:

- the exact count of the value
- the minimum count of the value
- the maximum count of the value

Specifying either of the latter two is inconsistent with the first, and, as
such, the validator does not allow setting both a count and a minimum or maximum
value. You may, however specify both minimum and maximum values, in which case
the validator operates similar to the [Between validator](between.md).

## Supported options

The following options are supported for `Zend\Validator\IsCountable`:

- `count`: Defines if the validation should look for a specific, exact count for
the value provided.
- `max`: Sets the maximum value for the validation; if the count of the value is
greater than the maximum, validation fails..
- `min`: Sets the minimum value for the validation; if the count of the value is
lower than the minimum, validation fails.

## Default behaviour

Given no options, the validator simply tests to see that the value may be
counted (i.e., it's an array or `Countable` instance):

```php
$validator = new Zend\Validator\IsCountable();

$validator->isValid(10); // false; not an array or Countable
$validator->isValid([10]); // true; value is an array
$validator->isValid(new ArrayObject([10])); // true; value is Countable
$validator->isValid(new stdClass); // false; value is not Countable
```

## Specifying an exact count

You can also specify an exact count; if the value is countable, and its count
matches, the the value is valid.

```php
$validator = new Zend\Validator\IsCountable(['count' => 3]);

$validator->isValid([1, 2, 3]); // true; countable, and count is 3
$validator->isValid(new ArrayObject([1, 2, 3])); // true; countable, and count is 3
$validator->isValid([1]); // false; countable, but count is 1
$validator->isValid(new ArrayObject([1])); // false; countable, but count is 1
```

## Specifying a minimum count

You may specify a minimum count. When you do, the value must be countable, and
greater than or equal to the minimum count you specify in order to be valid.

```php
$validator = new Zend\Validator\IsCountable(['min' => 2]);

$validator->isValid([1, 2, 3]); // true; countable, and count is 3
$validator->isValid(new ArrayObject([1, 2, 3])); // true; countable, and count is 3
$validator->isValid([1, 2]); // true; countable, and count is 2
$validator->isValid(new ArrayObject([1, 2])); // true; countable, and count is 2
$validator->isValid([1]); // false; countable, but count is 1
$validator->isValid(new ArrayObject([1])); // false; countable, but count is 1
```

## Specifying a maximum count

You may specify a maximum count. When you do, the value must be countable, and
less than or equal to the maximum count you specify in order to be valid.

```php
$validator = new Zend\Validator\IsCountable(['max' => 2]);

$validator->isValid([1, 2, 3]); // false; countable, but count is 3
$validator->isValid(new ArrayObject([1, 2, 3])); // false; countable, but count is 3
$validator->isValid([1, 2]); // true; countable, and count is 2
$validator->isValid(new ArrayObject([1, 2])); // true; countable, and count is 2
$validator->isValid([1]); // true; countable, and count is 1
$validator->isValid(new ArrayObject([1])); // true; countable, and count is 1
```

## Specifying both minimum and maximum

If you specify both a minimum and maximum, the count must be _between_ the two,
inclusively (i.e., it may be the minimum or maximum, and any value between).

```php
$validator = new Zend\Validator\IsCountable([
'min' => 3,
'max' => 5,
]);

$validator->isValid([1, 2, 3]); // true; countable, and count is 3
$validator->isValid(new ArrayObject([1, 2, 3])); // true; countable, and count is 3
$validator->isValid(range(1, 5)); // true; countable, and count is 5
$validator->isValid(new ArrayObject(range(1, 5))); // true; countable, and count is 5
$validator->isValid([1, 2]); // false; countable, and count is 2
$validator->isValid(new ArrayObject([1, 2])); // false; countable, and count is 2
$validator->isValid(range(1, 6)); // false; countable, and count is 6
$validator->isValid(new ArrayObject(range(1, 6))); // false; countable, and count is 6
```
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pages:
- InArray: validators/in-array.md
- Ip: validators/ip.md
- Isbn: validators/isbn.md
- IsCountable: validators/is-countable.md
- IsInstanceOf: validators/isinstanceof.md
- LessThan: validators/less-than.md
- NotEmpty: validators/not-empty.md
Expand Down
4 changes: 2 additions & 2 deletions src/EmailAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ public function isValid($value)
protected function idnToAscii($email)
{
if (extension_loaded('intl')) {
return (idn_to_ascii($email) ?: $email);
return (idn_to_ascii($email, 0, INTL_IDNA_VARIANT_UTS46) ?: $email);
}
return $email;
}
Expand All @@ -553,7 +553,7 @@ protected function idnToUtf8($email)
// the source string in those cases.
// But not when the source string is long enough.
// Thus we default to source string ourselves.
return idn_to_utf8($email) ?: $email;
return idn_to_utf8($email, 0, INTL_IDNA_VARIANT_UTS46) ?: $email;
}
return $email;
}
Expand Down
8 changes: 4 additions & 4 deletions src/File/Crc32.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function getCrc32()
* Sets the crc32 hash for one or multiple files
*
* @param string|array $options
* @return Crc32 Provides a fluent interface
* @return self Provides a fluent interface
*/
public function setCrc32($options)
{
Expand All @@ -68,7 +68,7 @@ public function setCrc32($options)
* Adds the crc32 hash for one or multiple files
*
* @param string|array $options
* @return Crc32 Provides a fluent interface
* @return self Provides a fluent interface
*/
public function addCrc32($options)
{
Expand Down Expand Up @@ -104,12 +104,12 @@ public function isValid($value, $file = null)
$this->setValue($filename);

// Is file readable ?
if (empty($file) || false === stream_resolve_include_path($file)) {
if (empty($file) || false === is_readable($file)) {
$this->error(self::NOT_FOUND);
return false;
}

$hashes = array_unique(array_keys($this->getHash()));
$hashes = array_unique(array_keys($this->getHash()));
$filehash = hash_file('crc32', $file);
if ($filehash === false) {
$this->error(self::NOT_DETECTED);
Expand Down
2 changes: 1 addition & 1 deletion src/File/ExcludeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function isValid($value, $file = null)
$this->setValue($filename);

// Is file readable ?
if (empty($file) || false === stream_resolve_include_path($file)) {
if (empty($file) || false === is_readable($file)) {
$this->error(self::NOT_FOUND);
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/File/ExcludeMimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function isValid($value, $file = null)
$this->setValue($filename);

// Is file readable ?
if (empty($file) || false === stream_resolve_include_path($file)) {
if (empty($file) || false === is_readable($file)) {
$this->error(self::NOT_READABLE);
return false;
}
Expand Down
8 changes: 4 additions & 4 deletions src/File/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function getCase()
* Sets the case to use
*
* @param bool $case
* @return Extension Provides a fluent interface
* @return self Provides a fluent interface
*/
public function setCase($case)
{
Expand All @@ -124,7 +124,7 @@ public function getExtension()
* Sets the file extensions
*
* @param string|array $extension The extensions to validate
* @return Extension Provides a fluent interface
* @return self Provides a fluent interface
*/
public function setExtension($extension)
{
Expand All @@ -137,7 +137,7 @@ public function setExtension($extension)
* Adds the file extensions
*
* @param string|array $extension The extensions to add for validation
* @return Extension Provides a fluent interface
* @return self Provides a fluent interface
*/
public function addExtension($extension)
{
Expand Down Expand Up @@ -196,7 +196,7 @@ public function isValid($value, $file = null)
$this->setValue($filename);

// Is file readable ?
if (empty($file) || false === stream_resolve_include_path($file)) {
if (empty($file) || false === is_readable($file)) {
$this->error(self::NOT_FOUND);
return false;
}
Expand Down
Loading