1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* CallbackAnnotation.php |
5
|
|
|
* |
6
|
|
|
* Jaxon annotation for client side callbacks. |
7
|
|
|
* |
8
|
|
|
* @package jaxon-annotations |
9
|
|
|
* @author Thierry Feuzeu <[email protected]> |
10
|
|
|
* @copyright 2024 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_string; |
21
|
|
|
use function preg_match; |
22
|
|
|
use function preg_split; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Specifies the javascript object to be used as callback. |
26
|
|
|
* |
27
|
|
|
* @usage('class' => true, 'method'=>true, 'multiple'=>true, 'inherited'=>true) |
28
|
|
|
*/ |
29
|
|
|
class CallbackAnnotation extends AbstractAnnotation |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* The name of the javascript object |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $sJsObject; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @inheritDoc |
40
|
|
|
*/ |
41
|
|
|
public static function parseAnnotation($value) |
42
|
|
|
{ |
43
|
|
|
$aParams = preg_split('/[\s]+/', $value, 2); |
44
|
|
|
return count($aParams) === 1 ? ['name' => $aParams[0]] : ['name' => $aParams[0], 'extra' => $aParams[1]]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Validate a javascript class name |
49
|
|
|
* |
50
|
|
|
* @param string $sJsObject The class name |
51
|
|
|
* |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
|
|
public function validateObjectName(string $sJsObject): bool |
55
|
|
|
{ |
56
|
|
|
return (preg_match('/^([a-zA-Z][a-zA-Z0-9_]*)(\.[a-zA-Z][a-zA-Z0-9_]*)*$/', $sJsObject) > 0); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @inheritDoc |
61
|
|
|
* @throws AnnotationException |
62
|
|
|
*/ |
63
|
|
|
public function initAnnotation(array $properties) |
64
|
|
|
{ |
65
|
|
|
if(count($properties) !== 1 || !isset($properties['name']) || !is_string($properties['name'])) |
66
|
|
|
{ |
67
|
|
|
throw new AnnotationException('the @callback annotation requires a single string as property'); |
68
|
|
|
} |
69
|
|
|
if(!$this->validateObjectName($properties['name'])) |
70
|
|
|
{ |
71
|
|
|
throw new AnnotationException($properties['name'] . ' is not a valid "name" value for the @callback annotation'); |
72
|
|
|
} |
73
|
|
|
$this->sJsObject = $properties['name']; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritDoc |
78
|
|
|
*/ |
79
|
|
|
public function getName(): string |
80
|
|
|
{ |
81
|
|
|
return 'callback'; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @inheritDoc |
86
|
|
|
*/ |
87
|
|
|
public function getValue() |
88
|
|
|
{ |
89
|
|
|
if(is_array($this->xPrevValue)) |
90
|
|
|
{ |
91
|
|
|
$this->xPrevValue[] = $this->sJsObject; // Append the current value to the array |
92
|
|
|
return $this->xPrevValue; |
93
|
|
|
} |
94
|
|
|
return [$this->sJsObject]; // Return the current value in an array |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|