1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* DataBagAnnotation.php |
5
|
|
|
* |
6
|
|
|
* Jaxon annotation for data bags. |
7
|
|
|
* |
8
|
|
|
* @package jaxon-annotations |
9
|
|
|
* @author Thierry Feuzeu <[email protected]> |
10
|
|
|
* @copyright 2022 Thierry Feuzeu <[email protected]> |
11
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
12
|
|
|
* @link https://github.com/jaxon-php/jaxon-annotations |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Jaxon\Annotations\Annotation; |
16
|
|
|
|
17
|
|
|
use mindplay\annotations\AnnotationException; |
18
|
|
|
|
19
|
|
|
use function count; |
20
|
|
|
use function is_array; |
21
|
|
|
use function is_string; |
22
|
|
|
use function preg_match; |
23
|
|
|
use function preg_split; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Specifies a data bag stored in the browser and included in ajax requests to a method. |
27
|
|
|
* |
28
|
|
|
* @usage('class' => true, 'method'=>true, 'multiple'=>true, 'inherited'=>true) |
29
|
|
|
*/ |
30
|
|
|
class DataBagAnnotation extends AbstractAnnotation |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* The data bag name |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $sName = ''; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @inheritDoc |
41
|
|
|
*/ |
42
|
|
|
public static function parseAnnotation($value) |
43
|
|
|
{ |
44
|
|
|
$aParams = preg_split('/[\s]+/', $value, 2); |
45
|
|
|
return count($aParams) === 1 ? ['name' => $aParams[0]] : ['name' => $aParams[0], 'extra' => $aParams[1]]; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $sDataBagName |
50
|
|
|
* |
51
|
|
|
* @return bool |
52
|
|
|
*/ |
53
|
|
|
protected function validateDataBagName(string $sDataBagName): bool |
54
|
|
|
{ |
55
|
|
|
return preg_match('/^[a-zA-Z][a-zA-Z0-9_\-\.]*$/', $sDataBagName) > 0; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @inheritDoc |
60
|
|
|
* @throws AnnotationException |
61
|
|
|
*/ |
62
|
|
|
public function initAnnotation(array $properties) |
63
|
|
|
{ |
64
|
|
|
if(count($properties) !== 1 || !isset($properties['name']) || !is_string($properties['name'])) |
65
|
|
|
{ |
66
|
|
|
throw new AnnotationException('The @databag annotation requires a property "name" of type string'); |
67
|
|
|
} |
68
|
|
|
if(!$this->validateDataBagName($properties['name'])) |
69
|
|
|
{ |
70
|
|
|
throw new AnnotationException($properties['name'] . ' is not a valid "name" value for the @databag annotation'); |
71
|
|
|
} |
72
|
|
|
$this->sName = $properties['name']; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritDoc |
77
|
|
|
*/ |
78
|
|
|
public function getName(): string |
79
|
|
|
{ |
80
|
|
|
return 'bags'; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @inheritDoc |
85
|
|
|
*/ |
86
|
|
|
public function getValue() |
87
|
|
|
{ |
88
|
|
|
if(is_array($this->xPrevValue)) |
89
|
|
|
{ |
90
|
|
|
$this->xPrevValue[] = $this->sName; // Append the current value to the array |
91
|
|
|
return $this->xPrevValue; |
92
|
|
|
} |
93
|
|
|
return [$this->sName]; // Return the current value in an array |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|