1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpUnitGen\Annotation; |
4
|
|
|
|
5
|
|
|
use PhpUnitGen\Annotation\AnnotationInterface\AnnotationInterface; |
6
|
|
|
use PhpUnitGen\Exception\AnnotationParseException; |
7
|
|
|
use PhpUnitGen\Exception\JsonException; |
8
|
|
|
use PhpUnitGen\Util\Json; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class SetAnnotation. |
12
|
|
|
* |
13
|
|
|
* @author Paul Thébaud <[email protected]>. |
14
|
|
|
* @copyright 2017-2018 Paul Thébaud <[email protected]>. |
15
|
|
|
* @license https://opensource.org/licenses/MIT The MIT license. |
16
|
|
|
* @link https://github.com/paul-thebaud/phpunit-generator |
17
|
|
|
* @since Class available since Release 2.0.0. |
18
|
|
|
*/ |
19
|
|
|
class SetAnnotation extends AbstractAnnotation |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var string $property The name of the property to get. |
23
|
|
|
*/ |
24
|
|
|
private $property; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
|
|
public function getType(): int |
30
|
|
|
{ |
31
|
|
|
return AnnotationInterface::TYPE_SET; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
public function compile(): void |
38
|
|
|
{ |
39
|
|
|
if (strlen($this->getStringContent()) > 0) { |
40
|
|
|
// Decode JSON content |
41
|
|
|
try { |
42
|
|
|
$decoded = Json::decode($this->getStringContent()); |
43
|
|
|
} catch (JsonException $exception) { |
44
|
|
|
throw new AnnotationParseException('"setter" annotation content is invalid (invalid JSON content)'); |
45
|
|
|
} |
46
|
|
|
if (! is_string($decoded)) { |
47
|
|
|
throw new AnnotationParseException( |
48
|
|
|
'"setter" annotation content is invalid (property name must be a string)' |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
$this->property = $decoded; |
52
|
|
|
} else { |
53
|
|
|
$this->property = preg_replace('/^set/', '', $this->getParentNode()->getName()); |
|
|
|
|
54
|
|
|
$this->property = lcfirst($this->property); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return string The name of the property to get. |
60
|
|
|
*/ |
61
|
|
|
public function getProperty(): string |
62
|
|
|
{ |
63
|
|
|
return $this->property; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|