UploadAnnotation::parseAnnotation()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
/**
4
 * UploadAnnotation.php
5
 *
6
 * Jaxon annotation for file upload.
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 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 an upload form field id.
26
 *
27
 * @usage('method' => true)
28
 */
29
class UploadAnnotation extends AbstractAnnotation
30
{
31
    /**
32
     * The name of the upload field
33
     *
34
     * @var string
35
     */
36
    protected $sField = '';
37
38
    /**
39
     * @inheritDoc
40
     */
41
    public static function parseAnnotation($value)
42
    {
43
        $aParams = preg_split('/[\s]+/', $value, 2);
44
        return count($aParams) === 1 ? ['field' => $aParams[0]] :
45
            ['field' => $aParams[0], 'extra' => $aParams[1]];
46
    }
47
48
    /**
49
     * @inheritDoc
50
     * @throws AnnotationException
51
     */
52
    public function initAnnotation(array $properties)
53
    {
54
        if(count($properties) != 1 || !isset($properties['field']) ||
55
            !is_string($properties['field']))
56
        {
57
            throw new AnnotationException('The @upload annotation requires a property "field" of type string');
58
        }
59
        $this->sField = $properties['field'];
60
    }
61
62
    /**
63
     * @inheritDoc
64
     */
65
    public function saveValue(Metadata $xMetadata, string $sMethod = '*'): void
66
    {
67
        $xMetadata->upload($sMethod)->setValue($this->sField);
68
    }
69
}
70