Skip to content

Commit 6d1bb95

Browse files
authored
Merge pull request #9 from wol-soft/FormatValidation
Format validation
2 parents 7f9542b + eb74bed commit 6d1bb95

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace PHPModelGenerator\Exception\String;
6+
7+
use PHPModelGenerator\Exception\ValidationException;
8+
9+
/**
10+
* Class FormatException
11+
*
12+
* @package PHPModelGenerator\Exception\String
13+
*/
14+
class FormatException extends ValidationException
15+
{
16+
/** @var string */
17+
protected $expectedFormat;
18+
19+
/**
20+
* FormatException constructor.
21+
*
22+
* @param $providedValue
23+
* @param string $propertyName
24+
* @param string $expectedFormat
25+
*/
26+
public function __construct($providedValue, string $propertyName, string $expectedFormat)
27+
{
28+
$this->expectedFormat = $expectedFormat;
29+
30+
parent::__construct(
31+
"Value for $propertyName must match the format $expectedFormat",
32+
$propertyName,
33+
$providedValue
34+
);
35+
}
36+
37+
/**
38+
* @return string
39+
*/
40+
public function getExpectedFormat(): string
41+
{
42+
return $this->expectedFormat;
43+
}
44+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace PHPModelGenerator\Format;
6+
7+
/**
8+
* Class FormatFromRegEx
9+
*
10+
* @package PHPModelGenerator\Format
11+
*/
12+
class FormatValidatorFromRegEx implements FormatValidatorInterface
13+
{
14+
private $pattern;
15+
16+
public function __construct(string $pattern)
17+
{
18+
$this->pattern = $pattern;
19+
}
20+
21+
/**
22+
* @return string
23+
*/
24+
public function getPattern(): string
25+
{
26+
return $this->pattern;
27+
}
28+
29+
public static function validate(string $input, string $pattern = ''): bool
30+
{
31+
return preg_match($pattern, $input) === 1;
32+
}
33+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace PHPModelGenerator\Format;
6+
7+
interface FormatValidatorInterface
8+
{
9+
/**
10+
* Validate if the given $input is valid for the format
11+
*
12+
* @param string $input
13+
*
14+
* @return bool
15+
*/
16+
public static function validate(string $input): bool;
17+
}

0 commit comments

Comments
 (0)