forked from bigbluebutton/bigbluebutton-api-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseParameters.php
More file actions
147 lines (127 loc) · 4.51 KB
/
Copy pathBaseParameters.php
File metadata and controls
147 lines (127 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
/*
* BigBlueButton open source conferencing system - https://www.bigbluebutton.org/.
*
* Copyright (c) 2016-2025 BigBlueButton Inc. and by respective authors (see below).
*
* This program is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation; either version 3.0 of the License, or (at your option) any later
* version.
*
* BigBlueButton is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along
* with BigBlueButton; if not, see <https://www.gnu.org/licenses/>.
*/
namespace BigBlueButton\Parameters;
use BigBlueButton\Attribute\ApiParameterMapper;
/**
* Class BaseParameters.
*/
abstract class BaseParameters
{
public function getHTTPQuery(): string
{
$apiData = $this->toApiDataArray();
// No need for null checks anymore since toApiDataArray() filters them out
foreach ($apiData as $value) {
if (!is_string($value)) {
throw new \RuntimeException(sprintf(
'Invalid API parameter type: %s',
gettype($value)
));
}
}
return $this->buildHTTPQuery($apiData);
}
/**
* @return array<string, null|string> // Keys are strings, values are strings or null
*/
public function toApiDataArray(): array
{
$result = [];
$classReflection = new \ReflectionClass($this);
foreach ($classReflection->getMethods() as $method) {
foreach ($method->getAttributes(ApiParameterMapper::class) as $attribute) {
/** @var ApiParameterMapper $attributeObject */
$attributeObject = $attribute->newInstance();
$key = $attributeObject->getAttributeName();
$value = $this->strictConvertToApiValue($this->{$method->getName()}());
// Only include non-null values
if (null !== $value) {
$result[$key] = $value;
}
}
}
return $result;
}
/**
* @param array<string, null|string> $array // Keys and values are both strings
*/
protected function buildHTTPQuery(array $array): string
{
return str_replace(
['%20', '!', "'", '(', ')', '*'],
['+', '%21', '%27', '%28', '%29', '%2A'],
http_build_query($array, '', '&', \PHP_QUERY_RFC3986)
);
}
/**
* Converts any value to API string format with strict type enforcement.
*
* @param mixed $value
*/
private function strictConvertToApiValue($value): ?string
{
if (null === $value) {
return null;
}
// Handle BackedEnum cases
if ($value instanceof \BackedEnum) {
$enumValue = $value->value;
if (!is_scalar($enumValue)) {
throw new \RuntimeException(sprintf(
'Enum value for %s must be scalar, got %s',
get_class($value),
gettype($enumValue)
));
}
return (string) $enumValue;
}
// Handle arrays
if (is_array($value)) {
return $this->convertArrayToApiString($value);
}
// Handle all other cases with strict string conversion
if (is_bool($value)) {
return $value ? 'true' : 'false';
}
if (is_object($value)) {
throw new \RuntimeException(sprintf(
'Cannot convert object of type %s to API value',
get_class($value)
));
}
// Force string conversion for all scalar values
return (string) $value;
}
/**
* Converts array values to comma-separated string with strict typing.
*
* @param array<mixed> $values // Array of mixed values that will be converted
*/
private function convertArrayToApiString(array $values): string
{
$converted = [];
foreach ($values as $item) {
$convertedItem = $this->strictConvertToApiValue($item);
if (null !== $convertedItem) {
$converted[] = $convertedItem;
}
}
return implode(',', $converted);
}
}