Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various fixes and api version change #8

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
23 changes: 20 additions & 3 deletions src/TogglConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ class TogglConnection {
/**
* The Toggl API version, used in HTTP requests.
*/
const API_VERSION = 'v6';
const API_VERSION = 'v8';

private $userAgent = 'Toggl PHP SDK';
public $userAgent = 'Toggl PHP SDK';

private $token;

private $options = array();

public $debug = false;
public $curlVerbose = null;

/**
* Construct the API object.
Expand Down Expand Up @@ -53,7 +56,7 @@ public function setOptions(array $options) {
* A fully-quantified Toggl API URL.
*/
public function getURL($resource, array $query = array()) {
$url = 'https://www.toggl.com/api/' . self::API_VERSION . '/' . $resource . '.json';
$url = 'https://www.toggl.com/api/' . self::API_VERSION . '/' . $resource;
if (!empty($query)) {
$url .= '?' . http_build_query($query, NULL, '&');
}
Expand Down Expand Up @@ -98,6 +101,12 @@ public function request($resource, array $options = array()) {
curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $options['method']);
curl_setopt($ch, CURLOPT_USERPWD, $this->getToken() . ':api_token');
if ($this->debug) {
$this->curlVerbose = array('url' => $url, 'postfields' => isset($options['data'])?json_encode($options['data']):null);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$verbose = fopen('php://temp', 'rw+');
curl_setopt($ch, CURLOPT_STDERR, $verbose);
}

// Build and format the headers.
foreach (array_merge($this->getHeaders(), $options['headers']) as $header => $value) {
Expand All @@ -118,6 +127,14 @@ public function request($resource, array $options = array()) {
$response->success = $response->code == 200;

curl_close($ch);

if ($this->debug) {
rewind($verbose);
$this->curlVerbose['result'] = $result;
$this->curlVerbose['curl'] = stream_get_contents($verbose);
fclose($verbose);
}

return $response;
}

Expand Down
26 changes: 17 additions & 9 deletions src/TogglRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ abstract class TogglRecord {

protected $data = array();
protected $connection;

public $debug = false;

public function __construct(TogglConnection $connection, array $data = array()) {
$this->connection = $connection;
$this->connection->debug = $this->debug;
$this->data = $data;
}

Expand Down Expand Up @@ -57,37 +60,42 @@ public static function load(TogglConnection $connection, $id, array $options = a
if (!empty($response->data['data'])) {
return new $class($connection, $response->data['data']);
}
return FALSE;
return $response->success;
}

public static function loadMultiple(TogglConnection $connection, array $options = array()) {
$class = get_called_class();
$response = $connection->request($class::$element_plural_name, $options);
$count = 0;
foreach ($response->data['data'] as $key => $record) {
$response->data['data'][$key] = new $class($connection, $record);
$count++;
$ret = array('data' => array(), 'count' => 0);
foreach ($response->data as $key => $record) {
$ret['data'][] = new $class($connection, $record);
}
$response->data['count'] = $count;
return $response->data;
$ret['count'] = count($ret['data']);
return $ret;
}

public function save(array $options = array()) {
$options['method'] = !empty($this->id) ? 'PUT' : 'POST';
$options['data'][$this::$element_name] = $this->data;
$resource = $this::element_plural_name . (!empty($this->id) ? '/' . $this->id : '');
$resource = $this::$element_plural_name . (!empty($this->id) ? '/' . $this->id : '');
$response = $this->connection->request($resource, $options);
$this->data = $response->data['data'];
return TRUE;
return $response->success;
}

public function delete(array $options = array()) {
if (!empty($this->id)) {
$options['method'] = 'DELETE';
$resource = $this::$element_plural_name . '/' . $this->id;
$response = $this->connection->request($resource, $options);
return $response->success;
}
$this->data = array();
return TRUE;
}

public function getCurlDebugInfo() {
return $this->connection->curlVerbose;
}

}
45 changes: 45 additions & 0 deletions src/TogglReport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

class TogglReport {

public static function loadDateRange(TogglReportConnection $connection, $workspaceId, $start_date, $end_date, array $options = array()) {
if (!$start_date || !$end_date) {
throw new TogglException("Invalid parameters for loading report.");
}

$ret = array(
'data' => array(),
'count' => 0
);

if (isset($start_date) && isset($end_date)) {

if ($end_date < $start_date) {
throw new TogglException("Start date cannot be after the end date.");
}
$options['query']['workspace_id'] = $workspaceId;
$options['query']['since'] = gmdate('Y-m-d', $start_date);
$options['query']['until'] = gmdate('Y-m-d', $end_date);
} else {
return $ret;
}

$page = 1;
while (true) {
$options['query']['page'] = $page;
$response = $connection->request('details', $options);
if (!$response->success) throw new TogglException($response->data['error']['message']);

foreach ($response->data['data'] as $key => $record) {
$entry = new TogglTimeEntry($connection, $record);
$entry->wid = $workspaceId;
$ret['data'][] = $entry;
}
if ($page==$response->data['total_count']) break;
$page++;
}
$ret['count'] = count($ret['data']);
return $ret;
}

}
34 changes: 34 additions & 0 deletions src/TogglReportConnection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/**
* @file
* Defines TogglConnection.
*/

class TogglReportConnection extends TogglConnection {
/**
* The Toggl API version, used in HTTP requests.
*/
public $report_api_version = 'v2';

/**
* Construct the request URI.
*
* @param string $resource
* The resource name/path.
* @param array $query
* An array of query string parameters to add to the URL.
*
* @return
* A fully-quantified Toggl API URL.
*/
public function getURL($resource, array $query = array()) {
$query['user_agent'] = $this->userAgent;
$url = 'https://toggl.com/reports/api/' . $this->report_api_version . '/' . $resource;
if (!empty($query)) {
$url .= '?' . http_build_query($query, NULL, '&');
}
return $url;
}

}
2 changes: 1 addition & 1 deletion src/TogglUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class TogglUtility {
/**
* The date format used by Toggl.com.
*/
const DATE_FORMAT = 'Y-m-d\TH:i:sO';
const DATE_FORMAT = 'c';

/**
* Filter an array of objects or nested arrays by a variable depth value.
Expand Down
4 changes: 2 additions & 2 deletions src/toggl.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Toggl {
/**
* The Toggl API version, used in HTTP requests.
*/
const API_VERSION = 'v6';
const API_VERSION = 'v8';

const DATE_FORMAT = 'Y-m-d\TH:i:sO';

Expand Down Expand Up @@ -47,7 +47,7 @@ public function getToken() {
* A fully-quantified Toggl API URL.
*/
protected function getURL($resource, array $query = array()) {
$url = 'https://www.toggl.com/api/' . self::API_VERSION . '/' . $resource . '.json';
$url = 'https://www.toggl.com/api/' . self::API_VERSION . '/' . $resource;
if (!empty($query)) {
$url .= '?' . http_build_query($query, NULL, '&');
}
Expand Down