Skip to content

Commit

Permalink
Simplify operation calls
Browse files Browse the repository at this point in the history
  • Loading branch information
bonroyage committed Nov 16, 2020
1 parent 06bea81 commit 97f4b9c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 108 deletions.
14 changes: 0 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,6 @@ The following services are supported
| Site | [Tripleseat documentation](https://support.tripleseat.com/hc/en-us/articles/212912147-Sites-API) | [Schema](http://api.tripleseat.com/v1/site_schema.json) |
| User | [Tripleseat documentation](https://support.tripleseat.com/hc/en-us/articles/212567567-Users-API) | [Schema](http://api.tripleseat.com/v1/user_schema.json) |

### Calling operations

```php
// Option 1: $tripleseat->[service]->[operation]()

$tripleseat->booking->all();
$tripleseat->user->get(1);

// Option 2: $tripleseat->[operation][Service]()

$tripleseat->allBooking();
$tripleseat->getUser(1);
```

### Sites
> A site represents a group of venues. Sites can have multiple locations.
Expand Down
100 changes: 6 additions & 94 deletions src/Tripleseat.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php namespace Tripleseat;

use Generator;
use Psr\Http\Client\ClientInterface;
use Tripleseat\Exceptions\InvalidArgumentException;
use Tripleseat\Exceptions\InvalidAuthConfiguration;
Expand All @@ -18,48 +17,6 @@
* @property Services\Location location
* @property Services\Site site
* @property Services\User user
*
* @method Generator allAccount(int $fromPage = 1, int $untilPage = PHP_INT_MAX)
* @method Generator searchAccount(array $parameters, int $fromPage = 1, int $untilPage = PHP_INT_MAX)
* @method getAccount(int $id)
* @method createAccount(array $account)
* @method updateAccount(int $id, array $account)
* @method deleteAccount(int $id)
*
* @method Generator allBooking(int $fromPage = 1, int $untilPage = PHP_INT_MAX)
* @method Generator searchBooking(array $parameters, int $fromPage = 1, int $untilPage = PHP_INT_MAX)
* @method getBooking(int $id)
* @method createBooking(array $booking)
* @method updateBooking(int $id, array $booking)
* @method deleteBooking(int $id)
*
* @method Generator allContact(int $fromPage = 1, int $untilPage = PHP_INT_MAX)
* @method Generator searchContact(array $parameters, int $fromPage = 1, int $untilPage = PHP_INT_MAX)
* @method getContact(int $id)
* @method createContact(array $contact)
* @method updateContact(int $id, array $contact)
* @method deleteContact(int $id)
*
* @method Generator allEvent(int $fromPage = 1, int $untilPage = PHP_INT_MAX)
* @method Generator searchEvent(array $parameters, int $fromPage = 1, int $untilPage = PHP_INT_MAX)
* @method getEvent(int $id)
* @method createEvent(array $event)
* @method updateEvent(int $id, array $event)
* @method deleteEvent(int $id)
*
* @method Generator allLead(int $fromPage = 1, int $untilPage = PHP_INT_MAX)
* @method Generator searchLead(array $parameters, int $fromPage = 1, int $untilPage = PHP_INT_MAX)
* @method getLead(int $id)
* @method createLead(array $lead, array $additionalData = [])
* @method Generator formsLead()
*
* @method Generator allLocation()
*
* @method Generator allSite()
*
* @method Generator allUser(int $fromPage = 1, int $untilPage = PHP_INT_MAX)
* @method Generator searchUser(array $parameters, int $fromPage = 1, int $untilPage = PHP_INT_MAX)
* @method getUser(int $id)
*/
class Tripleseat implements \ArrayAccess
{
Expand Down Expand Up @@ -141,48 +98,12 @@ public function __get(string $name)
}

/**
* @param $name
* @param $arguments
* @return mixed
* @throws InvalidService
*/
public function __call($name, $arguments)
{
if ([$service, $method] = $this->extractServiceAndMethod($name)) {
$service = $this->__get($service);

if (!empty($method)) {
return call_user_func_array([$service, $method], $arguments);
}
}

throw new InvalidService($name);
}

/**
* Parses a string to check if the last part matches any of the defined
* services and returns an array with the service and the string in front
* of the service as the method name.
*
* @param string $haystack
* @return array|null
* @param int $offset
* @return bool
*/
private function extractServiceAndMethod(string $haystack)
{
$haystack = strtolower($haystack);
$needles = array_keys($this->availableServices);

foreach ($needles as $needle) {
if ($needle !== '' && substr_compare($haystack, (string)$needle, -strlen($needle), null, true) === 0) {
return [strtolower($needle), strstr($haystack, (string)$needle, true)];
}
}

return null;
}

private function sites()
public function offsetExists($offset)
{
// Load and cache the sites
if (is_null($this->sites)) {
$this->sites = [];

Expand All @@ -191,20 +112,11 @@ private function sites()
}
}

return $this->sites;
}

/**
* @param mixed $offset
* @return bool
*/
public function offsetExists($offset)
{
return array_key_exists($offset, $this->sites());
return array_key_exists($offset, $this->sites);
}

/**
* @param mixed $offset
* @param int $offset
* @return Tripleseat
* @throws InvalidSite
*/
Expand Down

0 comments on commit 97f4b9c

Please sign in to comment.