1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SwaggerGen\Swagger\Type; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Abstract regular expression string definition |
7
|
|
|
* |
8
|
|
|
* @package SwaggerGen |
9
|
|
|
* @author Martijn van der Lee <[email protected]> |
10
|
|
|
* @copyright 2014-2016 Martijn van der Lee |
11
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
12
|
|
|
*/ |
13
|
|
|
class AbstractRegexType extends StringType |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
const REGEX_DEFAULT_START = '(?:=('; |
17
|
|
|
const REGEX_DEFAULT_END = '))?'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The raw regular expression to use. |
21
|
|
|
* Exclude start (`^`) and end (`$`) anchors. |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $regex = null; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Construct and setup the regular expression for this type |
28
|
|
|
* |
29
|
|
|
* @param \SwaggerGen\Swagger\AbstractObject $parent |
30
|
|
|
* @param string $definition |
31
|
|
|
* @param string $regex |
32
|
|
|
*/ |
33
|
|
|
public function __construct(\SwaggerGen\Swagger\AbstractObject $parent, $definition, $type, $regex) |
34
|
|
|
{ |
35
|
|
|
$this->type = $type; |
36
|
|
|
$this->regex = $regex; |
37
|
|
|
|
38
|
|
|
parent::__construct($parent, $definition); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected function parseDefinition($definition) |
42
|
|
|
{ |
43
|
|
|
$definition = self::trim($definition); |
44
|
|
|
|
45
|
|
|
$match = array(); |
46
|
|
|
if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT_START . $this->regex . self::REGEX_DEFAULT_END . self::REGEX_END, $definition, $match) !== 1) { |
47
|
|
|
throw new \SwaggerGen\Exception("Unparseable {$this->type} definition: '{$definition}'"); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if (strtolower($match[1] !== $this->type)) { |
51
|
|
|
throw new \SwaggerGen\Exception("Not a {$this->type}: '{$definition}'"); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$this->pattern = '^' . $this->regex . '$'; |
55
|
|
|
$this->default = isset($match[2]) && $match[2] !== '' ? $this->validateDefault($match[2]) : null; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function validateDefault($value) |
59
|
|
|
{ |
60
|
|
|
$value = parent::validateDefault($value); |
61
|
|
|
|
62
|
|
|
if (preg_match('/' . $this->pattern . '/', $value) !== 1) { |
63
|
|
|
throw new \SwaggerGen\Exception("Invalid {$this->type} default value"); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $value; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
} |
70
|
|
|
|