Skip to content

Commit

Permalink
Merge pull request #10 from exonet/tsi/EXO-4666
Browse files Browse the repository at this point in the history
Add links navigation and getRecursive
  • Loading branch information
styxit committed Aug 7, 2020
2 parents 0266fc9 + 4c0e1b2 commit b9cf609
Show file tree
Hide file tree
Showing 23 changed files with 393 additions and 64 deletions.
8 changes: 2 additions & 6 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
preset: recommended

enabled:
- strict
- strict_param

disabled:
- align_double_arrow
- simplified_null_return
- align_double_arrow

15 changes: 11 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
language: php

php:
- 7.1
- 7.2
- 7.3
services:
- docker

cache:
directories:
- vendor
- $HOME/.composer/cache

before_install:
- docker pull php:7.1-cli
- docker pull php:7.2-cli
- docker pull php:7.3-cli
- docker pull php:7.4-cli

install:
- composer install

script:
- ./run-tests.sh
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ All notable changes to `exonet-api-php` will be documented in this file.
Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) principles.

## Unreleased
[Compare v2.2.0 - Unreleased](https://github.com/exonet/exonet-api-php/compare/v2.2.0...master)
[Compare v2.3.0 - Unreleased](https://github.com/exonet/exonet-api-php/compare/v2.3.0...master)

## [v2.3.0](https://github.com/exonet/exonet-api-php/releases/tag/v2.3.0) - 2020-08-07
[Compare v2.2.0 - v2.3.0](https://github.com/exonet/exonet-api-php/compare/v2.2.0...v2.3.0)
### Added
- Add the `total()` method to resource sets to get the total number of resources (and not only the number of resources in the current resource set).
- Add `nextPage`, `previousPage`, `firstPage` and `lastPage` methods to the `ApiResourceSet` for easy loading of paginated resource sets.
- Add a `getRecursive` method to the `Request` to get the resource set including recursively the resource sets from the following pages.

## [v2.2.0](https://github.com/exonet/exonet-api-php/releases/tag/v2.2.0) - 2019-11-19
[Compare v2.1.1 - v2.2.0](https://github.com/exonet/exonet-api-php/compare/v2.1.1...v2.2.0)
Expand Down
25 changes: 24 additions & 1 deletion docs/api_responses.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Using API Responses
There are two types of API responses upon a successful request. If a single resource is requested then an [`ApiResource`](api_resources.md) is
There are two types of API responses upon a successful request. If a single resource is requested then an [`ApiResource`](api_resources.md) is
returned, if multiple resources are requested then an `ApiResourceSet` is returned.

## The `ApiResourceSet` class
Expand All @@ -16,6 +16,29 @@ foreach ($certificates as $certificate) {
}
```

The get the number of items in a resource set, you can use one of the following methods:
```php
$certificates->count(); // Returns the number of resources in the current resource set.
$certificates->total(); // Returns the total number of resources in the resource set, ignoring pagination.
```

If `count != total` you can get the next/previous/first/last page by calling one of the pagination methods:
```php
// Get the next resource set:
$certificates->nextPage();

// Get the previous resource set:
$certificates->previousPage();

// Get the first resource set:
$certificates->firstPage();

// Get the last resource set:
$certificates->lastPage();
```

Each of this methods will return `null` if not available.

## The [`ApiResource`](api_resources.md) class
Each resource returned by the API is transformed to an [`ApiResource`](api_resources.md) instance. This makes it possible to have easy access
to the attributes, resourceType and ID of the resource. Each of these fields can be accessed as if it is a property on the class:
Expand Down
15 changes: 14 additions & 1 deletion docs/calls.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Making API calls
After the client has been initialised, use the `resource` method to define which type of resource you want to get from
After the client has been initialised, use the `resource` method to define which type of resource you want to get from
the API:

```php
Expand All @@ -24,6 +24,19 @@ After setting the parameters you can call the `->get()` method to retrieve the r
$certificates = $certificatesRequest->get();
```

It is also possible to get all resource sets recursively. The package will check the URL defined in `links.next` and as
long as the value is not `null` it will make an additional request and merge the results:

```php
$certificates = $certificatesRequest->getRecursive();
```

Please note that the `getRecursive` method respects pagination and filters. So the following example will get all
non-expired certificates, starting from page two in batches of ten:
```php
$certificates = $certificatesRequest->filter('expired', false)->page(2)->size(10)->getRecursive();
```

## Getting a single resource by ID
If you want to get a specific resource by its ID, use the `id()` method to define specify the ID of the resource:
```php
Expand Down
2 changes: 1 addition & 1 deletion examples/dns_record_post.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
// Show this message when there are no zones.
if (empty($zones)) {
echo 'There are no zones available.';
die();
exit();
}
$zone = $zones[0];

Expand Down
2 changes: 1 addition & 1 deletion examples/dns_zone_details.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
// Show this message when there are no zones.
if (empty($zones)) {
echo 'There are no zones available.';
die();
exit();
}
$zone = $zones[0];

Expand Down
2 changes: 1 addition & 1 deletion examples/ticket_details.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// Show this message when there are no tickets available.
if (empty($tickets)) {
echo 'There are no tickets available';
die();
exit();
}

$ticket = $tickets[0];
Expand Down
2 changes: 1 addition & 1 deletion examples/tickets.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* @param string $description The description of the list.
* @param ApiResourceSet $ticketList The resource set containing ticket resources.
*/
function renderTickets(string $description, ApiResourceSet $ticketList) : void
function renderTickets(string $description, ApiResourceSet $ticketList): void
{
echo sprintf("\n%s (%d)\n%s\n", $description, count($ticketList), str_repeat('-', strlen($description)));

Expand Down
55 changes: 55 additions & 0 deletions run-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
bold=$(tput bold)
red=$(tput setaf 1)
green=$(tput setaf 2)
normal=$(tput sgr0)
RESULTS=""
HAS_FAILED_TESTS=0

function run() {
PHP_VERSION=$1

echo ""
echo "--------------------------------------------"
echo "Testing with PHP ${bold}$PHP_VERSION${normal}"
echo "--------------------------------------------"

docker run \
-it \
--rm \
--name php"$PHP_VERSION" \
-v "$PWD":/usr/src \
-w /usr/src/ \
php:"$PHP_VERSION"-cli \
php ./vendor/bin/phpunit

if [ $? -eq 0 ]; then
RESULTS="$RESULTS\n${green}✓ PHP $PHP_VERSION${normal}"
else
HAS_FAILED_TESTS=1
RESULTS="$RESULTS\n${red}𐄂 PHP $PHP_VERSION${normal}"
fi

echo "(PHP $PHP_VERSION)"
}

# Get the arguments from the call (i.e. ./run-tests.sh 7.4)
SET_PHP_VERSION=$1

# If an arguments is given, only run that version.
if [ "$#" -eq 1 ]; then
run "$SET_PHP_VERSION"
else
# Run tests for different PHP 7 versions.
for phpversion in {1..4}; do
run "7.$phpversion"
RESULTS="$RESULTS\n"
done
fi

echo ""
echo "RESULTS"
echo "----------------------"
printf "$RESULTS"

exit $HAS_FAILED_TESTS

4 changes: 2 additions & 2 deletions src/Auth/AbstractAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ abstract class AbstractAuth
*
* @return string The access token.
*/
public function getToken() : string
public function getToken(): string
{
return $this->token;
}
Expand All @@ -29,7 +29,7 @@ public function getToken() : string
*
* @param string $token The access token.
*/
public function setToken(string $token) : void
public function setToken(string $token): void
{
$this->token = $token;
}
Expand Down
16 changes: 8 additions & 8 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Client implements LoggerAwareInterface
/**
* The version of this package. Used in the user-agent header.
*/
public const CLIENT_VERSION = 'v1.0.0';
public const CLIENT_VERSION = 'v2.3.0';

/**
* The API base URL.
Expand Down Expand Up @@ -75,7 +75,7 @@ public function __construct(?AbstractAuth $auth = null, ?string $apiUrl = null)
*
* @return Client The cache instance.
*/
public static function getInstance() : self
public static function getInstance(): self
{
if (!isset(self::$_instance)) {
self::$_instance = new self();
Expand All @@ -91,7 +91,7 @@ public static function getInstance() : self
*
* @return AbstractAuth The auth instance.
*/
public function getAuth() : AbstractAuth
public function getAuth(): AbstractAuth
{
if ($this->auth === null) {
$this->log()->error('No authentication method set.');
Expand All @@ -109,7 +109,7 @@ public function getAuth() : AbstractAuth
*
* @return self The current Client instance.
*/
public function setAuth(AbstractAuth $auth) : self
public function setAuth(AbstractAuth $auth): self
{
$this->auth = $auth;

Expand All @@ -121,7 +121,7 @@ public function setAuth(AbstractAuth $auth) : self
*
* @return string The API URL to connect to.
*/
public function getApiUrl() : string
public function getApiUrl(): string
{
return $this->apiUrl;
}
Expand All @@ -133,7 +133,7 @@ public function getApiUrl() : string
*
* @return self The current Client instance.
*/
public function setApiUrl(string $apiUrl) : self
public function setApiUrl(string $apiUrl): self
{
if (substr($apiUrl, -1) !== '/') {
$apiUrl .= '/';
Expand All @@ -149,7 +149,7 @@ public function setApiUrl(string $apiUrl) : self
*
* @return LoggerInterface The log instance.
*/
public function log() : LoggerInterface
public function log(): LoggerInterface
{
if ($this->logger === null) {
// If there's no logger set, use the NullLogger.
Expand All @@ -166,7 +166,7 @@ public function log() : LoggerInterface
*
* @return self The current Client instance.
*/
public function setLogger(LoggerInterface $log) : self
public function setLogger(LoggerInterface $log): self
{
$this->logger = $log;

Expand Down
Loading

0 comments on commit b9cf609

Please sign in to comment.