Skip to content

Commit 52ab35c

Browse files
Merge pull request #919 from appwrite/fix-php
Fix PHP
2 parents d0c35c0 + 22d4bac commit 52ab35c

File tree

11 files changed

+77
-75
lines changed

11 files changed

+77
-75
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ jobs:
3434
Node16,
3535
Node18,
3636
Node20,
37-
PHP74,
3837
PHP80,
38+
PHP83,
3939
Python38,
4040
Python39,
4141
Python310,

templates/php/base/params.twig

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
$apiParams = [];
22
{% if method.parameters.all | length %}
33
{% for parameter in method.parameters.all %}
4-
{% if parameter.required and not parameter.nullable %}
5-
if (!isset(${{ parameter.name | caseCamel | escapeKeyword }})) {
6-
throw new {{spec.title | caseUcfirst}}Exception('Missing required parameter: "{{ parameter.name | caseCamel | escapeKeyword }}"');
7-
}
8-
{% endif %}
9-
{% endfor %}
10-
{% for parameter in method.parameters.query %}
11-
if (!is_null(${{ parameter.name | caseCamel | escapeKeyword }})) {
12-
$apiParams['{{ parameter.name }}'] = ${{ parameter.name | caseCamel | escapeKeyword }};
13-
}
14-
{% endfor %}
15-
{% for parameter in method.parameters.body %}
16-
if (!is_null(${{ parameter.name | caseCamel | escapeKeyword }})) {
17-
$apiParams['{{ parameter.name }}'] = ${{ parameter.name | caseCamel | escapeKeyword }};
18-
}
19-
{% endfor %}
20-
{% for parameter in method.parameters.formData %}
4+
{% if not parameter.required and not parameter.nullable %}
5+
216
if (!is_null(${{ parameter.name | caseCamel | escapeKeyword }})) {
227
$apiParams['{{ parameter.name }}'] = ${{ parameter.name | caseCamel | escapeKeyword }};
238
}
9+
{% else %}
10+
$apiParams['{{ parameter.name }}'] = ${{ parameter.name | caseCamel | escapeKeyword }};
11+
{% endif %}
2412
{% endfor %}
25-
{% endif %}
13+
{% endif %}
14+
15+
$apiHeaders = [];
16+
{%~ for parameter in method.parameters.header %}
17+
$apiHeaders['{{ parameter.name }}'] = ${{ parameter.name | caseCamel | escapeKeyword }};
18+
{%~ endfor %}
19+
{%~ for key, header in method.headers %}
20+
$apiHeaders['{{ key }}'] = '{{ header }}';
21+
{%~ endfor %}

templates/php/base/requests/api.twig

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
return $this->client->call(
22
Client::METHOD_{{ method.method | caseUpper }},
33
$apiPath,
4-
[
5-
{%~ for parameter in method.parameters.header %}
6-
'{{ parameter.name }}' => ${{ parameter.name | caseCamel | escapeKeyword }},
7-
{%~ endfor %}
8-
{%~ for key, header in method.headers %}
9-
'{{ key }}' => '{{ header }}',
10-
{%~ endfor %}
11-
],
4+
$apiHeaders,
125
$apiParams{% if method.type == 'webAuth' -%}, 'location'{% endif %}
136

147
);

