1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* BeforeAnnotation.php |
5
|
|
|
* |
6
|
|
|
* Jaxon annotation for callbacks. |
7
|
|
|
* |
8
|
|
|
* @package jaxon-core |
|
|
|
|
9
|
|
|
* @copyright 2022 Thierry Feuzeu <[email protected]> |
10
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
11
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
12
|
|
|
*/ |
|
|
|
|
13
|
|
|
|
14
|
|
|
namespace Jaxon\Annotations\Annotation; |
15
|
|
|
|
16
|
|
|
use mindplay\annotations\AnnotationException; |
17
|
|
|
|
18
|
|
|
use function array_keys; |
19
|
|
|
use function is_array; |
20
|
|
|
use function is_string; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Specifies a method to be called before the one targeted by a Jaxon request. |
24
|
|
|
* |
25
|
|
|
* @usage('class' => true, 'method'=>true, 'multiple'=>true, 'inherited'=>true) |
26
|
|
|
*/ |
|
|
|
|
27
|
|
|
class BeforeAnnotation extends AbstractAnnotation |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $sMethodName = ''; |
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
protected $sMethodParams = []; |
38
|
|
|
|
39
|
|
|
/** |
|
|
|
|
40
|
|
|
* @inheritDoc |
41
|
|
|
* @throws AnnotationException |
42
|
|
|
*/ |
|
|
|
|
43
|
|
|
public function initAnnotation(array $properties) |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
if(!isset($properties['call']) || !is_string($properties['call'])) |
46
|
|
|
{ |
47
|
|
|
throw new AnnotationException('The @before annotation requires a property "call" of type string'); |
48
|
|
|
} |
49
|
|
|
foreach(array_keys($properties) as $propName) |
50
|
|
|
{ |
51
|
|
|
if($propName !== 'call' && $propName !== 'with') |
52
|
|
|
{ |
53
|
|
|
throw new AnnotationException('Unknown property "' . $propName . '" in the @before annotation'); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
if(isset($properties['with'])) |
57
|
|
|
{ |
58
|
|
|
if(!is_array($properties['with'])) |
59
|
|
|
{ |
60
|
|
|
throw new AnnotationException('The "with" property of the @before annotation must be of type array'); |
61
|
|
|
} |
62
|
|
|
$this->sMethodParams = $properties['with']; |
63
|
|
|
} |
64
|
|
|
$this->sMethodName = $properties['call']; |
65
|
|
|
} |
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @inheritDoc |
69
|
|
|
*/ |
|
|
|
|
70
|
|
|
public function getName(): string |
71
|
|
|
{ |
72
|
|
|
return '__before'; |
73
|
|
|
} |
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritDoc |
77
|
|
|
*/ |
|
|
|
|
78
|
|
|
public function getValue() |
79
|
|
|
{ |
80
|
|
|
if(is_array($this->xPrevValue)) |
81
|
|
|
{ |
82
|
|
|
// Add the current value to the array |
83
|
|
|
$this->xPrevValue[$this->sMethodName] = $this->sMethodParams; |
84
|
|
|
return $this->xPrevValue; |
85
|
|
|
} |
86
|
|
|
// Return the current value in an array |
87
|
|
|
return [$this->sMethodName => $this->sMethodParams]; |
88
|
|
|
} |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|