@ -0,0 +1,3 @@ | |||
vendor | |||
.idea | |||
.vscode |
@ -0,0 +1,34 @@ | |||
# Pirectus - a small Client for Directus 9 | |||
This Client Supports Directus 9, it is written in PHP and use Guzzle for handling requests. | |||
In this Version | |||
## Installation | |||
```php | |||
``` | |||
## Quickstart | |||
```php | |||
require('vendor/autoload.php'); | |||
use Pirectus\Pirectus; | |||
use Pirectus\Auth\TokenAuth; | |||
$pirectus = new Pirectus('<directus-url>', [ | |||
'auth' => new TokenAuth('<directus-authtoken>') | |||
]); | |||
``` | |||
```php | |||
$results = $pirectus | |||
->items('pages') | |||
->fields(['id', 'title', 'content']) | |||
->filter([ | |||
'status' => ['_eq' => 'published'] | |||
]) | |||
->find(); | |||
``` |
@ -0,0 +1,22 @@ | |||
{ | |||
"name": "tentakelfabrik/pirectus", | |||
"version": "0.1.0", | |||
"type": "library", | |||
"license": "MIT", | |||
"authors": [ | |||
{ | |||
"name": "Björn Hase", | |||
"email": "me@tentakelfabrik.de", | |||
"homepage": "https://tentakelfabrik.de" | |||
} | |||
], | |||
"minimum-stability": "stable", | |||
"autoload": { | |||
"psr-4": { | |||
"Pirectus\\": "src/" | |||
} | |||
}, | |||
"require": { | |||
"guzzlehttp/guzzle": "^7.0" | |||
} | |||
} |
@ -0,0 +1,525 @@ | |||
{ | |||
"_readme": [ | |||
"This file locks the dependencies of your project to a known state", | |||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", | |||
"This file is @generated automatically" | |||
], | |||
"content-hash": "16a7f8e2a84fcbee8da3fa43f25eb246", | |||
"packages": [ | |||
{ | |||
"name": "guzzlehttp/guzzle", | |||
"version": "7.3.0", | |||
"source": { | |||
"type": "git", | |||
"url": "https://github.com/guzzle/guzzle.git", | |||
"reference": "7008573787b430c1c1f650e3722d9bba59967628" | |||
}, | |||
"dist": { | |||
"type": "zip", | |||
"url": "https://api.github.com/repos/guzzle/guzzle/zipball/7008573787b430c1c1f650e3722d9bba59967628", | |||
"reference": "7008573787b430c1c1f650e3722d9bba59967628", | |||
"shasum": "" | |||
}, | |||
"require": { | |||
"ext-json": "*", | |||
"guzzlehttp/promises": "^1.4", | |||
"guzzlehttp/psr7": "^1.7 || ^2.0", | |||
"php": "^7.2.5 || ^8.0", | |||
"psr/http-client": "^1.0" | |||
}, | |||
"provide": { | |||
"psr/http-client-implementation": "1.0" | |||
}, | |||
"require-dev": { | |||
"bamarni/composer-bin-plugin": "^1.4.1", | |||
"ext-curl": "*", | |||
"php-http/client-integration-tests": "^3.0", | |||
"phpunit/phpunit": "^8.5.5 || ^9.3.5", | |||
"psr/log": "^1.1" | |||
}, | |||
"suggest": { | |||
"ext-curl": "Required for CURL handler support", | |||
"ext-intl": "Required for Internationalized Domain Name (IDN) support", | |||
"psr/log": "Required for using the Log middleware" | |||
}, | |||
"type": "library", | |||
"extra": { | |||
"branch-alias": { | |||
"dev-master": "7.3-dev" | |||
} | |||
}, | |||
"autoload": { | |||
"psr-4": { | |||
"GuzzleHttp\\": "src/" | |||
}, | |||
"files": [ | |||
"src/functions_include.php" | |||
] | |||
}, | |||
"notification-url": "https://packagist.org/downloads/", | |||
"license": [ | |||
"MIT" | |||
], | |||
"authors": [ | |||
{ | |||
"name": "Michael Dowling", | |||
"email": "mtdowling@gmail.com", | |||
"homepage": "https://github.com/mtdowling" | |||
}, | |||
{ | |||
"name": "Márk Sági-Kazár", | |||
"email": "mark.sagikazar@gmail.com", | |||
"homepage": "https://sagikazarmark.hu" | |||
} | |||
], | |||
"description": "Guzzle is a PHP HTTP client library", | |||
"homepage": "http://guzzlephp.org/", | |||
"keywords": [ | |||
"client", | |||
"curl", | |||
"framework", | |||
"http", | |||
"http client", | |||
"psr-18", | |||
"psr-7", | |||
"rest", | |||
"web service" | |||
], | |||
"support": { | |||
"issues": "https://github.com/guzzle/guzzle/issues", | |||
"source": "https://github.com/guzzle/guzzle/tree/7.3.0" | |||
}, | |||
"funding": [ | |||
{ | |||
"url": "https://github.com/GrahamCampbell", | |||
"type": "github" | |||
}, | |||
{ | |||
"url": "https://github.com/Nyholm", | |||
"type": "github" | |||
}, | |||
{ | |||
"url": "https://github.com/alexeyshockov", | |||
"type": "github" | |||
}, | |||
{ | |||
"url": "https://github.com/gmponos", | |||
"type": "github" | |||
} | |||
], | |||
"time": "2021-03-23T11:33:13+00:00" | |||
}, | |||
{ | |||
"name": "guzzlehttp/promises", | |||
"version": "1.5.0", | |||
"source": { | |||
"type": "git", | |||
"url": "https://github.com/guzzle/promises.git", | |||
"reference": "136a635e2b4a49b9d79e9c8fee267ffb257fdba0" | |||
}, | |||
"dist": { | |||
"type": "zip", | |||
"url": "https://api.github.com/repos/guzzle/promises/zipball/136a635e2b4a49b9d79e9c8fee267ffb257fdba0", | |||
"reference": "136a635e2b4a49b9d79e9c8fee267ffb257fdba0", | |||
"shasum": "" | |||
}, | |||
"require": { | |||
"php": ">=5.5" | |||
}, | |||
"require-dev": { | |||
"symfony/phpunit-bridge": "^4.4 || ^5.1" | |||
}, | |||
"type": "library", | |||
"extra": { | |||
"branch-alias": { | |||
"dev-master": "1.5-dev" | |||
} | |||
}, | |||
"autoload": { | |||
"psr-4": { | |||
"GuzzleHttp\\Promise\\": "src/" | |||
}, | |||
"files": [ | |||
"src/functions_include.php" | |||
] | |||
}, | |||
"notification-url": "https://packagist.org/downloads/", | |||
"license": [ | |||
"MIT" | |||
], | |||
"authors": [ | |||
{ | |||
"name": "Graham Campbell", | |||
"email": "hello@gjcampbell.co.uk", | |||
"homepage": "https://github.com/GrahamCampbell" | |||
}, | |||
{ | |||
"name": "Michael Dowling", | |||
"email": "mtdowling@gmail.com", | |||
"homepage": "https://github.com/mtdowling" | |||
}, | |||
{ | |||
"name": "Tobias Nyholm", | |||
"email": "tobias.nyholm@gmail.com", | |||
"homepage": "https://github.com/Nyholm" | |||
}, | |||
{ | |||
"name": "Tobias Schultze", | |||
"email": "webmaster@tubo-world.de", | |||
"homepage": "https://github.com/Tobion" | |||
} | |||
], | |||
"description": "Guzzle promises library", | |||
"keywords": [ | |||
"promise" | |||
], | |||
"support": { | |||
"issues": "https://github.com/guzzle/promises/issues", | |||
"source": "https://github.com/guzzle/promises/tree/1.5.0" | |||
}, | |||
"funding": [ | |||
{ | |||
"url": "https://github.com/GrahamCampbell", | |||
"type": "github" | |||
}, | |||
{ | |||
"url": "https://github.com/Nyholm", | |||
"type": "github" | |||
}, | |||
{ | |||
"url": "https://tidelift.com/funding/github/packagist/guzzlehttp/promises", | |||
"type": "tidelift" | |||
} | |||
], | |||
"time": "2021-10-07T13:05:22+00:00" | |||
}, | |||
{ | |||
"name": "guzzlehttp/psr7", | |||
"version": "2.1.0", | |||
"source": { | |||
"type": "git", | |||
"url": "https://github.com/guzzle/psr7.git", | |||
"reference": "089edd38f5b8abba6cb01567c2a8aaa47cec4c72" | |||
}, | |||
"dist": { | |||
"type": "zip", | |||
"url": "https://api.github.com/repos/guzzle/psr7/zipball/089edd38f5b8abba6cb01567c2a8aaa47cec4c72", | |||
"reference": "089edd38f5b8abba6cb01567c2a8aaa47cec4c72", | |||
"shasum": "" | |||
}, | |||
"require": { | |||
"php": "^7.2.5 || ^8.0", | |||
"psr/http-factory": "^1.0", | |||
"psr/http-message": "^1.0", | |||
"ralouphie/getallheaders": "^3.0" | |||
}, | |||
"provide": { | |||
"psr/http-factory-implementation": "1.0", | |||
"psr/http-message-implementation": "1.0" | |||
}, | |||
"require-dev": { | |||
"bamarni/composer-bin-plugin": "^1.4.1", | |||
"http-interop/http-factory-tests": "^0.9", | |||
"phpunit/phpunit": "^8.5.8 || ^9.3.10" | |||
}, | |||
"suggest": { | |||
"laminas/laminas-httphandlerrunner": "Emit PSR-7 responses" | |||
}, | |||
"type": "library", | |||
"extra": { | |||
"branch-alias": { | |||
"dev-master": "2.1-dev" | |||
} | |||
}, | |||
"autoload": { | |||
"psr-4": { | |||
"GuzzleHttp\\Psr7\\": "src/" | |||
} | |||
}, | |||
"notification-url": "https://packagist.org/downloads/", | |||
"license": [ | |||
"MIT" | |||
], | |||
"authors": [ | |||
{ | |||
"name": "Graham Campbell", | |||
"email": "hello@gjcampbell.co.uk", | |||
"homepage": "https://github.com/GrahamCampbell" | |||
}, | |||
{ | |||
"name": "Michael Dowling", | |||
"email": "mtdowling@gmail.com", | |||
"homepage": "https://github.com/mtdowling" | |||
}, | |||
{ | |||
"name": "George Mponos", | |||
"email": "gmponos@gmail.com", | |||
"homepage": "https://github.com/gmponos" | |||
}, | |||
{ | |||
"name": "Tobias Nyholm", | |||
"email": "tobias.nyholm@gmail.com", | |||
"homepage": "https://github.com/Nyholm" | |||
}, | |||
{ | |||
"name": "Márk Sági-Kazár", | |||
"email": "mark.sagikazar@gmail.com", | |||
"homepage": "https://github.com/sagikazarmark" | |||
}, | |||
{ | |||
"name": "Tobias Schultze", | |||
"email": "webmaster@tubo-world.de", | |||
"homepage": "https://github.com/Tobion" | |||
}, | |||
{ | |||
"name": "Márk Sági-Kazár", | |||
"email": "mark.sagikazar@gmail.com", | |||
"homepage": "https://sagikazarmark.hu" | |||
} | |||
], | |||
"description": "PSR-7 message implementation that also provides common utility methods", | |||
"keywords": [ | |||
"http", | |||
"message", | |||
"psr-7", | |||
"request", | |||
"response", | |||
"stream", | |||
"uri", | |||
"url" | |||
], | |||
"support": { | |||
"issues": "https://github.com/guzzle/psr7/issues", | |||
"source": "https://github.com/guzzle/psr7/tree/2.1.0" | |||
}, | |||
"funding": [ | |||
{ | |||
"url": "https://github.com/GrahamCampbell", | |||
"type": "github" | |||
}, | |||
{ | |||
"url": "https://github.com/Nyholm", | |||
"type": "github" | |||
}, | |||
{ | |||
"url": "https://tidelift.com/funding/github/packagist/guzzlehttp/psr7", | |||
"type": "tidelift" | |||
} | |||
], | |||
"time": "2021-10-06T17:43:30+00:00" | |||
}, | |||
{ | |||
"name": "psr/http-client", | |||
"version": "1.0.1", | |||
"source": { | |||
"type": "git", | |||
"url": "https://github.com/php-fig/http-client.git", | |||
"reference": "2dfb5f6c5eff0e91e20e913f8c5452ed95b86621" | |||
}, | |||
"dist": { | |||
"type": "zip", | |||
"url": "https://api.github.com/repos/php-fig/http-client/zipball/2dfb5f6c5eff0e91e20e913f8c5452ed95b86621", | |||
"reference": "2dfb5f6c5eff0e91e20e913f8c5452ed95b86621", | |||
"shasum": "" | |||
}, | |||
"require": { | |||
"php": "^7.0 || ^8.0", | |||
"psr/http-message": "^1.0" | |||
}, | |||
"type": "library", | |||
"extra": { | |||
"branch-alias": { | |||
"dev-master": "1.0.x-dev" | |||
} | |||
}, | |||
"autoload": { | |||
"psr-4": { | |||
"Psr\\Http\\Client\\": "src/" | |||
} | |||
}, | |||
"notification-url": "https://packagist.org/downloads/", | |||
"license": [ | |||
"MIT" | |||
], | |||
"authors": [ | |||
{ | |||
"name": "PHP-FIG", | |||
"homepage": "http://www.php-fig.org/" | |||
} | |||
], | |||
"description": "Common interface for HTTP clients", | |||
"homepage": "https://github.com/php-fig/http-client", | |||
"keywords": [ | |||
"http", | |||
"http-client", | |||
"psr", | |||
"psr-18" | |||
], | |||
"support": { | |||
"source": "https://github.com/php-fig/http-client/tree/master" | |||
}, | |||
"time": "2020-06-29T06:28:15+00:00" | |||
}, | |||
{ | |||
"name": "psr/http-factory", | |||
"version": "1.0.1", | |||
"source": { | |||
"type": "git", | |||
"url": "https://github.com/php-fig/http-factory.git", | |||
"reference": "12ac7fcd07e5b077433f5f2bee95b3a771bf61be" | |||
}, | |||
"dist": { | |||
"type": "zip", | |||
"url": "https://api.github.com/repos/php-fig/http-factory/zipball/12ac7fcd07e5b077433f5f2bee95b3a771bf61be", | |||
"reference": "12ac7fcd07e5b077433f5f2bee95b3a771bf61be", | |||
"shasum": "" | |||
}, | |||
"require": { | |||
"php": ">=7.0.0", | |||
"psr/http-message": "^1.0" | |||
}, | |||
"type": "library", | |||
"extra": { | |||
"branch-alias": { | |||
"dev-master": "1.0.x-dev" | |||
} | |||
}, | |||
"autoload": { | |||
"psr-4": { | |||
"Psr\\Http\\Message\\": "src/" | |||
} | |||
}, | |||
"notification-url": "https://packagist.org/downloads/", | |||
"license": [ | |||
"MIT" | |||
], | |||
"authors": [ | |||
{ | |||
"name": "PHP-FIG", | |||
"homepage": "http://www.php-fig.org/" | |||
} | |||
], | |||
"description": "Common interfaces for PSR-7 HTTP message factories", | |||
"keywords": [ | |||
"factory", | |||
"http", | |||
"message", | |||
"psr", | |||
"psr-17", | |||
"psr-7", | |||
"request", | |||
"response" | |||
], | |||
"support": { | |||
"source": "https://github.com/php-fig/http-factory/tree/master" | |||
}, | |||
"time": "2019-04-30T12:38:16+00:00" | |||
}, | |||
{ | |||
"name": "psr/http-message", | |||
"version": "1.0.1", | |||
"source": { | |||
"type": "git", | |||
"url": "https://github.com/php-fig/http-message.git", | |||
"reference": "f6561bf28d520154e4b0ec72be95418abe6d9363" | |||
}, | |||
"dist": { | |||
"type": "zip", | |||
"url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363", | |||
"reference": "f6561bf28d520154e4b0ec72be95418abe6d9363", | |||
"shasum": "" | |||
}, | |||
"require": { | |||
"php": ">=5.3.0" | |||
}, | |||
"type": "library", | |||
"extra": { | |||
"branch-alias": { | |||
"dev-master": "1.0.x-dev" | |||
} | |||
}, | |||
"autoload": { | |||
"psr-4": { | |||
"Psr\\Http\\Message\\": "src/" | |||
} | |||
}, | |||
"notification-url": "https://packagist.org/downloads/", | |||
"license": [ | |||
"MIT" | |||
], | |||
"authors": [ | |||
{ | |||
"name": "PHP-FIG", | |||
"homepage": "http://www.php-fig.org/" | |||
} | |||
], | |||
"description": "Common interface for HTTP messages", | |||
"homepage": "https://github.com/php-fig/http-message", | |||
"keywords": [ | |||
"http", | |||
"http-message", | |||
"psr", | |||
"psr-7", | |||
"request", | |||
"response" | |||
], | |||
"support": { | |||
"source": "https://github.com/php-fig/http-message/tree/master" | |||
}, | |||
"time": "2016-08-06T14:39:51+00:00" | |||
}, | |||
{ | |||
"name": "ralouphie/getallheaders", | |||
"version": "3.0.3", | |||
"source": { | |||
"type": "git", | |||
"url": "https://github.com/ralouphie/getallheaders.git", | |||
"reference": "120b605dfeb996808c31b6477290a714d356e822" | |||
}, | |||
"dist": { | |||
"type": "zip", | |||
"url": "https://api.github.com/repos/ralouphie/getallheaders/zipball/120b605dfeb996808c31b6477290a714d356e822", | |||
"reference": "120b605dfeb996808c31b6477290a714d356e822", | |||
"shasum": "" | |||
}, | |||
"require": { | |||
"php": ">=5.6" | |||
}, | |||
"require-dev": { | |||
"php-coveralls/php-coveralls": "^2.1", | |||
"phpunit/phpunit": "^5 || ^6.5" | |||
}, | |||
"type": "library", | |||
"autoload": { | |||
"files": [ | |||
"src/getallheaders.php" | |||
] | |||
}, | |||
"notification-url": "https://packagist.org/downloads/", | |||
"license": [ | |||
"MIT" | |||
], | |||
"authors": [ | |||
{ | |||
"name": "Ralph Khattar", | |||
"email": "ralph.khattar@gmail.com" | |||
} | |||
], | |||
"description": "A polyfill for getallheaders.", | |||
"support": { | |||
"issues": "https://github.com/ralouphie/getallheaders/issues", | |||
"source": "https://github.com/ralouphie/getallheaders/tree/develop" | |||
}, | |||
"time": "2019-03-08T08:55:37+00:00" | |||
} | |||
], | |||
"packages-dev": [], | |||
"aliases": [], | |||
"minimum-stability": "stable", | |||
"stability-flags": [], | |||
"prefer-stable": false, | |||
"prefer-lowest": false, | |||
"platform": [], | |||
"platform-dev": [], | |||
"plugin-api-version": "2.1.0" | |||
} |
@ -0,0 +1,40 @@ | |||
<?php | |||
namespace Pirectus\Auth; | |||
/** | |||
* | |||
* | |||
* @author Björn Hase | |||
* @license http://opensource.org/licenses/MIT The MIT License | |||
* @link https://gitea.tentakelfabrik.de/tentakelfabrik/pirectus Gitea Repository | |||
* | |||
*/ | |||
class TokenAuth | |||
{ | |||
/** */ | |||
private $token = NULL; | |||
/** | |||
* setting token | |||
* | |||
* @param String $token | |||
* | |||
*/ | |||
public function __construct(String $token) | |||
{ | |||
$this->token = $token; | |||
} | |||
/** | |||
* getting token | |||
* | |||
* @return String | |||
* | |||
*/ | |||
public function getToken() | |||
{ | |||
return $this->token; | |||
} | |||
} |
@ -0,0 +1,53 @@ | |||
<?php | |||
namespace Pirectus; | |||
use Pirectus\QueryBuilder\ItemsQueryBuilder; | |||
/** | |||
* Client for Directus Api 9 | |||
* | |||
* @author Björn Hase, Tentakelfabrik | |||
* @license http://opensource.org/licenses/MIT The MIT License | |||
* @link https://gitea.tentakelfabrik.de/tentakelfabrik/pirectus | |||
* | |||
*/ | |||
class Pirectus | |||
{ | |||
/** url of api */ | |||
private $url; | |||
/** options for client */ | |||
private $options; | |||
/** | |||
* | |||
* | |||
* @param string $url | |||
* @param Array $options | |||
* | |||
*/ | |||
public function __construct(string $url, array $options = []) | |||
{ | |||
$this->url = $url; | |||
$this->options = $options; | |||
} | |||
/** | |||
* set name for items-collection and create ItemsQueryBuilder | |||
* | |||
* @param string $name | |||
* @return \Pirectus\ItemsQueryBuilder | |||
* | |||
*/ | |||
public function items(string $name) | |||
{ | |||
$query = [ | |||
'endpoint' => '/items/'.$name, | |||
'parameters' => [] | |||
]; | |||
return new ItemsQueryBuilder($query, $this->url, $this->options); | |||
} | |||
} |
@ -0,0 +1,104 @@ | |||
<?php | |||
namespace Pirectus\QueryBuilder; | |||
use Pirectus\Request; | |||
/** | |||
* Abstract Builder for Query to Directus | |||
* | |||
* @author Björn Hase, Tentakelfabrik | |||
* @license http://opensource.org/licenses/MIT The MIT License | |||
* @link https://gitea.tentakelfabrik.de/tentakelfabrik/pirectus | |||
* | |||
*/ | |||
abstract class AbstractQueryBuilder | |||
{ | |||
/** query ad array */ | |||
protected $query; | |||
/** url to directus api */ | |||
protected $url; | |||
/** request object with guzzle/client */ | |||
protected $request; | |||
/** | |||
* | |||
* | |||
* @param array $query | |||
* @param array $url | |||
* @param array $options | |||
* | |||
*/ | |||
public function __construct(array $query, string $url, array $options) | |||
{ | |||
$this->query = $query; | |||
$this->url = $url; | |||
$this->request = new Request($url, $options); | |||
} | |||
/** | |||
* request get | |||
* | |||
* | |||
* @return array | |||
* | |||
*/ | |||
public function find() | |||
{ | |||
$results = $this->request->get($this->query); | |||
return $results; | |||
} | |||
/** | |||
* request get, getting first result | |||
* | |||
* | |||
* @return array | |||
* | |||
*/ | |||
public function findOne() | |||
{ | |||
$results = $this->request->get($this->query); | |||
// if result has data, reduce to one | |||
if (isset($results['data']) && isset($results['data'][0])) { | |||
$results['data'] = $results['data'][0]; | |||
} | |||
return $result; | |||
} | |||
/** | |||
* request post, send data to create | |||
* | |||
* | |||
* @param array $data | |||
* @return array | |||
* | |||
*/ | |||
public function post(array $data) | |||
{ | |||
$response = $this->request->post($this->query, $data); | |||
return $response; | |||
} | |||
/** | |||
* request patch, send data to update | |||
* | |||
* | |||
* @param string $id | |||
* @param array $data | |||
* @return | |||
* | |||
*/ | |||
public function patch(string $id, array $data) | |||
{ | |||
$response = $this->request->patch($id, $this->query, $data); | |||
return $response; | |||
} | |||
} |
@ -0,0 +1,242 @@ | |||
<?php | |||
namespace Pirectus\QueryBuilder; | |||
use Pirectus\QueryBuilder\AbstractQueryBuilder; | |||
/** | |||
* Building Query for Items | |||
* | |||
* | |||
* @author Björn Hase, Tentakelfabrik | |||
* @license http://opensource.org/licenses/MIT The MIT License | |||
* @link https://gitea.tentakelfabrik.de/tentakelfabrik/pirectus | |||
* | |||
*/ | |||
class ItemsQueryBuilder extends AbstractQueryBuilder | |||
{ | |||
/** | |||
* set fields | |||
* | |||
* @param array $fields | |||
* @return \Pirectus\ItemsQueryBuilder | |||
* | |||
*/ | |||
public function fields(array $fields) | |||
{ | |||
$this->query['parameters']['fields'] = $fields; | |||
return $this; | |||
} | |||
/** | |||
* adding fields and merge | |||
* | |||
* @param array $fields | |||
* @return \Pirectus\ItemsQueryBuilder | |||
* | |||
*/ | |||
public function addFields(array $fields) | |||
{ | |||
$this->fillParameter('fields'); | |||
$this->query['parameters']['fields'] = array_merge($this->query['parameters']['fields'], $fields); | |||
return $this; | |||
} | |||
/** | |||
* set filter | |||
* | |||
* | |||
* @param array $filter | |||
* @return \Pirectus\ItemsQueryBuilder | |||
* | |||
*/ | |||
public function filter(array $filter) | |||
{ | |||
$this->query['parameters']['filter'] = $filter; | |||
return $this; | |||
} | |||
/** | |||
* adding filter and merge | |||
* | |||
* | |||
* @param array $filter | |||
* @return \Pirectus\ItemsQueryBuilder | |||
* | |||
*/ | |||
public function addFilter(array $filter) | |||
{ | |||
$this->fillParameter('filter'); | |||
$this->query['parameters']['filter'] = array_merge_recursive($this->query['parameters']['filter'], $filter); | |||
return $this; | |||
} | |||
/** | |||
* set limit | |||
* | |||
* @param int $value | |||
* @return \Pirectus\ItemsQueryBuilder | |||
* | |||
*/ | |||
public function limit(int $value) | |||
{ | |||
$this->query['parameters']['limit'] = $value; | |||
return $this; | |||
} | |||
/** | |||
* add offset | |||
* | |||
* @param int $value | |||
* @return \Pirectus\ItemsQueryBuilder | |||
* | |||
*/ | |||
public function offset(int $value) | |||
{ | |||
$this->query['parameters']['offset'] = $value; | |||
return $this; | |||
} | |||
/** | |||
* set GroupBy | |||
* | |||
* @param array $field | |||
* @return \Pirectus\ItemsQueryBuilder | |||
* | |||
*/ | |||
public function groupBy(array $groupBy) | |||
{ | |||
$this->query['parameters']['groupBy'] = $groupBy; | |||
return $this; | |||
} | |||
/** | |||
* add offset | |||
* | |||
* @param string $field | |||
* @return \Pirectus\ItemsQueryBuilder | |||
* | |||
*/ | |||
public function addGroupBy(string $field) | |||
{ | |||
$this->fillParameter('groupBy'); | |||
$this->query['parameters']['groupBy'][] = $field; | |||
return $this; | |||
} | |||
/** | |||
* set aggregate | |||
* | |||
* count, countDistinct, sum, sumDistinct, | |||
* avg, avgDistinct, min, max | |||
* | |||
* @param string $aggregate | |||
* @param string $field | |||
* @return \Pirectus\ItemsQueryBuilder | |||
* | |||
*/ | |||
public function aggregate(string $aggregate, string $field) | |||
{ | |||
$this->fillParameter('aggregate'); | |||
$this->query['parameters']['aggregate'][$aggregate] = $field; | |||
return $this; | |||
} | |||
/** | |||
* sort | |||
* | |||
* [ <field-name>, -<field-name> ] | |||
* | |||
* @param array $field | |||
* @return \Pirectus\ItemsQueryBuilder | |||
* | |||
*/ | |||
public function sort(array $sort) | |||
{ | |||
$this->query['parameters']['sort'] = $sort; | |||
return $this; | |||
} | |||
/** | |||
* add sort and merge | |||
* | |||
* [ <field-name>, -<field-name> ] | |||
* | |||
* @param array $field | |||
* @return \Pirectus\ItemsQueryBuilder | |||
* | |||
*/ | |||
public function addSort(array $sort) | |||
{ | |||
$this->fillParameter('sort'); | |||
$this->query['parameters']['sort'] = array_merge($this->query['parameters']['sort'], $sort); | |||
return $this; | |||
} | |||
/** | |||
* search | |||
* | |||
* @param string $value | |||
* @return \Pirectus\ItemsQueryBuilder | |||
* | |||
*/ | |||
public function search(string $value) | |||
{ | |||
$this->query['parameters']['search'] = $value; | |||
return $this; | |||
} | |||
/** | |||
* meta | |||
* | |||
* total_count, filter_count, * | |||
* | |||
* @param string $value | |||
* @return \Pirectus\ItemsQueryBuilder | |||
* | |||
*/ | |||
public function meta(string $value) | |||
{ | |||
$this->query['parameters']['meta'] = $value; | |||
return $this; | |||
} | |||
/** | |||
* set aliases | |||
* | |||
* | |||
* @param string $value | |||
* @return \Pirectus\ItemsQueryBuilder | |||
* | |||
*/ | |||
public function aliases(string $field, string $alias) | |||
{ | |||
$this->query['parameters']['alias'][$field] = $alias; | |||
return $this; | |||
} | |||
/** | |||
* if fields not set, add empty array | |||
* | |||
* @param string $field | |||
* | |||
*/ | |||
private function fill(string $field) | |||
{ | |||
if (!isset($this->query['parameters'][$field])) { | |||
$this->query['parameters'][$field] = []; | |||
} | |||
} | |||
} |
@ -0,0 +1,187 @@ | |||
<?php | |||
namespace Pirectus; | |||
use Pirectus\Auth\TokenAuth; | |||
use GuzzleHttp\Client; | |||
/** | |||
* Crete Requests, Parse Parameters and Response with Guzzle to Api | |||
* | |||
* | |||
* @author Björn Hase, Tentakelfabrik | |||
* @license http://opensource.org/licenses/MIT The MIT License | |||
* @link https://gitea.tentakelfabrik.de/tentakelfabrik/pirectus | |||
* | |||
*/ | |||
class Request | |||
{ | |||
/** client guzzle */ | |||
private $client; | |||
/** options */ | |||
private $options; | |||
/** headers */ | |||
private $headers = [ | |||
'Accept' => 'application/json', | |||
'Content-Type' => 'application/json' | |||
]; | |||
/** | |||
* | |||
* | |||
* @param string $url | |||
* @param array $options | |||
* | |||
*/ | |||
public function __construct(atring $url, array $options) | |||
{ | |||
$this->options = $options; | |||
// create client with base url | |||
$this->client = new Client([ | |||
'base_uri' => $url | |||
]); | |||
} | |||
/** | |||
* building http headers for request | |||
* | |||
* @return array | |||
* | |||
*/ | |||
public function buildHeaders() | |||
{ | |||
if (isset($this->options['auth']) && $this->options['auth'] instanceof TokenAuth) { | |||
$headers = array_merge($this->headers, [ | |||
'Authorization' => 'Bearer '.$this->options['auth']->getToken() | |||
]); | |||
} | |||
return $headers; | |||
} | |||
/** | |||
* build parameters for query | |||
* | |||
* @param array $query | |||
* @return string | |||
* | |||
*/ | |||
public function buildParameters(Array $query) | |||
{ | |||
// | |||
$parameters = []; | |||
// | |||
$result = ''; | |||
if (isset($query['parameters']['filter'])) { | |||
$parameters['filter'] = json_encode($query['parameters']['filter']); | |||
} | |||
if (isset($query['parameters']['fields'])) { | |||
$parameters['fields'] = implode(',', $query['parameters']['fields']); | |||
} | |||
if (isset($query['parameters']['limit'])) { | |||
$parameters['limit'] = intval($query['parameters']['limit']); | |||
} | |||
if (isset($query['parameters']['offset'])) { | |||
$parameters['offset'] = intval($query['parameters']['offset']); | |||
} | |||
if (isset($query['parameters']['sort'])) { | |||
$parameters['sort'] = implode(',', $query['parameters']['sort']); | |||
} | |||
if (isset($query['parameters']['meta'])) { | |||
$parameters['meta'] = $query['parameters']['meta']; | |||
} | |||
if (isset($query['parameters']['search'])) { | |||
$parameters['search'] = $query['parameters']['search']; | |||
} | |||
if (isset($query['parameters']['groupBy'])) { | |||
$parameters['groupBy'] = intval($query['parameters']['sort']); | |||
} | |||
if (isset($query['parameters']['aggregate'])) { | |||
$parameters['meta'] = $query['parameters']['aggregate']; | |||
} | |||
if (isset($query['parameters']['alias'])) { | |||
$parameters['alias'] = $query['parameters']['alias']; | |||
} | |||
if (count($parameters) > 0) { | |||
$result = http_build_query($parameters); | |||
} | |||
return $result; | |||
} | |||
/** | |||
* get entites in Directus Api | |||
* | |||
* @param array $query | |||
* @return mixed | |||
* | |||
*/ | |||
public function get(array $query) | |||
{ | |||
$response = $this->client->request('get', $query['endpoint'], [ | |||
'query' => $this->buildParameters($query), | |||
'headers' => $this->buildHeaders() | |||
]); | |||
$results = json_decode($response->getBody(), true); | |||
return $results; | |||
} | |||
/** | |||
* post single entity in Directus Api | |||
* | |||
* @param array $query | |||
* @param array $data | |||
* @return mixed | |||
* | |||
*/ | |||
public function post(array $query, array $data) | |||
{ | |||
$response = $this->client->request('post', $query['endpoint'], [ | |||
'headers' => $this->buildHeaders(), | |||
'body' => json_encode($data) | |||
]); | |||
$results = json_decode($response->getBody(), true); | |||
return $results; | |||
} | |||
/** | |||
* patch single entity in Directus Api | |||
* | |||
* @param string $id | |||
* @param array $query | |||
* @param array $data | |||
* @return mixed | |||
* | |||
*/ | |||
public function patch(string $id, array $query, array $data) | |||
{ | |||
$response = $this->client->request('patch', $query['endpoint'].'/'.$id, [ | |||
'headers' => $this->buildHeaders(), | |||
'body' => json_encode($data) | |||
]); | |||
$results = json_decode($response->getBody(), true); | |||
return $results; | |||
} | |||
} |