templates/php/src/Client.php.twig

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,28 @@ class Client
1414
const METHOD_CONNECT = 'CONNECT';
1515
const METHOD_TRACE = 'TRACE';
1616
17-
const CHUNK_SIZE = 5*1024*1024;
17+
const CHUNK_SIZE = 5 * 1024 * 1024;
1818
1919
/**
2020
* Is Self Signed Certificates Allowed?
2121
*
2222
* @var bool
2323
*/
24-
protected $selfSigned = false;
24+
protected bool $selfSigned = false;
2525
2626
/**
2727
* Service host name
2828
*
2929
* @var string
3030
*/
31-
protected $endpoint = '{{spec.endpoint}}';
31+
protected string $endpoint = '{{spec.endpoint}}';
3232
3333
/**
3434
* Global Headers
3535
*
3636
* @var array
3737
*/
38-
protected $headers = [
38+
protected array $headers = [
3939
'content-type' => '',
4040
'user-agent' => '{{spec.title | caseUcfirst}}{{ language.name | caseUcfirst }}SDK/{{ sdk.version }} ({{deviceInfo}})',
4141
'x-sdk-name'=> '{{ sdk.name }}',
@@ -45,7 +45,7 @@ class Client
4545
];
4646
4747
/**
48-
* SDK constructor.
48+
* Client constructor.
4949
*/
5050
public function __construct()
5151
{
@@ -66,7 +66,7 @@ class Client
6666
*
6767
* @return Client
6868
*/
69-
public function set{{header.key | caseUcfirst}}($value)
69+
public function set{{header.key | caseUcfirst}}(string $value): Client
7070
{
7171
$this->addHeader('{{header.name}}', $value);
7272
@@ -79,7 +79,7 @@ class Client
7979
* @param bool $status
8080
* @return $this
8181
*/
82-
public function setSelfSigned($status = true)
82+
public function setSelfSigned(bool $status = true): Client
8383
{
8484
$this->selfSigned = $status;
8585
@@ -90,7 +90,7 @@ class Client
9090
* @param $endpoint
9191
* @return $this
9292
*/
93-
public function setEndpoint($endpoint)
93+
public function setEndpoint(string $endpoint): Client
9494
{
9595
$this->endpoint = $endpoint;
9696
@@ -101,7 +101,7 @@ class Client
101101
* @param $key
102102
* @param $value
103103
*/
104-
public function addHeader($key, $value)
104+
public function addHeader(string $key, string $value): Client
105105
{
106106
$this->headers[strtolower($key)] = $value;
107107
@@ -120,7 +120,13 @@ class Client
120120
* @return array|string
121121
* @throws {{spec.title | caseUcfirst}}Exception
122122
*/
123-
public function call($method, $path = '', $headers = array(), array $params = array(), ?string $responseType = null)
123+
public function call(
124+
string $method,
125+
string $path = '',
126+
array $headers = [],
127+
array $params = [],
128+
?string $responseType = null
129+
)
124130
{
125131
$headers = array_merge($this->headers, $headers);
126132
$ch = curl_init($this->endpoint . $path . (($method == self::METHOD_GET && !empty($params)) ? '?' . http_build_query($params) : ''));
@@ -191,14 +197,14 @@ class Client
191197
}
192198
193199
if (curl_errno($ch)) {
194-
throw new {{spec.title | caseUcfirst}}Exception(curl_error($ch), $responseStatus, $responseBody);
200+
throw new {{spec.title | caseUcfirst}}Exception(curl_error($ch), $responseStatus, $responseBody['type'] ?? '', $responseBody);
195201
}
196202
197203
curl_close($ch);
198204
199205
if($responseStatus >= 400) {
200206
if(is_array($responseBody)) {
201-
throw new {{spec.title | caseUcfirst}}Exception($responseBody['message'], $responseStatus, $responseBody['type'] ?? '', $responseBody);
207+
throw new {{spec.title | caseUcfirst}}Exception($responseBody['message'], $responseStatus, $responseBody['type'] ?? '', json_encode($responseBody));
202208
} else {
203209
throw new {{spec.title | caseUcfirst}}Exception($responseBody, $responseStatus);
204210
}
@@ -218,7 +224,7 @@ class Client
218224
* @param string $prefix
219225
* @return array
220226
*/
221-
protected function flatten(array $data, $prefix = '') {
227+
protected function flatten(array $data, string $prefix = ''): array {
222228
$output = [];
223229
224230
foreach($data as $key => $value) {

templates/php/src/Exception.php.twig

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,25 @@ class {{spec.title | caseUcfirst}}Exception extends Exception {
99
/**
1010
* @var mixed
1111
*/
12-
private $response;
12+
private ?string $response;
1313
1414
/**
1515
* @var string
1616
*/
17-
private $type;
17+
private string $type;
1818
1919
/**
20-
* @param String $message
20+
* @param ?string $message
2121
* @param int $code
22-
* @param mixed $response
22+
* @param string $type
23+
* @param ?string $response
2324
*/
24-
public function __construct($message = null, $code = 0, $type = null, $response = null)
25-
{
25+
public function __construct(
26+
?string $message = null,
27+
int $code = 0,
28+
string $type = '',
29+
?string $response = null
30+
) {
2631
parent::__construct($message, $code);
2732
$this->response = $response;
2833
$this->type = $type;
@@ -31,15 +36,15 @@ class {{spec.title | caseUcfirst}}Exception extends Exception {
3136
/**
3237
* @return string
3338
*/
34-
public function getType()
39+
public function getType(): string
3540
{
3641
return $this->type;
3742
}
3843
3944
/**
40-
* @return mixed
45+
* @return ?string
4146
*/
42-
final public function getResponse()
47+
final public function getResponse(): ?string
4348
{
4449
return $this->response;
4550
}

templates/php/src/InputFile.php.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class InputFile {
2929
return $this->filename;
3030
}
3131
32-
public static function withPath(string $path, ?string $mimeType = null, ?string $filename = null)
32+
public static function withPath(string $path, ?string $mimeType = null, ?string $filename = null): InputFile
3333
{
3434
$instance = new InputFile();
3535
$instance->path = $path;
@@ -39,7 +39,7 @@ class InputFile {
3939
return $instance;
4040
}
4141
42-
public static function withData(string $data, ?string $mimeType = null, ?string $filename = null)
42+
public static function withData(string $data, ?string $mimeType = null, ?string $filename = null): InputFile
4343
{
4444
$instance = new InputFile();
4545
$instance->path = null;

templates/php/src/Permission.php.twig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,22 @@ class Permission
88
{
99
return "read(\"$role\")";
1010
}
11+
1112
public static function write(string $role): string
1213
{
1314
return "write(\"$role\")";
1415
}
16+
1517
public static function create(string $role): string
1618
{
1719
return "create(\"$role\")";
1820
}
21+
1922
public static function update(string $role): string
2023
{
2124
return "update(\"$role\")";
2225
}
26+
2327
public static function delete(string $role): string
2428
{
2529
return "delete(\"$role\")";

templates/php/src/Query.php.twig

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ class Query implements \JsonSerializable
2323
2424
public function __toString(): string
2525
{
26-
return json_encode($this->jsonSerialize());
26+
return json_encode($this);
2727
}
2828
29-
public function jsonSerialize(): array
29+
public function jsonSerialize(): mixed
3030
{
3131
return array_filter([
3232
'method' => $this->method,
@@ -42,7 +42,7 @@ class Query implements \JsonSerializable
4242
* @param mixed $value
4343
* @return string
4444
*/
45-
public static function equal(string $attribute, $value): string
45+
public static function equal(string $attribute, mixed $value): string
4646
{
4747
return (new Query('equal', $attribute, $value))->__toString();
4848
}
@@ -54,7 +54,7 @@ class Query implements \JsonSerializable
5454
* @param mixed $value
5555
* @return string
5656
*/
57-
public static function notEqual(string $attribute, $value): string
57+
public static function notEqual(string $attribute, mixed $value): string
5858
{
5959
return (new Query('notEqual', $attribute, $value))->__toString();
6060
}
@@ -66,7 +66,7 @@ class Query implements \JsonSerializable
6666
* @param mixed $value
6767
* @return string
6868
*/
69-
public static function lessThan(string $attribute, $value): string
69+
public static function lessThan(string $attribute, mixed $value): string
7070
{
7171
return (new Query('lessThan', $attribute, $value))->__toString();
7272
}
@@ -78,7 +78,7 @@ class Query implements \JsonSerializable
7878
* @param mixed $value
7979
* @return string
8080
*/
81-
public static function lessThanEqual(string $attribute, $value): string
81+
public static function lessThanEqual(string $attribute, mixed $value): string
8282
{
8383
return (new Query('lessThanEqual', $attribute, $value))->__toString();
8484
}
@@ -90,7 +90,7 @@ class Query implements \JsonSerializable
9090
* @param mixed $value
9191
* @return string
9292
*/
93-
public static function greaterThan(string $attribute, $value): string
93+
public static function greaterThan(string $attribute, mixed $value): string
9494
{
9595
return (new Query('greaterThan', $attribute, $value))->__toString();
9696
}
@@ -102,7 +102,7 @@ class Query implements \JsonSerializable
102102
* @param mixed $value
103103
* @return string
104104
*/
105-
public static function greaterThanEqual(string $attribute, $value): string
105+
public static function greaterThanEqual(string $attribute, mixed $value): string
106106
{
107107
return (new Query('greaterThanEqual', $attribute, $value))->__toString();
108108
}
@@ -149,7 +149,7 @@ class Query implements \JsonSerializable
149149
* @param string|int|float $end
150150
* @return string
151151
*/
152-
public static function between(string $attribute, $start, $end): string
152+
public static function between(string $attribute, mixed $start, mixed $end): string
153153
{
154154
return (new Query('between', $attribute, [$start, $end]))->__toString();
155155
}

templates/php/src/Service.php.twig

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,8 @@ namespace {{ spec.title | caseUcfirst }};
44
55
abstract class Service
66
{
7-
/**
8-
* @var Client
9-
*/
10-
protected $client;
7+
protected Client $client;
118
12-
/**
13-
* @param Client $client
14-
*/
159
public function __construct(Client $client)
1610
{
1711
$this->client = $client;

0 commit comments

Comments
 (0)