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

Commit a3f1f96

Browse files
committed
Merge pull request #58 from gsomoza/feature/uuid
Uuid Validator
2 parents 149e551 + ca0f167 commit a3f1f96

File tree

2 files changed

+166
-0
lines changed

2 files changed

+166
-0
lines changed

src/Uuid.php

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

test/UuidTest.php

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

0 commit comments

Comments
 (0)