UploadAnnotation   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 59
rs 10
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A parseAnnotation() 0 4 2
A getValue() 0 3 1
A getName() 0 3 1
A validateUploadField() 0 3 1
A initAnnotation() 0 11 5
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 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 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]] : ['field' => $aParams[0], 'extra' => $aParams[1]];
45
    }
46
47
    /**
48
     * @param string $sFieldName
49
     *
50
     * @return bool
51
     */
52
    protected function validateUploadField(string $sFieldName): bool
53
    {
54
        return preg_match('/^[a-zA-Z][a-zA-Z0-9_\-\.]*$/', $sFieldName) > 0;
55
    }
56
57
    /**
58
     * @inheritDoc
59
     * @throws AnnotationException
60
     */
61
    public function initAnnotation(array $properties)
62
    {
63
        if(count($properties) != 1 || !isset($properties['field']) || !is_string($properties['field']))
64
        {
65
            throw new AnnotationException('The @upload annotation requires a property "field" of type string');
66
        }
67
        if(!$this->validateUploadField($properties['field']))
68
        {
69
            throw new AnnotationException($properties['field'] . ' is not a valid "field" value for the @upload annotation');
70
        }
71
        $this->sField = $properties['field'];
72
    }
73
74
    /**
75
     * @inheritDoc
76
     */
77
    public function getName(): string
78
    {
79
        return 'upload';
80
    }
81
82
    /**
83
     * @inheritDoc
84
     */
85
    public function getValue()
86
    {
87
        return "'" . $this->sField . "'" ; // The field id is surrounded with simple quotes.
88
    }
89
}
90