Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
/tests/files/api.log
/coverage/*
/index.html
/phpcs.xml
/phpstan.neon
/phpunit.xml
/psalm.xml
/provision/*
/vendor/*
!/vendor/.gitkeep
Expand Down
11 changes: 7 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
"woohoolabs/yang": "^3.0"
},
"require-dev": {
"cakephp/cakephp-codesniffer": "^3.0",
"cakephp/cakephp-codesniffer": "^5.0",
"josegonzalez/dotenv": "2.*",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^9.0",
"phpstan/phpstan": "^1.12",
"phpunit/phpunit": "^10.5.5 || ^11.1.3",
"psy/psysh": "@stable",
"vimeo/psalm": "^5.18"
},
Expand Down Expand Up @@ -64,6 +64,9 @@
},
"prefer-stable": true,
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
4 changes: 2 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" processIsolation="false" stopOnFailure="false" bootstrap="./tests/bootstrap.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<source>
<include>
<directory suffix=".php">./src/</directory>
</include>
</coverage>
</source>
<php>
<ini name="memory_limit" value="-1"/>
<ini name="apc.enable_cli" value="1"/>
Expand Down
21 changes: 21 additions & 0 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0"?>
<psalm
errorLevel="8"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
findUnusedBaselineEntry="true"
findUnusedCode="false"
>
<issueHandlers>
<ImplicitToStringCast errorLevel="suppress" />
<UndefinedConstant errorLevel="suppress" />
</issueHandlers>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
</psalm>
46 changes: 25 additions & 21 deletions src/BEditaClient.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
declare(strict_types=1);

/**
* BEdita, API-first content management framework
* Copyright 2023 Atlas Srl, ChannelWeb Srl, Chialab Srl
Expand All @@ -11,6 +12,8 @@

namespace BEdita\SDK;

use Exception;

/**
* BEdita API Client class
*/
Expand All @@ -31,8 +34,9 @@ public function authenticate(string $username, string $password): ?array
unset($headers['Authorization']);
$this->setDefaultHeaders($headers);
}
$body = (string)json_encode(compact('username', 'password') + ['grant_type' => 'password']);

return $this->post('/auth', json_encode(compact('username', 'password') + ['grant_type' => 'password']), ['Content-Type' => 'application/json']);
return $this->post('/auth', $body, ['Content-Type' => 'application/json']);
}

/**
Expand All @@ -51,73 +55,73 @@ public function getObjects(string $type = 'objects', ?array $query = null, ?arra
/**
* GET a single object of a given type
*
* @param int|string $id Object id
* @param string|int $id Object id
* @param string $type Object type name
* @param array|null $query Optional query string
* @param array|null $headers Custom request headers
* @return array|null Response in array format
*/
public function getObject($id, string $type = 'objects', ?array $query = null, ?array $headers = null): ?array
public function getObject(string|int $id, string $type = 'objects', ?array $query = null, ?array $headers = null): ?array
{
return $this->get(sprintf('/%s/%s', $type, $id), $query, $headers);
}

/**
* Get a list of related resources or objects
*
* @param int|string $id Resource id or object uname/id
* @param string|int $id Resource id or object uname/id
* @param string $type Type name
* @param string $relation Relation name
* @param array|null $query Optional query string
* @param array|null $headers Custom request headers
* @return array|null Response in array format
*/
public function getRelated($id, string $type, string $relation, ?array $query = null, ?array $headers = null): ?array
public function getRelated(string|int $id, string $type, string $relation, ?array $query = null, ?array $headers = null): ?array
{
return $this->get(sprintf('/%s/%s/%s', $type, $id, $relation), $query, $headers);
}

/**
* Add a list of related resources or objects
*
* @param int|string $id Resource id or object uname/id
* @param string|int $id Resource id or object uname/id
* @param string $type Type name
* @param string $relation Relation name
* @param array $data Related resources or objects to add, MUST contain id and type
* @param array|null $headers Custom request headers
* @return array|null Response in array format
*/
public function addRelated($id, string $type, string $relation, array $data, ?array $headers = null): ?array
public function addRelated(string|int $id, string $type, string $relation, array $data, ?array $headers = null): ?array
{
return $this->post(sprintf('/%s/%s/relationships/%s', $type, $id, $relation), json_encode(compact('data')), $headers);
}

/**
* Remove a list of related resources or objects
*
* @param int|string $id Resource id or object uname/id
* @param string|int $id Resource id or object uname/id
* @param string $type Type name
* @param string $relation Relation name
* @param array $data Related resources or objects to remove from relation
* @param array|null $headers Custom request headers
* @return array|null Response in array format
*/
public function removeRelated($id, string $type, string $relation, array $data, ?array $headers = null): ?array
public function removeRelated(string|int $id, string $type, string $relation, array $data, ?array $headers = null): ?array
{
return $this->delete(sprintf('/%s/%s/relationships/%s', $type, $id, $relation), json_encode(compact('data')), $headers);
}

