Skip to content

Commit

Permalink
Require PHP 8.2, refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
bonroyage committed Feb 24, 2024
1 parent 7600ece commit c2fa550
Show file tree
Hide file tree
Showing 30 changed files with 500 additions and 431 deletions.
4 changes: 2 additions & 2 deletions LICENCE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License

Copyright (c) 2020 Roy de Vos Burchart
Copyright (c) 2024 Roy de Vos Burchart

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE.
31 changes: 19 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

A simple PHP wrapper around [Tripleseat's API](https://support.tripleseat.com/hc/en-us/sections/200821727-Tripleseat-API).

Requires at least PHP 7.1
Requires at least PHP 8.2

Until v1 there may be backward incompatible changes with every minor version (0.x).

Expand Down Expand Up @@ -59,10 +59,15 @@ An `InvalidSite` exception will be thrown if the site is not in the list of site
$mySite = $tripleseat[1];

// Search accounts in this site
$mySite->account->search(['query' => 'tripleseat']);
$mySite->account->search([
'query' => 'tripleseat'
]);

// is the same as
$tripleseat->account->search(['query' => 'tripleseat', 'site_id' => 1]);
$tripleseat->account->search([
'query' => 'tripleseat',
'site_id' => 1
]);
```

#### What does it do in the background?
Expand All @@ -87,13 +92,18 @@ $mySite = new Tripleseat([
Endpoints like `site`, `location`, and `user` don't support the `site_id` parameter. They will always return the same result regardless of what site ID is passed.

### `all` and `search` operations
When querying one of the `all` or `search` endpoints, the client will return a Generator that you can iterate through. These endpoints are paged and return 50 results per page. The client will check the `total_pages` property in the first response and make sure every page gets loaded. The next page will only get loaded once the iterator gets to that point.
When querying one of the `all` or `search` endpoints, the client will return a PaginatedResponse. These endpoints are paged and return 50 results per page.

Call [`iterator_to_array` (?)](https://www.php.net/manual/en/function.iterator-to-array.php) to convert the Generator to an array and load all pages immediately.
The PaginatedResponse class is iterable (over the results loaded in that page). It also features the following helpers:
- `currentPage(): int` - gets the current page number
- `totalPages(): int` - gets the total number of pages
- `hasMore(): bool` - checks if the page number is below the total number of pages
- `next(): ?PaginatedResponse` - get the next page (if applicable)
- `results(): array` - get an array of this page's results
- `all(): array` - load results from all pages into an array
- `untilPage(int $page): array` - load results from current until given page into an array

Additionally, you may provide a `$firstPage` or `$untilPage` on these operations to change from which page on and/or until which page the data should be loaded (provided it's less than the total number of pages).

Note: The `site` and `location` services are not paged and do not feature the `$firstPage` or `$untilPage` arguments.
Note: The `site` and `location` services are not paged.

```php
$bookings = $tripleseat->booking->search([
Expand All @@ -104,9 +114,6 @@ $bookings = $tripleseat->booking->search([
foreach($bookings as $booking) {
// do something with $booking
}

// convert from Generator to array
$bookingsArray = iterator_to_array($bookings);
```

### Other operations
Expand Down Expand Up @@ -186,4 +193,4 @@ You could use any [PSR-18](https://www.php-fig.org/psr/psr-18/) compatible clien
```php
$httpClient = // some class implementing Psr\Http\Client\ClientInterface
$tripleseat = new Tripleseat($auth, $httpClient);
```
```
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"license": "MIT",
"type": "library",
"require": {
"php": ">=7.4",
"php": "^8.2",
"ext-json": "*",
"php-http/client-implementation": "^1.0",
"psr/http-factory": "^1.0",
Expand All @@ -14,10 +14,11 @@
"require-dev": {
"phpunit/phpunit": "^8.5",
"guzzlehttp/psr7": "^1.6",
"guzzlehttp/guzzle": "7.0.1",
"guzzlehttp/guzzle": "^7.0",
"php-http/guzzle7-adapter": "0.1.1",
"http-interop/http-factory-guzzle": "^1.0",
"symfony/var-dumper" : "5.2.x-dev"
"symfony/var-dumper" : "5.2.x-dev",
"laravel/pint": "^1.14"
},
"autoload": {
"psr-4": {
Expand Down
40 changes: 40 additions & 0 deletions pint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"preset": "laravel",
"rules": {
"class_attributes_separation": {
"elements": {
"const": "only_if_meta"
}
},
"concat_space": {
"spacing": "one"
},
"elseif": false,
"function_declaration": {
"closure_fn_spacing": "none"
},
"no_extra_blank_lines": {
"tokens": [
"throw",
"use",
"extra",
"return",
"curly_brace_block"
]
},
"not_operator_with_successor_space": false,
"trailing_comma_in_multiline": {
"elements": [
"arrays",
"match",
"parameters"
]
},
"type_declaration_spaces": {
"elements": [
"function"
]
},
"phpdoc_align": false
}
}
14 changes: 8 additions & 6 deletions src/Contracts/Http.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?php namespace Tripleseat\Contracts;
<?php

namespace Tripleseat\Contracts;

interface Http
{
public function get(string $path, array $query = []);
public function get(string $path, array $query = []): array;

public function post(string $path, $body = null, array $query = []);
public function post(string $path, ?array $body = null, array $query = []): array;

public function put(string $path, $body = null, array $query = []);
public function put(string $path, ?array $body = null, array $query = []): array;

public function delete(string $path, array $query = []);
}
public function delete(string $path, array $query = []): array;
}
10 changes: 5 additions & 5 deletions src/Exceptions/HttpException.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php namespace Tripleseat\Exceptions;
<?php

namespace Tripleseat\Exceptions;

use Exception;

class HttpException extends Exception implements TripleseatException
{

public $httpStatus = 0;
public $httpMessage = null;
public $httpBody = null;
Expand All @@ -29,7 +30,7 @@ public function getMessageFromHttpBody()
return $this->httpBody;
}

public function __toString()
public function __toString(): string
{
$base = 'Tripleseat HttpException: Http Status: ' . $this->httpStatus;

Expand All @@ -39,5 +40,4 @@ public function __toString()

return $base;
}

}
}
7 changes: 4 additions & 3 deletions src/Exceptions/InvalidArgumentException.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace Tripleseat\Exceptions;
<?php

namespace Tripleseat\Exceptions;

class InvalidArgumentException extends \InvalidArgumentException implements TripleseatException
{

}
}
7 changes: 4 additions & 3 deletions src/Exceptions/InvalidAuthConfiguration.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace Tripleseat\Exceptions;
<?php

namespace Tripleseat\Exceptions;

class InvalidAuthConfiguration extends \InvalidArgumentException implements TripleseatException
{

}
}
7 changes: 4 additions & 3 deletions src/Exceptions/InvalidService.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php namespace Tripleseat\Exceptions;
<?php

namespace Tripleseat\Exceptions;

use Exception;

class InvalidService extends Exception implements TripleseatException
{

}
}
7 changes: 4 additions & 3 deletions src/Exceptions/InvalidSite.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php namespace Tripleseat\Exceptions;
<?php

namespace Tripleseat\Exceptions;

use Exception;

class InvalidSite extends Exception implements TripleseatException
{

}
}
7 changes: 4 additions & 3 deletions src/Exceptions/TripleseatException.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace Tripleseat\Exceptions;
<?php

namespace Tripleseat\Exceptions;

interface TripleseatException
{

}
}
Loading

0 comments on commit c2fa550

Please sign in to comment.