Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
namespace Aws\EndpointV2\Ruleset;
4
 
5
use Aws\Exception\UnresolvedEndpointException;
1441 ariadna 6
use function \Aws\is_associative;
1 efrain 7
 
8
/**
9
 * Houses properties of an individual parameter definition.
10
 */
11
class RulesetParameter
12
{
13
    /** @var string */
14
    private $name;
15
 
16
    /** @var string */
17
    private $type;
18
 
19
    /** @var string */
20
    private $builtIn;
21
 
22
    /** @var string */
23
    private $default;
24
 
25
    /** @var array */
26
    private $required;
27
 
28
    /** @var string */
29
    private $documentation;
30
 
31
    /** @var boolean */
32
    private $deprecated;
33
 
1441 ariadna 34
    /** @var array<string, string> */
35
    private static $typeMap = [
36
        'String' => 'is_string',
37
        'Boolean' => 'is_bool',
38
        'StringArray' => 'isStringArray'
39
    ];
40
 
1 efrain 41
    public function __construct($name, array $definition)
42
    {
43
        $type = ucfirst($definition['type']);
44
        if ($this->isValidType($type)) {
45
            $this->type = $type;
46
        } else {
47
            throw new UnresolvedEndpointException(
48
                'Unknown parameter type ' . "`{$type}`" .
1441 ariadna 49
                '. Parameters must be of type `String`, `Boolean` or `StringArray.'
1 efrain 50
            );
51
        }
1441 ariadna 52
 
1 efrain 53
        $this->name = $name;
1441 ariadna 54
        $this->builtIn = $definition['builtIn'] ?? null;
55
        $this->default = $definition['default'] ?? null;
56
        $this->required = $definition['required'] ?? false;
57
        $this->documentation = $definition['documentation'] ?? null;
58
        $this->deprecated = $definition['deprecated'] ?? false;
1 efrain 59
    }
60
 
61
    /**
62
     * @return mixed
63
     */
64
    public function getName()
65
    {
66
        return $this->name;
67
    }
68
 
69
    /**
70
     * @return mixed
71
     */
72
    public function getType()
73
    {
74
        return $this->type;
75
    }
76
 
77
    /**
78
     * @return mixed
79
     */
80
    public function getBuiltIn()
81
    {
82
        return $this->builtIn;
83
    }
84
 
85
    /**
86
     * @return mixed
87
     */
88
    public function getDefault()
89
    {
90
        return $this->default;
91
    }
92
 
93
    /**
94
     * @return boolean
95
     */
96
    public function getRequired()
97
    {
98
        return $this->required;
99
    }
100
 
101
    /**
102
     * @return string
103
     */
104
    public function getDocumentation()
105
    {
106
        return $this->documentation;
107
    }
108
 
109
    /**
110
     * @return boolean
111
     */
112
    public function getDeprecated()
113
    {
114
        return $this->deprecated;
115
    }
116
 
117
    /**
118
     * Validates that an input parameter matches the type provided in its definition.
119
     *
120
     * @return void
121
     * @throws InvalidArgumentException
122
     */
123
    public function validateInputParam($inputParam)
124
    {
1441 ariadna 125
        if (!$this->isValidInput($inputParam)) {
1 efrain 126
            throw new UnresolvedEndpointException(
127
                "Input parameter `{$this->name}` is the wrong type. Must be a {$this->type}."
128
            );
129
        }
130
 
131
        if ($this->deprecated) {
132
            $deprecated = $this->deprecated;
133
            $deprecationString = "{$this->name} has been deprecated ";
1441 ariadna 134
            $msg = $deprecated['message'] ?? null;
135
            $since = $deprecated['since'] ?? null;
1 efrain 136
 
1441 ariadna 137
            if (!is_null($since)){
138
                $deprecationString .= 'since ' . $since . '. ';
139
            }
140
            if (!is_null($msg)) {
141
                $deprecationString .= $msg;
142
            }
1 efrain 143
 
144
            trigger_error($deprecationString, E_USER_WARNING);
145
        }
146
    }
147
 
148
    private function isValidType($type)
149
    {
1441 ariadna 150
        return isset(self::$typeMap[$type]);
1 efrain 151
    }
1441 ariadna 152
 
153
    private function isValidInput($inputParam): bool
154
    {
155
        $method = self::$typeMap[$this->type];
156
        if (is_callable($method)) {
157
            return $method($inputParam);
158
        } elseif (method_exists($this, $method)) {
159
            return $this->$method($inputParam);
160
        }
161
 
162
        return false;
163
    }
164
 
165
    private function isStringArray(array $array): bool
166
    {
167
        if (is_associative($array)) {
168
            return false;
169
        }
170
 
171
        foreach($array as $value) {
172
            if (!is_string($value)) {
173
                return false;
174
            }
175
        }
176
 
177
        return true;
178
    }
1 efrain 179
}