/**
* Replace a list of related resources or objects: previuosly related are removed and replaced with these.
*
* @param int|string $id Object id
* @param string|int $id Object id
* @param string $type Object type name
* @param string $relation Relation name
* @param array $data Related resources or objects to insert
* @param array|null $headers Custom request headers
* @return array|null Response in array format
*/
public function replaceRelated($id, string $type, string $relation, array $data, ?array $headers = null): ?array
public function replaceRelated(string|int $id, string $type, string $relation, array $data, ?array $headers = null): ?array
{
return $this->patch(sprintf('/%s/%s/relationships/%s', $type, $id, $relation), json_encode(compact('data')), $headers);
}
Expand Down Expand Up @@ -170,11 +174,11 @@ public function saveObject(string $type, array $data, ?array $headers = null): ?
/**
* Delete an object (DELETE) => move to trashcan.
*
* @param int|string $id Object id
* @param string|int $id Object id
* @param string $type Object type name
* @return array|null Response in array format
*/
public function deleteObject($id, string $type): ?array
public function deleteObject(string|int $id, string $type): ?array
{
return $this->delete(sprintf('/%s/%s', $type, $id));
}
Expand All @@ -191,7 +195,7 @@ public function deleteObjects(array $ids, string $type = 'objects'): ?array
$response = null;
try {
$response = $this->delete(sprintf('/%s?ids=%s', $type, implode(',', $ids)));
} catch (\Exception $e) {
} catch (Exception $e) {
// fallback to delete one by one, to be retrocompatible
foreach ($ids as $id) {
$response = !empty($response) ? $response : $this->deleteObject($id, $type);
Expand All @@ -204,10 +208,10 @@ public function deleteObjects(array $ids, string $type = 'objects'): ?array
/**
* Remove an object => permanently remove object from trashcan.
*
* @param int|string $id Object id
* @param string|int $id Object id
* @return array|null Response in array format
*/
public function remove($id): ?array
public function remove(string|int $id): ?array
{
return $this->delete(sprintf('/trash/%s', $id));
}
Expand All @@ -223,7 +227,7 @@ public function removeObjects(array $ids): ?array
$response = null;
try {
$response = $this->delete(sprintf('/trash?ids=%s', implode(',', $ids)));
} catch (\Exception $e) {
} catch (Exception $e) {
// fallback to delete one by one, to be retrocompatible
foreach ($ids as $id) {
$response = !empty($response) ? $response : $this->remove($id);
Expand Down Expand Up @@ -270,7 +274,7 @@ public function upload(string $filename, string $filepath, ?array $headers = nul
* @return array|null Response in array format
* @throws \BEdita\SDK\BEditaClientException
*/
public function createMediaFromStream($streamId, string $type, array $body): ?array
public function createMediaFromStream(string $streamId, string $type, array $body): ?array
{
$id = $this->createMedia($type, $body);
$this->addStreamToMedia($streamId, $id, $type);
Expand Down Expand Up @@ -335,7 +339,7 @@ public function addStreamToMedia(string $streamId, string $id, string $type): vo
* @param array $query The query params for thumbs call.
* @return array|null Response in array format
*/
public function thumbs($id = null, $query = []): ?array
public function thumbs(?int $id = null, array $query = []): ?array
{
if (empty($id) && empty($query['ids'])) {
throw new BEditaClientException('Invalid empty id|ids for thumbs');
Expand Down Expand Up @@ -377,11 +381,11 @@ public function relationData(string $name): ?array
/**
* Restore object from trash
*
* @param int|string $id Object id
* @param string|int $id Object id
* @param string $type Object type name
* @return array|null Response in array format
*/
public function restoreObject($id, string $type): ?array
public function restoreObject(string|int $id, string $type): ?array
{
return $this->patch(
sprintf('/trash/%s', $id),
Expand Down
17 changes: 11 additions & 6 deletions src/BEditaClientException.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

/**
* BEdita, API-first content management framework
* Copyright 2018 ChannelWeb Srl, Chialab Srl
Expand All @@ -10,45 +12,48 @@

namespace BEdita\SDK;

use Exception;
use RuntimeException;

/**
* Network exception thrown by BEdita API Client.
*/
class BEditaClientException extends \RuntimeException
class BEditaClientException extends RuntimeException
{
/**
* Array of attributes that are passed in from the constructor, and
* made available in the view when a development error is displayed.
*
* @var array
*/
protected $attributes = [];
protected array $attributes = [];

/**
* Template string that has attributes sprintf()'ed into it.
*
* @var string
*/
protected $messageTemplate = '[%s] %s';
protected string $messageTemplate = '[%s] %s';

/**
* Default exception code
*
* @var int
*/
protected $defaultCode = 503;
protected int $defaultCode = 503;

/**
* Constructor.
*
* Allows you to create exceptions that are treated as framework errors and disabled
* when debug = 0.
*
* @param string|array $message Either the string of the error message, or an array of attributes
* @param array|string $message Either the string of the error message, or an array of attributes
* that are made available in the view, and sprintf()'d into Exception::$_messageTemplate
* @param int|null $code The code of the error, is also the HTTP status code for the error.
* @param \Exception|null $previous the previous exception.
*/
public function __construct($message = '', ?int $code = null, \Exception $previous = null)
public function __construct(array|string $message = '', ?int $code = null, ?Exception $previous = null)
{
if ($code === null) {
$code = $this->defaultCode;
Expand Down
Loading
Loading