fix: Filter only null values

Creates an AbstractEndpoint to provide a method
to filter only `null` values.
This commit is contained in:
Michael Gerdemann
2020-01-18 09:18:45 +01:00
parent 1b66abe535
commit 09416c4ce1
3 changed files with 32 additions and 5 deletions

View File

@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace Avency\Gitea\Endpoint;
use Avency\Gitea\Client;
/**
* Abstract endpoint
*/
abstract class AbstractEndpoint implements EndpointInterface
{
/**
* @param array $array
* @return array
*/
protected function removeNullValues(array $array): array
{
return array_filter(
$array,
function($value) {
return !is_null($value);
}
);
}
}

View File

@ -9,7 +9,7 @@ use Avency\Gitea\Client;
/** /**
* Miscellaneous endpoint * Miscellaneous endpoint
*/ */
class Miscellaneous implements EndpointInterface class Miscellaneous extends AbstractEndpoint implements EndpointInterface
{ {
const BASE_URI = 'api/v1'; const BASE_URI = 'api/v1';
@ -41,7 +41,7 @@ class Miscellaneous implements EndpointInterface
'text' => $text, 'text' => $text,
'wiki' => $wiki 'wiki' => $wiki
]; ];
$options['json'] = array_filter($options['json']); $options['json'] = $this->removeNullValues($options['json']);
$response = $this->client->request(self::BASE_URI . '/markdown', 'POST', $options); $response = $this->client->request(self::BASE_URI . '/markdown', 'POST', $options);
return (string)$response->getBody(); return (string)$response->getBody();

View File

@ -9,7 +9,7 @@ use Avency\Gitea\Client;
/** /**
* Repositories endpoint * Repositories endpoint
*/ */
class Repositories implements EndpointInterface class Repositories extends AbstractEndpoint implements EndpointInterface
{ {
const BASE_URI = 'api/v1/repos'; const BASE_URI = 'api/v1/repos';
@ -79,7 +79,7 @@ class Repositories implements EndpointInterface
] ]
]; ];
$options['json'] = array_filter($options['json']); $options['json'] = $this->removeNullValues($options['json']);
$response = $this->client->request(self::BASE_URI . '/migrate', 'POST', $options); $response = $this->client->request(self::BASE_URI . '/migrate', 'POST', $options);
return \GuzzleHttp\json_decode($response->getBody(), true); return \GuzzleHttp\json_decode($response->getBody(), true);
} }
@ -134,7 +134,7 @@ class Repositories implements EndpointInterface
'sort' => $sort, 'sort' => $sort,
'order' => $order, 'order' => $order,
]; ];
$options['query'] = array_filter($options['query']); $options['query'] = $this->removeNullValues($options['query']);
$response = $this->client->request(self::BASE_URI . '/search', 'GET', $options); $response = $this->client->request(self::BASE_URI . '/search', 'GET', $options);
return \GuzzleHttp\json_decode($response->getBody(), true); return \GuzzleHttp\json_decode($response->getBody(), true);
} }