|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Formularium; |
|
4
|
|
|
|
|
5
|
|
|
use Formularium\Exception\Exception; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class to store extra user parameters |
|
9
|
|
|
*/ |
|
10
|
|
|
final class Extradata |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
public $name; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var ExtradataParameter[] |
|
19
|
|
|
*/ |
|
20
|
|
|
public $args = []; |
|
21
|
|
|
|
|
22
|
|
|
public static function getFromData(string $name = null, array $data) : Extradata |
|
23
|
|
|
{ |
|
24
|
|
|
if (!$name) { |
|
25
|
|
|
$name = $data['name'] ?? null; |
|
26
|
|
|
} |
|
27
|
|
|
if (!$name) { |
|
28
|
|
|
throw new Exception("Missing name in fields"); |
|
29
|
|
|
} |
|
30
|
|
|
return new Extradata($name, $data ?? []); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function __construct(string $name, array $args = []) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->name = $name; |
|
36
|
|
|
foreach ($args as $n => $d) { |
|
37
|
|
|
$this->args[] = ($d instanceof ExtradataParameter) ? $d : new ExtradataParameter($d['name'], $d['value']); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function appendParameter(ExtradataParameter $p): self |
|
42
|
|
|
{ |
|
43
|
|
|
$this->args[] = $p; |
|
44
|
|
|
return $this; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function hasParameter(string $name): bool |
|
48
|
|
|
{ |
|
49
|
|
|
foreach ($this->args as $a) { |
|
50
|
|
|
if ($a->name === $name) { |
|
51
|
|
|
return true; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
return false; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Undocumented function |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $name The parameter name |
|
61
|
|
|
* @param Mixed $default |
|
62
|
|
|
* @return Mixed |
|
63
|
|
|
*/ |
|
64
|
|
|
public function value(string $name, $default = null) |
|
65
|
|
|
{ |
|
66
|
|
|
foreach ($this->args as $a) { |
|
67
|
|
|
if ($a->name === $name) { |
|
68
|
|
|
return $a->value; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
return $default; |
|
72
|
|
|
} |
|
73
|
|
|
/** |
|
74
|
|
|
* Undocumented function |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $name |
|
77
|
|
|
* @return ExtradataParameter|null |
|
78
|
|
|
*/ |
|
79
|
|
|
public function parameter(string $name): ?ExtradataParameter |
|
80
|
|
|
{ |
|
81
|
|
|
foreach ($this->args as $a) { |
|
82
|
|
|
if ($a->name === $name) { |
|
83
|
|
|
return $a; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
return null; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Get the value of name |
|
91
|
|
|
* |
|
92
|
|
|
* @return string |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getName() |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->name; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Set the value of name |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $name |
|
103
|
|
|
* |
|
104
|
|
|
* @return self |
|
105
|
|
|
*/ |
|
106
|
|
|
public function setName(string $name) |
|
107
|
|
|
{ |
|
108
|
|
|
$this->name = $name; |
|
109
|
|
|
|
|
110
|
|
|
return $this; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|