CallbackAnnotation::parseAnnotation()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 4
rs 10
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 Jaxon\App\Metadata\Metadata;
18
use mindplay\annotations\AnnotationException;
19
20
use function count;
21
use function is_string;
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 javascript object name
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
     * @inheritDoc
49
     * @throws AnnotationException
50
     */
51
    public function initAnnotation(array $properties)
52
    {
53
        if(count($properties) !== 1 || !isset($properties['name']) || !is_string($properties['name']))
54
        {
55
            throw new AnnotationException('the @callback annotation requires a single string as property');
56
        }
57
        $this->sJsObject = $properties['name'];
58
    }
59
60
    /**
61
     * @inheritDoc
62
     */
63
    public function saveValue(Metadata $xMetadata, string $sMethod = '*'): void
64
    {
65
        $xMetadata->callback($sMethod)->addValue($this->sJsObject);
66
    }
67
}
68