|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Swaggest\PhpCodeBuilder\Property; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use Swaggest\CodeBuilder\PlaceholderString; |
|
7
|
|
|
use Swaggest\JsonSchema\Exception\StringException; |
|
8
|
|
|
use Swaggest\JsonSchema\Helper; |
|
9
|
|
|
use Swaggest\JsonSchema\InvalidValue; |
|
10
|
|
|
use Swaggest\PhpCodeBuilder\PhpAnyType; |
|
11
|
|
|
use Swaggest\PhpCodeBuilder\PhpClass; |
|
12
|
|
|
use Swaggest\PhpCodeBuilder\PhpCode; |
|
13
|
|
|
use Swaggest\PhpCodeBuilder\PhpConstant; |
|
14
|
|
|
use Swaggest\PhpCodeBuilder\PhpFlags; |
|
15
|
|
|
use Swaggest\PhpCodeBuilder\PhpFunction; |
|
16
|
|
|
use Swaggest\PhpCodeBuilder\PhpNamedVar; |
|
17
|
|
|
use Swaggest\PhpCodeBuilder\PhpStdType; |
|
18
|
|
|
use Swaggest\PhpCodeBuilder\Types\TypeOf; |
|
19
|
|
|
|
|
20
|
|
|
class PatternPropertySetter extends PhpFunction |
|
21
|
|
|
{ |
|
22
|
4 |
|
public function __construct(PhpConstant $patternConst, PhpAnyType $type) |
|
23
|
|
|
{ |
|
24
|
4 |
|
$name = PhpCode::makePhpName($patternConst->getValue(), false); |
|
25
|
4 |
|
parent::__construct('set' . $name . 'Value', PhpFlags::VIS_PUBLIC); |
|
26
|
|
|
|
|
27
|
4 |
|
$this->addArgument(new PhpNamedVar('name', PhpStdType::string())); |
|
28
|
4 |
|
$this->addArgument(new PhpNamedVar('value', $type)); |
|
29
|
|
|
|
|
30
|
4 |
|
$this->skipCodeCoverage = true; |
|
31
|
4 |
|
$this->addThrows(PhpClass::byFQN(InvalidValue::class)); |
|
32
|
|
|
|
|
33
|
4 |
|
$this->setResult(PhpStdType::tSelf()); |
|
34
|
4 |
|
$this->setBody( |
|
35
|
4 |
|
new PlaceholderString(<<<PHP |
|
36
|
4 |
|
if (preg_match(:helper::toPregPattern(self::{$patternConst->getName()}), \$name)) { |
|
37
|
|
|
throw new StringException('Pattern mismatch', :stringException::PATTERN_MISMATCH); |
|
38
|
|
|
} |
|
39
|
4 |
|
\$this->addPatternPropertyName(self::{$patternConst->getName()}, \$name); |
|
40
|
|
|
\$this->{\$name} = \$value; |
|
41
|
|
|
return \$this; |
|
42
|
|
|
|
|
43
|
|
|
PHP |
|
44
|
|
|
, |
|
45
|
|
|
array( |
|
46
|
4 |
|
':stringException' => new TypeOf(PhpClass::byFQN(StringException::class)), |
|
47
|
4 |
|
':helper' => new TypeOf(PhpClass::byFQN(Helper::class)), |
|
48
|
|
|
)) |
|
49
|
|
|
); |
|
50
|
|
|
} |
|
51
|
|
|
} |