Skip to content

Commit

Permalink
Merge pull request #919 from appwrite/fix-php
Browse files Browse the repository at this point in the history
Fix PHP
  • Loading branch information
christyjacob4 authored Aug 20, 2024
2 parents d0c35c0 + 22d4bac commit 52ab35c
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 75 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ jobs:
Node16,
Node18,
Node20,
PHP74,
PHP80,
PHP83,
Python38,
Python39,
Python310,
Expand Down
32 changes: 14 additions & 18 deletions templates/php/base/params.twig
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
$apiParams = [];
{% if method.parameters.all | length %}
{% for parameter in method.parameters.all %}
{% if parameter.required and not parameter.nullable %}
if (!isset(${{ parameter.name | caseCamel | escapeKeyword }})) {
throw new {{spec.title | caseUcfirst}}Exception('Missing required parameter: "{{ parameter.name | caseCamel | escapeKeyword }}"');
}
{% endif %}
{% endfor %}
{% for parameter in method.parameters.query %}
if (!is_null(${{ parameter.name | caseCamel | escapeKeyword }})) {
$apiParams['{{ parameter.name }}'] = ${{ parameter.name | caseCamel | escapeKeyword }};
}
{% endfor %}
{% for parameter in method.parameters.body %}
if (!is_null(${{ parameter.name | caseCamel | escapeKeyword }})) {
$apiParams['{{ parameter.name }}'] = ${{ parameter.name | caseCamel | escapeKeyword }};
}
{% endfor %}
{% for parameter in method.parameters.formData %}
{% if not parameter.required and not parameter.nullable %}

if (!is_null(${{ parameter.name | caseCamel | escapeKeyword }})) {
$apiParams['{{ parameter.name }}'] = ${{ parameter.name | caseCamel | escapeKeyword }};
}
{% else %}
$apiParams['{{ parameter.name }}'] = ${{ parameter.name | caseCamel | escapeKeyword }};
{% endif %}
{% endfor %}
{% endif %}
{% endif %}

