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

Commit e52334e

Browse files
committed
Merging develop to master in preparation for 2.8.0
2 parents d76eabd + 319e89b commit e52334e

File tree

8 files changed

+246
-1
lines changed

8 files changed

+246
-1
lines changed

CHANGELOG.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,31 @@
22

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

5+
## 2.8.0 - TBD
6+
7+
### Added
8+
9+
- [#58](https://github.com/zendframework/zend-validator/pull/58) adds a new
10+
`Uuid` validator, capable of validating if Versions 1-5 UUIDs are well-formed.
11+
- [#64](https://github.com/zendframework/zend-validator/pull/64) ports
12+
`Zend\ModuleManager\Feature\ValidatorProviderInterface` to
13+
`Zend\Validator\ValidatorProviderInterface`, and updates the `Module::init()`
14+
to typehint against the new interface instead of the one from
15+
zend-modulemanager. Applications targeting zend-mvc v3 can start updating
16+
their code to implement the new interface, or simply duck-type against it.
17+
18+
### Deprecated
19+
20+
- Nothing.
21+
22+
### Removed
23+
24+
- Nothing.
25+
26+
### Fixed
27+
28+
- Nothing.
29+
530
## 2.7.3 - 2016-05-16
631

732
### Added

doc/book/set.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ The following validators come with the zend-validator distribution.
2929
- [StringLength](validators/string-length.md)
3030
- [Timezone](validators/timezone.md)
3131
- [Uri](validators/uri.md)
32+
- [Uuid](validators/uuid.md)
3233

3334
## Additional validators
3435

doc/book/validators/uuid.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# UUID Validator
2+
3+
`Zend\Validator\Uuid` allows validating [Universally Unique IDentifiers](https://en.wikipedia.org/wiki/Universally_unique_identifier)
4+
(UUIDs). UUIDs are 128-bit values that are guaranteed to be "practically unique"
5+
in order to help prevent identifier conflicts. Five separate UUID versions
6+
exist:
7+
8+
- Version 1, which uses a combination of date-time and hardware MAC addresses to
9+
generate the hash.
10+
- Version 2, which uses a combination of date-time and system user/group identifiers.
11+
- Version 3, which uses an MD5sum of a URI or distinguished name to generate the
12+
hash.
13+
- Version 4, which uses a CSPRNG to generate the hash.
14+
- Version 5, which uses the same idea as Version 3, but using SHA-1 for hashing.
15+
16+
The `Uuid` validator is capable of validating whether a string is a valid UUID
17+
of any version. It does not validate that the UUID exists in your system,
18+
however, only that it is well-formed.
19+
20+
> ### Introduced in 2.8.0
21+
>
22+
> `Zend\Validator\Uuid` was introduced with version 2.8.0.
23+
24+
## Supported options
25+
26+
The `Uuid` validator has no additional options.
27+
28+
## Basic usage
29+
30+
```php
31+
$validator = new Zend\Validator\Uuid();
32+
33+
if ($validator->isValid($uuid)) {
34+
// UUID was valid
35+
} else {
36+
// Invalid/mal-formed UUID; use $validator->getMessages() for more detail
37+
}
38+
```

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pages:
3535
- StringLength: validators/string-length.md
3636
- Timezone: validators/timezone.md
3737
- Uri: validators/uri.md
38+
- Uuid: validators/uuid.md
3839
- "File Validators":
3940
- Intro: validators/file/intro.md
4041
- Count: validators/file/count.md

src/Module.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function init($moduleManager)
3636
$serviceListener->addServiceManager(
3737
'ValidatorManager',
3838
'validators',
39-
'Zend\ModuleManager\Feature\ValidatorProviderInterface',
39+
ValidatorProviderInterface::class,
4040
'getValidatorConfig'
4141
);
4242
}

src/Uuid.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* @link http://github.com/zendframework/zend-validator for the canonical source repository
4+
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
5+
* @license http://framework.zend.com/license/new-bsd New BSD License
6+
*/
7+
8+
namespace Zend\Validator;
9+
10+
/**
11+
* Uuid validator.
12+
*/
13+
final class Uuid extends AbstractValidator
14+
{
15+
/**
16+
* Matches Uuid's versions 1 to 5.
17+
*/
18+
const REGEX_UUID = '/^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$/';
19+
20+
const INVALID = 'valueNotUuid';
21+
const NOT_STRING = 'valueNotString';
22+
23+
/**
24+
* @var array
25+
*/
26+
protected $messageTemplates = [
27+
self::NOT_STRING => 'Invalid type given; string expected',
28+
self::INVALID => 'Invalid UUID format',
29+
];
30+
31+
/**
32+
* Returns true if and only if $value meets the validation requirements.
33+
*
34+
* If $value fails validation, then this method returns false, and
35+
* getMessages() will return an array of messages that explain why the
36+
* validation failed.
37+
*
38+
* @param mixed $value
39+
*
40+
* @return bool
41+
*
42+
* @throws Exception\RuntimeException If validation of $value is impossible
43+
*/
44+
public function isValid($value)
45+
{
46+
if (! is_string($value)) {
47+
$this->error(self::NOT_STRING);
48+
return false;
49+
}
50+
51+
$this->setValue($value);
52+
53+
if (empty($value)
54+
|| $value !== '00000000-0000-0000-0000-000000000000'
55+
&& ! preg_match(self::REGEX_UUID, $value)
56+
) {
57+
$this->error(self::INVALID);
58+
return false;
59+
}
60+
61+
return true;
62+
}
63+
}

src/ValidatorProviderInterface.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* @link http://github.com/zendframework/zend-validator for the canonical source repository
4+
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
5+
* @license http://framework.zend.com/license/new-bsd New BSD License
6+
*/
7+
8+
namespace Zend\Validator;
9+
10+
/**
11+
* Hint to the zend-modulemanager ServiceListener that a module provides validators.
12+
*
13+
* Module classes implementing this interface hint to
14+
* Zend\ModuleManager\ServiceListener that they provide validators for the
15+
* ValidatorPluginManager.
16+
*
17+
* For users of zend-mvc/zend-modulemanager v2, this poses no backwards-compatibility
18+
* break as the method getValidatorConfig is still duck-typed within Zend\Validator\Module
19+
* when providing configuration to the ServiceListener.
20+
*/
21+
interface ValidatorProviderInterface
22+
{
23+
/**
24+
* Provide plugin manager configuration for validators.
25+
*
26+
* @return array
27+
*/
28+
public function getValidatorConfig();
29+
}

test/UuidTest.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
/**
3+
* @link http://github.com/zendframework/zend-validator for the canonical source repository
4+
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
5+
* @license http://framework.zend.com/license/new-bsd New BSD License
6+
*/
7+
8+
namespace ZendTest\Validator;
9+
10+
use PHPUnit_Framework_TestCase as TestCase;
11+
use stdClass;
12+
use Zend\Validator\Uuid;
13+
14+
/**
15+
* Class UuidTest.
16+
*
17+
* Uuid test cases based on https://github.com/beberlei/assert/blob/master/tests/Assert/Tests/AssertTest.php
18+
*/
19+
final class UuidTest extends TestCase
20+
{
21+
/**
22+
* @var Uuid
23+
*/
24+
protected $validator;
25+
26+
public function setUp()
27+
{
28+
$this->validator = new Uuid();
29+
}
30+
31+
/**
32+
* @param $uuid
33+
* @dataProvider validUuidProvider
34+
*/
35+
public function testValidUuid($uuid)
36+
{
37+
$this->assertTrue($this->validator->isValid($uuid));
38+
$messages = $this->validator->getMessages();
39+
$this->assertCount(0, $messages);
40+
}
41+
42+
/**
43+
* @param $uuid
44+
* @dataProvider invalidUuidProvider
45+
*/
46+
public function testInvalidUuid($uuid, $expectedMessageKey)
47+
{
48+
$this->assertFalse($this->validator->isValid($uuid));
49+
$messages = $this->validator->getMessages();
50+
$this->assertCount(1, $messages);
51+
$this->assertArrayHasKey($expectedMessageKey, $messages);
52+
$this->assertNotEmpty($messages[$expectedMessageKey]);
53+
}
54+
55+
/**
56+
* @return array
57+
*/
58+
public function validUuidProvider()
59+
{
60+
return [
61+
'zero-fill' => ['00000000-0000-0000-0000-000000000000'],
62+
'version-1' => ['ff6f8cb0-c57d-11e1-9b21-0800200c9a66'],
63+
'version-2' => ['ff6f8cb0-c57d-21e1-9b21-0800200c9a66'],
64+
'version-3' => ['ff6f8cb0-c57d-31e1-9b21-0800200c9a66'],
65+
'version-4' => ['ff6f8cb0-c57d-41e1-9b21-0800200c9a66'],
66+
'version-5' => ['ff6f8cb0-c57d-51e1-9b21-0800200c9a66'],
67+
'uppercase' => ['FF6F8CB0-C57D-11E1-9B21-0800200C9A66'],
68+
];
69+
}
70+
71+
/**
72+
* @return array
73+
*/
74+
public function invalidUuidProvider()
75+
{
76+
return [
77+
'invalid-characters' => ['zf6f8cb0-c57d-11e1-9b21-0800200c9a66', Uuid::INVALID],
78+
'missing-separators' => ['af6f8cb0c57d11e19b210800200c9a66', Uuid::INVALID],
79+
'invalid-segment-2' => ['ff6f8cb0-c57da-51e1-9b21-0800200c9a66', Uuid::INVALID],
80+
'invalid-segment-1' => ['af6f8cb-c57d-11e1-9b21-0800200c9a66', Uuid::INVALID],
81+
'invalid-segement-5' => ['3f6f8cb0-c57d-11e1-9b21-0800200c9a6', Uuid::INVALID],
82+
'truncated' => ['3f6f8cb0', Uuid::INVALID],
83+
'empty-string' => ['', Uuid::INVALID],
84+
'all-numeric' => [123, Uuid::NOT_STRING],
85+
'object' => [new stdClass(), Uuid::NOT_STRING],
86+
];
87+
}
88+
}

0 commit comments

Comments
 (0)