Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 14 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Library Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
206 changes: 120 additions & 86 deletions src/Wheniwork.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,172 +12,205 @@
* @author Daniel Olfelt <[email protected]>
* @version 0.1
*/
class Wheniwork {
class Wheniwork
{
/**
* Library Version
*/
const VERSION = '0.1';

const METHOD_GET = 'get';
const METHOD_POST = 'post';
const METHOD_PUT = 'put';
const METHOD_PATCH = 'patch';
/**
* HTTP Methods
*/
const METHOD_GET = 'get';
const METHOD_POST = 'post';
const METHOD_PUT = 'put';
const METHOD_PATCH = 'patch';
const METHOD_DELETE = 'delete';

private $api_key;
private $api_token;
private $api_endpoint = 'https://api.wheniwork.com/2';
private $api_headers = [];
private $verify_ssl = false;
private $api_headers = [];
private $verify_ssl = FALSE;

/**
* Create a new instance
*
* @param string $api_token The user WhenIWork API token
* @param array $options Allows you to set the `headers` and the `endpoint`
*/
function __construct($api_token = null, $options = [])
function __construct($api_token = NULL, $options = [])
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PSR-2.5 says we MUST use lowercase for true, false, and null. Sorry. 😞

{
$this->api_token = $api_token;

if (!empty($options['endpoint'])) {
$this->setEndpoint($options['endpoint']);
$this->setEndpoint($options['endpoint']);
}
if (!empty($options['headers'])) {
$this->setHeaders($options['headers'], true);
$this->setHeaders($options['headers'], TRUE);
}
}

/**
* Set the user token for all requests
*
* @param string $api_token The user WhenIWork API token
* @return Wheniwork
*/
public function setToken($api_token) {
public function setToken($api_token)
{
$this->api_token = $api_token;

return $this;
}

/**
* Get the user token to save for future requests
*
* @return string The user WhenIWork API token
*/
public function getToken() {
public function getToken()
{
return $this->api_token;
}

/**
* Set the endpoint for all requests
* @param string $endpoint The WhenIWork API endpoint to use
* @return Wheniwork
*/
public function setEndpoint($endpoint) {
$this->api_endpoint = $endpoint;
return $this;
* Set the endpoint for all requests
*
* @param string $endpoint The WhenIWork API endpoint to use
* @return Wheniwork
*/
public function setEndpoint($endpoint)
{
$this->api_endpoint = $endpoint;

return $this;
}

/**
* Get the endpoint to use for future requests
* @return string The WhenIWork API endpoint
*/
public function getEndpoint() {
return $this->api_endpoint;
* Get the endpoint to use for future requests
*
* @return string The WhenIWork API endpoint
*/
public function getEndpoint()
{
return $this->api_endpoint;
}

/**
* Set the host for all requests
* @param array $headers Global headers for all future requests
* @return Wheniwork
*/
public function setHeaders(array $headers, $reset = false) {
if ($reset === true) {
$this->api_headers = $headers;
}
else {
$this->api_headers += $headers;
}
* Set the headers for all requests
*
* @param array $headers Global headers for all future requests
* @param bool $reset
* @return $this
*/
public function setHeaders(array $headers, $reset = FALSE)
{
if ($reset === TRUE) {
$this->api_headers = $headers;
} else {
$this->api_headers += $headers;
}

return $this;
return $this;
}

/**
* Get the host to use for future requests
* @return array Global headers array
*/
public function getHeaders() {
return $this->api_headers;
* Get the host to use for future requests
*
* @return array Global headers array
*/
public function getHeaders()
{
return $this->api_headers;
}

/**
* Get an object or list.
* @param string $method The API method to call, e.g. '/users/'
* @param array $params An array of arguments to pass to the method.
* @param array $headers Array of custom headers to be passed
*
* @param string $method The API method to call, e.g. '/users/'
* @param array $params An array of arguments to pass to the method.
* @param array $headers Array of custom headers to be passed
* @return array Object of json decoded API response.
*/
public function get($method, $params = array(), $headers = array()) {
public function get($method, $params = [], $headers = [])
{
return $this->makeRequest($method, self::METHOD_GET, $params, $headers);
}

/**
* Post to an endpoint.
* @param string $method The API method to call, e.g. '/shifts/publish/'
* @param array $params An array of data used to create the object.
* @param array $headers Array of custom headers to be passed
*
* @param string $method The API method to call, e.g. '/shifts/publish/'
* @param array $params An array of data used to create the object.
* @param array $headers Array of custom headers to be passed
* @return array Object of json decoded API response.
*/
public function post($method, $params = array(), $headers = array()) {
public function post($method, $params = [], $headers = [])
{
return $this->makeRequest($method, self::METHOD_POST, $params, $headers);
}

/**
* Create an object. Helper method for post.
* @param string $method The API method to call, e.g. '/users/'
* @param array $params An array of data used to create the object.
* @param array $headers Array of custom headers to be passed
*
* @param string $method The API method to call, e.g. '/users/'
* @param array $params An array of data used to create the object.
* @param array $headers Array of custom headers to be passed
* @return array Object of json decoded API response.
*/
public function create($method, $params = array(), $headers = array()) {
public function create($method, $params = [], $headers = [])
{
return $this->post($method, $params, $headers);
}

/**
* Update an object. Must include the ID.
* @param string $method The API method to call, e.g. '/users/1'
* @param array $params An array of data to update the object. Only changed fields needed.
* @param array $headers Array of custom headers to be passed
*
* @param string $method The API method to call, e.g. '/users/1'
* @param array $params An array of data to update the object. Only changed fields needed.
* @param array $headers Array of custom headers to be passed
* @return array Object of json decoded API response.
*/
public function update($method, $params = array(), $headers = array()) {
public function update($method, $params = [], $headers = [])
{
return $this->makeRequest($method, self::METHOD_PUT, $params, $headers);
}

/**
* Delete an object. Must include the ID.
* @param string $method The API method to call, e.g. '/users/1'
* @param array $params An array of arguments to pass to the method.
* @param array $headers Array of custom headers to be passed
*
* @param string $method The API method to call, e.g. '/users/1'
* @param array $params An array of arguments to pass to the method.
* @param array $headers Array of custom headers to be passed
* @return array Object of json decoded API response.
*/
public function delete($method, $params = array(), $headers = array()) {
public function delete($method, $params = [], $headers = [])
{
return $this->makeRequest($method, self::METHOD_DELETE, $params, $headers);
}


/**
* Performs the underlying HTTP request. Exciting stuff happening here. Not really.
* @param string $method The API method to be called
*
* @param string $method The API method to be called
* @param string $request The type of request
* @param array $params Assoc array of parameters to be passed
* @param array $headers Assoc array of custom headers to be passed
* @param array $params Assoc array of parameters to be passed
* @param array $headers Assoc array of custom headers to be passed
* @return array Assoc array of decoded result
*/
private function makeRequest($method, $request, $params = array(), $headers = array()) {

$url = $this->getEndpoint().'/'.$method;
private function makeRequest($method, $request, $params = [], $headers = [])
{
$url = $this->getEndpoint() . '/' . $method;

if ($params && ($request == self::METHOD_GET || $request == self::METHOD_DELETE)) {
$url .= '?'.http_build_query($params);
$url .= '?' . http_build_query($params);
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'WhenIWork-PHP/0.1');
curl_setopt($ch, CURLOPT_USERAGENT, 'WhenIWork-PHP/' . static::VERSION);

$headers += $this->getHeaders();

Expand All @@ -186,51 +219,52 @@ private function makeRequest($method, $request, $params = array(), $headers = ar
$headers['W-Token'] = $this->api_token;
}

$header_data = array();
foreach ($headers as $k=>$v) {
$headers_data[] = $k.': '.$v;
$headers_data = [];
foreach ($headers as $k => $v) {
$headers_data[] = $k . ': ' . $v;
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_data);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($request));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verify_ssl);

if (in_array($request, array(self::METHOD_POST, self::METHOD_PUT, self::METHOD_PATCH))) {
if (in_array($request, [self::METHOD_POST, self::METHOD_PUT, self::METHOD_PATCH])) {
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
}

$result = curl_exec($ch);
curl_close($ch);

return $result ? json_decode($result) : false;
return $result ? json_decode($result) : FALSE;
}



/**
* Login helper using developer key and credentials to get back a login response
*
* @param $key Developer API key
* @param $email Email of the user logging in
* @param $password Password of the user
* @return
*/
public static function login($key, $email, $password) {
$params = array(
public static function login($key, $email, $password)
{
$params = [
"username" => $email,
"password" => $password
);
$headers = array(
"password" => $password,
];

$headers = [
'W-Key' => $key
);
];

$login = new static();
$response = $login->makeRequest("login", self::METHOD_POST, $params, $headers);

return $response;
}

}
2 changes: 2 additions & 0 deletions tests/WheniworkTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

require_once __DIR__ . '/../src/Wheniwork.php';

/**
* Test for the client library.
*
Expand Down