$apiHeaders = [];
{%~ for parameter in method.parameters.header %}
$apiHeaders['{{ parameter.name }}'] = ${{ parameter.name | caseCamel | escapeKeyword }};
{%~ endfor %}
{%~ for key, header in method.headers %}
$apiHeaders['{{ key }}'] = '{{ header }}';
{%~ endfor %}
9 changes: 1 addition & 8 deletions templates/php/base/requests/api.twig
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
return $this->client->call(
Client::METHOD_{{ method.method | caseUpper }},
$apiPath,
[
{%~ for parameter in method.parameters.header %}
'{{ parameter.name }}' => ${{ parameter.name | caseCamel | escapeKeyword }},
{%~ endfor %}
{%~ for key, header in method.headers %}
'{{ key }}' => '{{ header }}',
{%~ endfor %}
],
$apiHeaders,
$apiParams{% if method.type == 'webAuth' -%}, 'location'{% endif %}

);
32 changes: 19 additions & 13 deletions templates/php/src/Client.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,28 @@ class Client
const METHOD_CONNECT = 'CONNECT';
const METHOD_TRACE = 'TRACE';
const CHUNK_SIZE = 5*1024*1024;
const CHUNK_SIZE = 5 * 1024 * 1024;
/**
* Is Self Signed Certificates Allowed?
*
* @var bool
*/
protected $selfSigned = false;
protected bool $selfSigned = false;
/**
* Service host name
*
* @var string
*/
protected $endpoint = '{{spec.endpoint}}';
protected string $endpoint = '{{spec.endpoint}}';
/**
* Global Headers
*
* @var array
*/
protected $headers = [
protected array $headers = [
'content-type' => '',
'user-agent' => '{{spec.title | caseUcfirst}}{{ language.name | caseUcfirst }}SDK/{{ sdk.version }} ({{deviceInfo}})',
'x-sdk-name'=> '{{ sdk.name }}',
Expand All @@ -45,7 +45,7 @@ class Client
];
/**
* SDK constructor.
* Client constructor.
*/
public function __construct()
{
Expand All @@ -66,7 +66,7 @@ class Client
*
* @return Client
*/
public function set{{header.key | caseUcfirst}}($value)
public function set{{header.key | caseUcfirst}}(string $value): Client
{
$this->addHeader('{{header.name}}', $value);
Expand All @@ -79,7 +79,7 @@ class Client
* @param bool $status
* @return $this
*/
public function setSelfSigned($status = true)
public function setSelfSigned(bool $status = true): Client
{
$this->selfSigned = $status;
Expand All @@ -90,7 +90,7 @@ class Client
* @param $endpoint
* @return $this
*/
public function setEndpoint($endpoint)
public function setEndpoint(string $endpoint): Client
{
$this->endpoint = $endpoint;
Expand All @@ -101,7 +101,7 @@ class Client
* @param $key
* @param $value
*/
public function addHeader($key, $value)
public function addHeader(string $key, string $value): Client
{
$this->headers[strtolower($key)] = $value;
Expand All @@ -120,7 +120,13 @@ class Client
* @return array|string
* @throws {{spec.title | caseUcfirst}}Exception
*/
public function call($method, $path = '', $headers = array(), array $params = array(), ?string $responseType = null)
public function call(
string $method,
string $path = '',
array $headers = [],
array $params = [],
?string $responseType = null
)
{
$headers = array_merge($this->headers, $headers);
$ch = curl_init($this->endpoint . $path . (($method == self::METHOD_GET && !empty($params)) ? '?' . http_build_query($params) : ''));
Expand Down Expand Up @@ -191,14 +197,14 @@ class Client
}
if (curl_errno($ch)) {
throw new {{spec.title | caseUcfirst}}Exception(curl_error($ch), $responseStatus, $responseBody);
throw new {{spec.title | caseUcfirst}}Exception(curl_error($ch), $responseStatus, $responseBody['type'] ?? '', $responseBody);
}
curl_close($ch);
if($responseStatus >= 400) {
if(is_array($responseBody)) {
throw new {{spec.title | caseUcfirst}}Exception($responseBody['message'], $responseStatus, $responseBody['type'] ?? '', $responseBody);
throw new {{spec.title | caseUcfirst}}Exception($responseBody['message'], $responseStatus, $responseBody['type'] ?? '', json_encode($responseBody));
} else {
throw new {{spec.title | caseUcfirst}}Exception($responseBody, $responseStatus);
}
Expand All @@ -218,7 +224,7 @@ class Client
* @param string $prefix
* @return array
*/
protected function flatten(array $data, $prefix = '') {
protected function flatten(array $data, string $prefix = ''): array {
$output = [];
foreach($data as $key => $value) {
Expand Down
23 changes: 14 additions & 9 deletions templates/php/src/Exception.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,25 @@ class {{spec.title | caseUcfirst}}Exception extends Exception {
/**
* @var mixed
*/
private $response;
private ?string $response;
/**
* @var string
*/
private $type;
private string $type;
/**
* @param String $message
* @param ?string $message
* @param int $code
* @param mixed $response
* @param string $type
* @param ?string $response
*/
public function __construct($message = null, $code = 0, $type = null, $response = null)
{
public function __construct(
?string $message = null,
int $code = 0,
string $type = '',
?string $response = null
) {
parent::__construct($message, $code);
$this->response = $response;
$this->type = $type;
Expand All @@ -31,15 +36,15 @@ class {{spec.title | caseUcfirst}}Exception extends Exception {
/**
* @return string
*/
public function getType()
public function getType(): string
{
return $this->type;
}
/**
* @return mixed
* @return ?string
*/
final public function getResponse()
final public function getResponse(): ?string
{
return $this->response;
}
Expand Down
4 changes: 2 additions & 2 deletions templates/php/src/InputFile.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class InputFile {
return $this->filename;
}
public static function withPath(string $path, ?string $mimeType = null, ?string $filename = null)
public static function withPath(string $path, ?string $mimeType = null, ?string $filename = null): InputFile
{
$instance = new InputFile();
$instance->path = $path;
Expand All @@ -39,7 +39,7 @@ class InputFile {
return $instance;
}
public static function withData(string $data, ?string $mimeType = null, ?string $filename = null)
public static function withData(string $data, ?string $mimeType = null, ?string $filename = null): InputFile
{
$instance = new InputFile();
$instance->path = null;
Expand Down
4 changes: 4 additions & 0 deletions templates/php/src/Permission.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,22 @@ class Permission
{
return "read(\"$role\")";
}
public static function write(string $role): string
{
return "write(\"$role\")";
}
public static function create(string $role): string
{
return "create(\"$role\")";
}
public static function update(string $role): string
{
return "update(\"$role\")";
}
public static function delete(string $role): string
{
return "delete(\"$role\")";
Expand Down
18 changes: 9 additions & 9 deletions templates/php/src/Query.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ class Query implements \JsonSerializable
public function __toString(): string
{
return json_encode($this->jsonSerialize());
return json_encode($this);
}
public function jsonSerialize(): array
public function jsonSerialize(): mixed
{
return array_filter([
'method' => $this->method,
Expand All @@ -42,7 +42,7 @@ class Query implements \JsonSerializable
* @param mixed $value
* @return string
*/
public static function equal(string $attribute, $value): string
public static function equal(string $attribute, mixed $value): string
{
return (new Query('equal', $attribute, $value))->__toString();
}
Expand All @@ -54,7 +54,7 @@ class Query implements \JsonSerializable
* @param mixed $value
* @return string
*/
public static function notEqual(string $attribute, $value): string
public static function notEqual(string $attribute, mixed $value): string
{
return (new Query('notEqual', $attribute, $value))->__toString();
}
Expand All @@ -66,7 +66,7 @@ class Query implements \JsonSerializable
* @param mixed $value
* @return string
*/
public static function lessThan(string $attribute, $value): string
public static function lessThan(string $attribute, mixed $value): string
{
return (new Query('lessThan', $attribute, $value))->__toString();
}
Expand All @@ -78,7 +78,7 @@ class Query implements \JsonSerializable
* @param mixed $value
* @return string
*/
public static function lessThanEqual(string $attribute, $value): string
public static function lessThanEqual(string $attribute, mixed $value): string
{
return (new Query('lessThanEqual', $attribute, $value))->__toString();
}
Expand All @@ -90,7 +90,7 @@ class Query implements \JsonSerializable
* @param mixed $value
* @return string
*/
public static function greaterThan(string $attribute, $value): string
public static function greaterThan(string $attribute, mixed $value): string
{
return (new Query('greaterThan', $attribute, $value))->__toString();
}
Expand All @@ -102,7 +102,7 @@ class Query implements \JsonSerializable
* @param mixed $value
* @return string
*/
public static function greaterThanEqual(string $attribute, $value): string
public static function greaterThanEqual(string $attribute, mixed $value): string
{
return (new Query('greaterThanEqual', $attribute, $value))->__toString();
}
Expand Down Expand Up @@ -149,7 +149,7 @@ class Query implements \JsonSerializable
* @param string|int|float $end
* @return string
*/
public static function between(string $attribute, $start, $end): string
public static function between(string $attribute, mixed $start, mixed $end): string
{
return (new Query('between', $attribute, [$start, $end]))->__toString();
}
Expand Down
8 changes: 1 addition & 7 deletions templates/php/src/Service.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,8 @@ namespace {{ spec.title | caseUcfirst }};
abstract class Service
{
/**
* @var Client
*/
protected $client;
protected Client $client;
/**
* @param Client $client
*/
public function __construct(Client $client)
{
$this->client = $client;
Expand Down
Loading

0 comments on commit 52ab35c

Please sign in to comment.