diff --git a/Mite/Mite.php b/Mite/Mite.php index 293fd5d..6fad6f1 100644 --- a/Mite/Mite.php +++ b/Mite/Mite.php @@ -1,721 +1,721 @@ - - */ -class Mite -{ - /** - * Your mite address - * @var string - */ - protected $address = null; - - /** - * Your mite API key - * @var string - */ - protected $apiKey = null; - - /** - * Resty instance - * @var Resty - */ - private $Resty = null; - - function __construct($address, $apiKey, $agent = 'Mitey/v0.1') - { - // check for Resty, we need it! - if (!class_exists('Mite\Rest\Resty')) - { - throw new \Exception('Needed class "Resty" does not exist.'); - } - - $this->address = $address; - $this->apiKey = $apiKey; - - $this->Resty = new Resty($this->address, 'json'); - - $this->Resty->setHeader('X-MiteApiKey', $this->apiKey) - ->setHeader('User-Agent', $agent) - // TODO set correct ssl data - ->setSSL(false); - } - - /** - * Get array of mite customers by defined filters - * - * @param string $name - * @param int $limit - * @param int $offset - * - * @return MiteCustomerIterator - */ - public function getCustomers($name = '', $limit = false, $offset = false) - { - return $this->getEntities( - 'customers.json', - 'MiteCustomerIterator', - 'MiteCustomer', - 'customer', - array('name' => $name), - $limit, - $offset - ); - } - - /** - * Generic method to fetch different types of entities of the mite API - * - * @param string $endpoint API url - * @param string $iterator Classname of the returned iterator class - * @param string $entity_class Classname of intatiated entity type - * @param string $property Property name of returned API response - * @param array $params Filter properties - * @param int $limit - * @param int $offset - */ - private function getEntities($endpoint, $iterator = 'ArrayIterator', $entity_class = 'MiteEntity', $property = '', Array $params = array(), $limit = false, $offset = false) - { - $limit && $params['limit'] = $limit; - $offset && $params['page'] = $offset; - - try - { - $response = $this->Resty->get($endpoint, $params); - - if (!is_array($response)) - { - throw new MiteException(__FUNCTION__.'(): No response from REST interface.'); - } - - $iterator = '\\Mite\\'.$iterator; - $entity_class = '\\Mite\\'.$entity_class; - $entities = new $iterator(); - foreach ($response as $r) - { - $entities->append(new $entity_class($r->$property)); - } - - return $entities; - } - catch (\Exception $e) - { - throw new MiteException($e->getMessage()); - } - } - - /** - * Get specified customer object - * - * @param int $id The customer id - * @return MiteCustomer - */ - public function getCustomer($id) - { - return $this->getEntity('customers/'.$id.'.json', 'customer', 'MiteCustomer'); - } - - /** - * Get specified project object - * - * @param int $id The project id - * @return MiteProject - */ - public function getProject($id) - { - return $this->getEntity('projects/'.$id.'.json', 'project', 'MiteProject'); - } - - /** - * get specified time - * - * @param int $id - * @return MiteTime - */ - public function getTime($id) - { - return $this->getEntity('time_entries/'.$id.'.json', 'time_entry', 'MiteTime'); - } - - /** - * get specified service - * - * @param int $id - * @return MiteService - */ - public function getService($id) - { - return $this->getEntity('services/'.$id.'.json', 'service', 'MiteService'); - } - - /** - * Get specified entity object - * - * @param string $endpoint - * @param string $property - * @param string $entity_class - */ - private function getEntity($endpoint, $property, $entity_class = 'MiteEntity') - { - try - { - $response = $this->Resty->get($endpoint); - if ($response->$property) - { - $entity_class = '\\Mite\\'.$entity_class; - return new $entity_class($response->$property); - } - } - catch(\Exception $e) {} - - throw new MiteException('Entity of type '.ucfirst($property).' not found (Endpoint: '.$endpoint.').'); - } - - /** - * API Testing Customer ID 170653 - * default note: Testkunde zum Testen und Entwickeln der API Anbindung zu facturandum. - * - * Update customer data - * - * @param int $id the customer id - * @param Array $data data to be updated - * @return boolean - */ - public function updateCustomer($id, Array $data) - { - return $this->updateEntity('customers/'.$id.'.json', array('customer' => $data)); - } - - /** - * Update project data - * - * @param int $id the project id - * @param Array $data data to be updated - * @return boolean - */ - public function updateProject($id, Array $data) - { - return $this->updateEntity('projects/'.$id.'.json', array('project' => $data)); - } - - /** - * Update service properties - * - * @param int $id the service id - * @param Array $data the properties to be updated - */ - public function updateService($id, Array $data) - { - return $this->updateEntity('services/'.$id.'json', array('service' => $data)); - } - - /** - * Update specified mite entity - * - * @param string $endpoint - * @param Array $data - * @throws MiteException - * @return boolean; - */ - private function updateEntity($endpoint, Array $data) - { - try - { - $response = $this->Resty->put($endpoint, $data); - if ($this->Resty->getInfo('http_code') == '200') - { - return true; - } - } - catch (\Exception $e) - { - throw new MiteException('Entity update failed. '.$e->getMessage()); - } - } - - /** - * Add a new customer - * - * @param array $data Associative array with customer data to be saved - * Take a look at http://mite.yo.lk/api/kunden.html for parameters - * @return MiteCustomer - */ - public function addCustomer(Array $data) - { - return $this->addEntity('customers.json', array('customer' => $data), 'customer'); - } - - /** - * Add a new project - * - * @param array $data Associative array with project data to be saved - * Take a look at http://mite.yo.lk/api/projekte.html for parameters - * @return MiteProject - */ - public function addProject(Array $data) - { - return $this->addEntity('projects.json', array('project' => $data), 'project'); - } - - /** - * Add a new service - * - * @param Array $data Associative array with service properties - * http://mite.yo.lk/en/api/services.html - * @return MiteService - */ - public function addService(Array $data) - { - return $this->addEntity('services.json', array('service' => $data), 'service'); - } - - /** - * Start tracking - * - * @param int $id - */ - public function startTracking($id) - { - //PATCH /tracker/:id.json - try - { - - $response = $this->Resty->patch('/tracker/'.$id.'.json'); - - if ($this->Resty->getInfo('http_code') == '200') { - return $response; - } - } - catch(\Exception $e) - { - throw new MiteException('Entity couldn\'t be added ('.$e->getMessage().').'); - } - - return false; - } - - /** - * Add entity - * - * @param string $endpoint - * @param array $params - * @param string $prop - */ - private function addEntity($endpoint, Array $params = array(), $prop = '', $entity_class = false) - { - try - { - $response = $this->Resty->post($endpoint, $params); - - if ($this->Resty->getInfo('http_code') == '201') - { - if ($entity_class) - { - $entity_class = '\\Mite\\'.$entity_class; - return new $entity_class($response->$prop); - } - - return $response->$prop; - } - } - catch(\Exception $e) - { - throw new MiteException('Entity couldn\'t be added ('.$e->getMessage().').'); - } - - return false; - } - - /** - * Delete specified customer - * - * @param int $id - * @return boolean - */ - public function deleteCustomer($id) - { - return $this->deleteEntity('customers/'.$id.'.json'); - } - - /** - * Delete specified project - * - * @param int $id - * @return boolean - */ - public function deleteProject($id) - { - return $this->deleteEntity('projects/'.$id.'.json'); - } - - /** - * Delete specified service - * - * @param int $id - * @return boolean - */ - public function deleteService($id) - { - return $this->deleteEntity('services/'.$id.'.json'); - } - - /** - * Deletes a entity - * - * @param string $endpoint - * @throws MiteException - * @return boolean - */ - private function deleteEntity($endpoint) - { - try - { - $response = $this->Resty->delete($endpoint); - if ($this->Resty->getInfo('http_code') == '200') - { - return true; - } - } - catch (\Exception $e) - { - throw new MiteException('Entity deletion failed "'.$endpoint.'" ('.$e->getMessage().').'); - } - - return false; - } - - /** - * Get all projects by defined filters - * - * @param string $name - * @param int $limit - * @param int $offset - */ - public function getProjects($name = '', $limit = false, $offset = false) - { - return $this->getEntities( - 'projects.json', - 'MiteProjectIterator', - 'MiteProject', - 'project', - array('name' => $name), - $limit, - $offset - ); - } - - /** - * Get all services by defined filters - * - * http://mite.yo.lk/en/api/services.html - * - * @param string $name - * @param int $limit - * @param int $offset - * @return MiteServiceIterator - */ - public function getServices($name = '', $limit = false, $offset = false) - { - return $this->getEntities( - 'services.json', - 'MiteServiceIterator', - 'MiteService', - 'service', - array('name' => $name), - $limit, - $offset - ); - } - - /** - * Get all archived projects - * - * @param string $name - * @return MiteProjectIterator - */ - public function getArchivedProjects($name = '') - { - return $this->getEntities( - 'projects/archived.json', - 'MiteProjectIterator', - 'MiteProject', - 'project', - array('name' => $name) - ); - } - - /** - * Get current used mite account infos - * - * @return MiteAccount - */ - public function getAccount() - { - return $this->getEntity('account.json', 'account'); - } - - /** - * Get current logged user - * - * @return MiteUser - */ - public function getMyself() - { - return $this->getEntity('myself.json', 'user'); - } - - /** - * Get time entries by defined filters - * - * @param Array $customers array of customer id's - * @param Array $projects array of project id's - * @param Array $services array of service id's - * @param Array $users array of user id's - * @param boolean $billable true: return only billable entries, false: return only not billable entries, null return all - * @param string $note will search in notes field of time entries - * @param string $at today, yesterday, this_week, last_week, this_month, last_month or date in format YYYY-MM-DD or false - * @param string $from date in format YYYY-MM-DD or false - * @param string $to date in format YYYY-MM-DD or false - * @param boolean $locked true: return only locked, false: return only unlocked, null: return all - * @param int $limit optional limit - * @param int $offset optional offset - * - * @return MiteTimeIterator - */ - public function getTimes(Array $customers = array(), Array $projects = array(), Array $services = array(), - Array $users = array(), $billable = null, $note = false, $at = false, $from = false, - $to = false, $locked = null, $limit = false, $offset = false) - { - $params = array(); - - // produce proper filter array - !empty($customers) && $params['customer_id'] = implode(',', $customers); - !empty($projects) && $params['project_id'] = implode(',', $projects); - !empty($services) && $params['service_id'] = implode(',', $services); - !empty($users) && $params['user_id'] = implode(',', $users); - ($billable !== null) && $params['billable'] = $billable; - $note && $params['note'] = $note; - - preg_match('/^today|yesterday|this_week|last_week|this_month|last_month|\d{4}-\d{2}-\d{2}$/', $at) && $params['at'] = $at; - preg_match('/^\d{4}-\d{2}-\d{2}$/', $from) && $params['from'] = $from; - preg_match('/^\d{4}-\d{2}-\d{2}$/', $to) && $params['to'] = $to; - ($locked !== null) && $params['locked'] = $locked; - - return $this->getEntities( - 'time_entries.json', - 'MiteTimeIterator', - 'MiteTime', - 'time_entry', - $params, - $limit, - $offset - ); - } - - /** - * Get time entries by defined filters - * - * @param Array $customers array of customer id's - * @param Array $projects array of project id's - * @param Array $services array of service id's - * @param Array $users array of user id's - * @param boolean $billable true: return only billable entries, false: return only not billable entries, null return all - * @param string $note will search in notes field of time entries - * @param string $at today, yesterday, this_week, last_week, this_month, last_month or date in format YYYY-MM-DD or false - * @param string $from date in format YYYY-MM-DD or false - * @param string $to date in format YYYY-MM-DD or false - * @param boolean $locked true: return only locked, false: return only unlocked, null: return all - * @param int $limit optional limit - * @param int $offset optional offset - * @param Array $groups array of group_by names (customer, project, service, user, day, week, month, year) - * - * @return MiteTimeIterator - */ - public function getGroupedTimes(Array $groups = array(), Array $customers = array(), Array $projects = array(), Array $services = array(), - Array $users = array(), $billable = null, $note = false, $at = false, $from = false, - $to = false, $locked = null, $limit = false, $offset = false) - { - $params = array(); - - // produce proper filter array - !empty($groups) && $params['group_by'] = implode(',', $groups); - !empty($customers) && $params['customer_id'] = implode(',', $customers); - !empty($projects) && $params['project_id'] = implode(',', $projects); - !empty($services) && $params['service_id'] = implode(',', $services); - !empty($users) && $params['user_id'] = implode(',', $users); - ($billable !== null) && $params['billable'] = $billable; - $note && $params['note'] = $note; - - preg_match('/^today|yesterday|this_week|last_week|this_month|last_month|\d{4}-\d{2}-\d{2}$/', $at) && $params['at'] = $at; - preg_match('/^\d{4}-\d{2}-\d{2}$/', $from) && $params['from'] = $from; - preg_match('/^\d{4}-\d{2}-\d{2}$/', $to) && $params['to'] = $to; - ($locked !== null) && $params['locked'] = $locked; - - return $this->getEntities( - 'time_entries.json', - 'MiteGroupedTimeIterator', - 'MiteGroupedTime', - 'time_entry_group', - $params, - $limit, - $offset - ); - } - - /** - * Add a new time entry - * - * @param string $date - * @param int $minutes - * @param string $note - * @param int $user - * @param int $project - * @param int $service - * - * @return MiteTime - */ - public function addTime($date = false, $minutes = false, $note = false, $user = false, $project = false, $service = false) - { - $params = array(); - - ($date && preg_match('/^\d{4}-\d{2}-\d{2}$/', $date)) && $params['date_at'] = $date; - $minutes && $params['minutes'] = $minutes; - $note && $params['note'] = $note; - $user && $params['user_id'] = $user; - $project && $params['project_id'] = $project; - $service && $params['service_id'] = $service; - - return $this->addEntity('time_entries.json', array('time-entry' => $params), 'time_entry', 'MiteTime'); - } - - /** - * Update existing time entry - * - * @param int $id - * @param string $date - * @param int $minutes - * @param string $note - * @param int $user - * @param int $project - * @param int $service - */ - public function updateTime($id, $date = false, $minutes = false, $note = false, $user = false, $project = false, $service = false, $locked = null) - { - $params = array(); - - ($date && preg_match('/^\d{4}-\d{2}-\d{2}$/', $date)) && $params['date_at'] = $date; - $minutes && $params['minutes'] = $minutes; - $note && $params['note'] = $note; - $user && $params['user_id'] = $user; - $project && $params['project_id'] = $project; - $service && $params['service_id'] = $service; - !is_null($locked) && $params['locked'] = $locked; - - return $this->updateEntity('time_entries/'.$id.'.json', array('time-entry' => $params)); - } - - /** - * Delete a existing time entry - * - * @param int $id - */ - public function deleteTime($id) - { - return $this->deleteEntity('time_entries/'.$id.'.json'); - } - - /** - * Get users - * - * @param string $name - * @param string $email - * @param int $limit - * @param int $offset - */ - public function getUsers($name = false, $email = false, $limit = false, $offset = false) - { - $params = array(); - - $name && $params['name'] = $name; - $email && $params['email'] = $email; - $limit && $params['limit'] = $limit; - $offset && $params['offset'] = $offset; - - return $this->getEntities( - 'users.json', - 'MiteUserIterator', - 'MiteUser', - 'user', - $params, - $limit, - $offset - ); - } - - /** - * Get archived users - * - * @param string $name - * @param string $email - * @param int $limit - * @param int $offset - */ - public function getArchivedUsers($name = false, $email = false, $limit = false, $offset = false) - { - $params = array(); - - $name && $params['name'] = $name; - $email && $params['email'] = $email; - $limit && $params['limit'] = $limit; - $offset && $params['offset'] = $offset; - - return $this->getEntities( - 'users/archived.json', - 'MiteUserIterator', - 'MiteUser', - 'user', - $params, - $limit, - $offset - ); - } - - /** - * Get a specified mite User - * - * @param int $id - */ - public function getUser($id) - { - return $this->getEntity('users/'.$id.'.json', 'user', 'MiteUser'); - } -} - -spl_autoload_register(function($class) { - $file = implode(DIRECTORY_SEPARATOR, explode('\\', ltrim($class, '\\'))); - $path = dirname(__DIR__) . DIRECTORY_SEPARATOR . $file . '.php'; - if(file_exists($path)) include $path; -}); + + */ +class Mite +{ + /** + * Your mite address + * @var string + */ + protected $address = null; + + /** + * Your mite API key + * @var string + */ + protected $apiKey = null; + + /** + * Resty instance + * @var Resty + */ + private $Resty = null; + + function __construct($address, $apiKey, $agent = 'Mitey/v0.1') + { + // check for Resty, we need it! + if (!class_exists('Mite\Rest\Resty')) + { + throw new \Exception('Needed class "Resty" does not exist.'); + } + + $this->address = $address; + $this->apiKey = $apiKey; + + $this->Resty = new Resty($this->address, 'json'); + + $this->Resty->setHeader('X-MiteApiKey', $this->apiKey) + ->setHeader('User-Agent', $agent) + // TODO set correct ssl data + ->setSSL(false); + } + + /** + * Get array of mite customers by defined filters + * + * @param string $name + * @param int $limit + * @param int $offset + * + * @return MiteCustomerIterator + */ + public function getCustomers($name = '', $limit = false, $offset = false) + { + return $this->getEntities( + 'customers.json', + 'MiteCustomerIterator', + 'MiteCustomer', + 'customer', + array('name' => $name), + $limit, + $offset + ); + } + + /** + * Generic method to fetch different types of entities of the mite API + * + * @param string $endpoint API url + * @param string $iterator Classname of the returned iterator class + * @param string $entity_class Classname of intatiated entity type + * @param string $property Property name of returned API response + * @param array $params Filter properties + * @param int $limit + * @param int $offset + */ + private function getEntities($endpoint, $iterator = 'ArrayIterator', $entity_class = 'MiteEntity', $property = '', Array $params = array(), $limit = false, $offset = false) + { + $limit && $params['limit'] = $limit; + $offset && $params['page'] = $offset; + + try + { + $response = $this->Resty->get($endpoint, $params); + + if (!is_array($response)) + { + throw new MiteException(__FUNCTION__.'(): No response from REST interface.'); + } + + $iterator = '\\Mite\\'.$iterator; + $entity_class = '\\Mite\\'.$entity_class; + $entities = new $iterator(); + foreach ($response as $r) + { + $entities->append(new $entity_class($r->$property)); + } + + return $entities; + } + catch (\Exception $e) + { + throw new MiteException($e->getMessage()); + } + } + + /** + * Get specified customer object + * + * @param int $id The customer id + * @return MiteCustomer + */ + public function getCustomer($id) + { + return $this->getEntity('customers/'.$id.'.json', 'customer', 'MiteCustomer'); + } + + /** + * Get specified project object + * + * @param int $id The project id + * @return MiteProject + */ + public function getProject($id) + { + return $this->getEntity('projects/'.$id.'.json', 'project', 'MiteProject'); + } + + /** + * get specified time + * + * @param int $id + * @return MiteTime + */ + public function getTime($id) + { + return $this->getEntity('time_entries/'.$id.'.json', 'time_entry', 'MiteTime'); + } + + /** + * get specified service + * + * @param int $id + * @return MiteService + */ + public function getService($id) + { + return $this->getEntity('services/'.$id.'.json', 'service', 'MiteService'); + } + + /** + * Get specified entity object + * + * @param string $endpoint + * @param string $property + * @param string $entity_class + */ + private function getEntity($endpoint, $property, $entity_class = 'MiteEntity') + { + try + { + $response = $this->Resty->get($endpoint); + if ($response->$property) + { + $entity_class = '\\Mite\\'.$entity_class; + return new $entity_class($response->$property); + } + } + catch(\Exception $e) {} + + throw new MiteException('Entity of type '.ucfirst($property).' not found (Endpoint: '.$endpoint.').'); + } + + /** + * API Testing Customer ID 170653 + * default note: Testkunde zum Testen und Entwickeln der API Anbindung zu facturandum. + * + * Update customer data + * + * @param int $id the customer id + * @param Array $data data to be updated + * @return boolean + */ + public function updateCustomer($id, Array $data) + { + return $this->updateEntity('customers/'.$id.'.json', array('customer' => $data)); + } + + /** + * Update project data + * + * @param int $id the project id + * @param Array $data data to be updated + * @return boolean + */ + public function updateProject($id, Array $data) + { + return $this->updateEntity('projects/'.$id.'.json', array('project' => $data)); + } + + /** + * Update service properties + * + * @param int $id the service id + * @param Array $data the properties to be updated + */ + public function updateService($id, Array $data) + { + return $this->updateEntity('services/'.$id.'json', array('service' => $data)); + } + + /** + * Update specified mite entity + * + * @param string $endpoint + * @param Array $data + * @throws MiteException + * @return boolean; + */ + private function updateEntity($endpoint, Array $data) + { + try + { + $response = $this->Resty->put($endpoint, $data); + if ($this->Resty->getInfo('http_code') == '200') + { + return true; + } + } + catch (\Exception $e) + { + throw new MiteException('Entity update failed. '.$e->getMessage()); + } + } + + /** + * Add a new customer + * + * @param array $data Associative array with customer data to be saved + * Take a look at http://mite.de/api/kunden.html for parameters + * @return MiteCustomer + */ + public function addCustomer(Array $data) + { + return $this->addEntity('customers.json', array('customer' => $data), 'customer'); + } + + /** + * Add a new project + * + * @param array $data Associative array with project data to be saved + * Take a look at http://mite.de/api/projekte.html for parameters + * @return MiteProject + */ + public function addProject(Array $data) + { + return $this->addEntity('projects.json', array('project' => $data), 'project'); + } + + /** + * Add a new service + * + * @param Array $data Associative array with service properties + * http://mite.de/en/api/services.html + * @return MiteService + */ + public function addService(Array $data) + { + return $this->addEntity('services.json', array('service' => $data), 'service'); + } + + /** + * Start tracking + * + * @param int $id + */ + public function startTracking($id) + { + //PATCH /tracker/:id.json + try + { + + $response = $this->Resty->patch('/tracker/'.$id.'.json'); + + if ($this->Resty->getInfo('http_code') == '200') { + return $response; + } + } + catch(\Exception $e) + { + throw new MiteException('Entity couldn\'t be added ('.$e->getMessage().').'); + } + + return false; + } + + /** + * Add entity + * + * @param string $endpoint + * @param array $params + * @param string $prop + */ + private function addEntity($endpoint, Array $params = array(), $prop = '', $entity_class = false) + { + try + { + $response = $this->Resty->post($endpoint, $params); + + if ($this->Resty->getInfo('http_code') == '201') + { + if ($entity_class) + { + $entity_class = '\\Mite\\'.$entity_class; + return new $entity_class($response->$prop); + } + + return $response->$prop; + } + } + catch(\Exception $e) + { + throw new MiteException('Entity couldn\'t be added ('.$e->getMessage().').'); + } + + return false; + } + + /** + * Delete specified customer + * + * @param int $id + * @return boolean + */ + public function deleteCustomer($id) + { + return $this->deleteEntity('customers/'.$id.'.json'); + } + + /** + * Delete specified project + * + * @param int $id + * @return boolean + */ + public function deleteProject($id) + { + return $this->deleteEntity('projects/'.$id.'.json'); + } + + /** + * Delete specified service + * + * @param int $id + * @return boolean + */ + public function deleteService($id) + { + return $this->deleteEntity('services/'.$id.'.json'); + } + + /** + * Deletes a entity + * + * @param string $endpoint + * @throws MiteException + * @return boolean + */ + private function deleteEntity($endpoint) + { + try + { + $response = $this->Resty->delete($endpoint); + if ($this->Resty->getInfo('http_code') == '200') + { + return true; + } + } + catch (\Exception $e) + { + throw new MiteException('Entity deletion failed "'.$endpoint.'" ('.$e->getMessage().').'); + } + + return false; + } + + /** + * Get all projects by defined filters + * + * @param string $name + * @param int $limit + * @param int $offset + */ + public function getProjects($name = '', $limit = false, $offset = false) + { + return $this->getEntities( + 'projects.json', + 'MiteProjectIterator', + 'MiteProject', + 'project', + array('name' => $name), + $limit, + $offset + ); + } + + /** + * Get all services by defined filters + * + * http://mite.de/en/api/services.html + * + * @param string $name + * @param int $limit + * @param int $offset + * @return MiteServiceIterator + */ + public function getServices($name = '', $limit = false, $offset = false) + { + return $this->getEntities( + 'services.json', + 'MiteServiceIterator', + 'MiteService', + 'service', + array('name' => $name), + $limit, + $offset + ); + } + + /** + * Get all archived projects + * + * @param string $name + * @return MiteProjectIterator + */ + public function getArchivedProjects($name = '') + { + return $this->getEntities( + 'projects/archived.json', + 'MiteProjectIterator', + 'MiteProject', + 'project', + array('name' => $name) + ); + } + + /** + * Get current used mite account infos + * + * @return MiteAccount + */ + public function getAccount() + { + return $this->getEntity('account.json', 'account'); + } + + /** + * Get current logged user + * + * @return MiteUser + */ + public function getMyself() + { + return $this->getEntity('myself.json', 'user'); + } + + /** + * Get time entries by defined filters + * + * @param Array $customers array of customer id's + * @param Array $projects array of project id's + * @param Array $services array of service id's + * @param Array $users array of user id's + * @param boolean $billable true: return only billable entries, false: return only not billable entries, null return all + * @param string $note will search in notes field of time entries + * @param string $at today, yesterday, this_week, last_week, this_month, last_month or date in format YYYY-MM-DD or false + * @param string $from date in format YYYY-MM-DD or false + * @param string $to date in format YYYY-MM-DD or false + * @param boolean $locked true: return only locked, false: return only unlocked, null: return all + * @param int $limit optional limit + * @param int $offset optional offset + * + * @return MiteTimeIterator + */ + public function getTimes(Array $customers = array(), Array $projects = array(), Array $services = array(), + Array $users = array(), $billable = null, $note = false, $at = false, $from = false, + $to = false, $locked = null, $limit = false, $offset = false) + { + $params = array(); + + // produce proper filter array + !empty($customers) && $params['customer_id'] = implode(',', $customers); + !empty($projects) && $params['project_id'] = implode(',', $projects); + !empty($services) && $params['service_id'] = implode(',', $services); + !empty($users) && $params['user_id'] = implode(',', $users); + ($billable !== null) && $params['billable'] = $billable; + $note && $params['note'] = $note; + + preg_match('/^today|yesterday|this_week|last_week|this_month|last_month|\d{4}-\d{2}-\d{2}$/', $at) && $params['at'] = $at; + preg_match('/^\d{4}-\d{2}-\d{2}$/', $from) && $params['from'] = $from; + preg_match('/^\d{4}-\d{2}-\d{2}$/', $to) && $params['to'] = $to; + ($locked !== null) && $params['locked'] = $locked; + + return $this->getEntities( + 'time_entries.json', + 'MiteTimeIterator', + 'MiteTime', + 'time_entry', + $params, + $limit, + $offset + ); + } + + /** + * Get time entries by defined filters + * + * @param Array $customers array of customer id's + * @param Array $projects array of project id's + * @param Array $services array of service id's + * @param Array $users array of user id's + * @param boolean $billable true: return only billable entries, false: return only not billable entries, null return all + * @param string $note will search in notes field of time entries + * @param string $at today, yesterday, this_week, last_week, this_month, last_month or date in format YYYY-MM-DD or false + * @param string $from date in format YYYY-MM-DD or false + * @param string $to date in format YYYY-MM-DD or false + * @param boolean $locked true: return only locked, false: return only unlocked, null: return all + * @param int $limit optional limit + * @param int $offset optional offset + * @param Array $groups array of group_by names (customer, project, service, user, day, week, month, year) + * + * @return MiteTimeIterator + */ + public function getGroupedTimes(Array $groups = array(), Array $customers = array(), Array $projects = array(), Array $services = array(), + Array $users = array(), $billable = null, $note = false, $at = false, $from = false, + $to = false, $locked = null, $limit = false, $offset = false) + { + $params = array(); + + // produce proper filter array + !empty($groups) && $params['group_by'] = implode(',', $groups); + !empty($customers) && $params['customer_id'] = implode(',', $customers); + !empty($projects) && $params['project_id'] = implode(',', $projects); + !empty($services) && $params['service_id'] = implode(',', $services); + !empty($users) && $params['user_id'] = implode(',', $users); + ($billable !== null) && $params['billable'] = $billable; + $note && $params['note'] = $note; + + preg_match('/^today|yesterday|this_week|last_week|this_month|last_month|\d{4}-\d{2}-\d{2}$/', $at) && $params['at'] = $at; + preg_match('/^\d{4}-\d{2}-\d{2}$/', $from) && $params['from'] = $from; + preg_match('/^\d{4}-\d{2}-\d{2}$/', $to) && $params['to'] = $to; + ($locked !== null) && $params['locked'] = $locked; + + return $this->getEntities( + 'time_entries.json', + 'MiteGroupedTimeIterator', + 'MiteGroupedTime', + 'time_entry_group', + $params, + $limit, + $offset + ); + } + + /** + * Add a new time entry + * + * @param string $date + * @param int $minutes + * @param string $note + * @param int $user + * @param int $project + * @param int $service + * + * @return MiteTime + */ + public function addTime($date = false, $minutes = false, $note = false, $user = false, $project = false, $service = false) + { + $params = array(); + + ($date && preg_match('/^\d{4}-\d{2}-\d{2}$/', $date)) && $params['date_at'] = $date; + $minutes && $params['minutes'] = $minutes; + $note && $params['note'] = $note; + $user && $params['user_id'] = $user; + $project && $params['project_id'] = $project; + $service && $params['service_id'] = $service; + + return $this->addEntity('time_entries.json', array('time-entry' => $params), 'time_entry', 'MiteTime'); + } + + /** + * Update existing time entry + * + * @param int $id + * @param string $date + * @param int $minutes + * @param string $note + * @param int $user + * @param int $project + * @param int $service + */ + public function updateTime($id, $date = false, $minutes = false, $note = false, $user = false, $project = false, $service = false, $locked = null) + { + $params = array(); + + ($date && preg_match('/^\d{4}-\d{2}-\d{2}$/', $date)) && $params['date_at'] = $date; + $minutes && $params['minutes'] = $minutes; + $note && $params['note'] = $note; + $user && $params['user_id'] = $user; + $project && $params['project_id'] = $project; + $service && $params['service_id'] = $service; + !is_null($locked) && $params['locked'] = $locked; + + return $this->updateEntity('time_entries/'.$id.'.json', array('time-entry' => $params)); + } + + /** + * Delete a existing time entry + * + * @param int $id + */ + public function deleteTime($id) + { + return $this->deleteEntity('time_entries/'.$id.'.json'); + } + + /** + * Get users + * + * @param string $name + * @param string $email + * @param int $limit + * @param int $offset + */ + public function getUsers($name = false, $email = false, $limit = false, $offset = false) + { + $params = array(); + + $name && $params['name'] = $name; + $email && $params['email'] = $email; + $limit && $params['limit'] = $limit; + $offset && $params['offset'] = $offset; + + return $this->getEntities( + 'users.json', + 'MiteUserIterator', + 'MiteUser', + 'user', + $params, + $limit, + $offset + ); + } + + /** + * Get archived users + * + * @param string $name + * @param string $email + * @param int $limit + * @param int $offset + */ + public function getArchivedUsers($name = false, $email = false, $limit = false, $offset = false) + { + $params = array(); + + $name && $params['name'] = $name; + $email && $params['email'] = $email; + $limit && $params['limit'] = $limit; + $offset && $params['offset'] = $offset; + + return $this->getEntities( + 'users/archived.json', + 'MiteUserIterator', + 'MiteUser', + 'user', + $params, + $limit, + $offset + ); + } + + /** + * Get a specified mite User + * + * @param int $id + */ + public function getUser($id) + { + return $this->getEntity('users/'.$id.'.json', 'user', 'MiteUser'); + } +} + +spl_autoload_register(function($class) { + $file = implode(DIRECTORY_SEPARATOR, explode('\\', ltrim($class, '\\'))); + $path = dirname(__DIR__) . DIRECTORY_SEPARATOR . $file . '.php'; + if(file_exists($path)) include $path; +}); diff --git a/README.md b/README.md index 7f3de05..07bbfbb 100644 --- a/README.md +++ b/README.md @@ -1,86 +1,86 @@ -Mitey - PHP Wrapper for the mite API -==================================== -This script provides easy access to the [mite API](http://mite.yo.lk/api/index.html) via php objects. You'll be able to use code hinting in your IDE. - -Requirements ------------- -Mitey requires the following -* PHP 5.3.0 or higher -* PHP cURL - -Quick start ------------ -Clone this repo into any webserver and call `examples/index.php`, there you can put in your mite API credentials and -test some methods and check the response values this script provides. If you are, for whatever reason, not able to -clone this, you can go to [http://dev.codeschmiede.de/Mitey/examples/](http://dev.codeschmiede.de/Mitey/examples/) for the example page. - -Usage ------ -```php -getCustomers(); -for ($customers->rewind(); $customers->valid(); $customers->next()) -{ - $customer = $customers->current(); - echo $customer->name . "
"; -} - -// all this methods will return iterator objects -$iterator = $mite->getArchivedProjects(); -$iterator = $mite->getCustomers(); -$iterator = $mite->getProjects(); -$iterator = $mite->getTimes(); -$iterator = $mite->getGroupedTimes(array('week, project')); -$iterator = $mite->getUsers(); -``` - -Get customer details of a specified account -```php -getCustomer(1234); // 1234 is the needed customer id -// $customer contains a object of type MiteCustomer -``` - -Add new stuff to your mite account (customers, projects, times) -```php -addCustomer(array( - 'name' => 'My new customer name', - 'note' => 'Generated via Mitey, totally easy. Whoop whoop.' -)); - -// new project -$newProject = $mite->addProject(array( - 'name' => 'My brand new project', - 'note' => 'Generated via Mitey, totally easy. Whoop whoop.', - 'customer_id' => $newCustomer->id -)); - -// new time entry -$newtime = $mite->addTime( - date('Y-m-d'), // date of time entry - 666, // time in seconds - 'My workdescription, created via Mitey, whoop whoop.', - $mite->getMyself()->id, // user id - $newProject->id, // optional: project id - false // optional: service id -); -``` - -Look into the code `Mite/Mite.php` for a complete list of methods this library provides. - -Contact -------- -In case you wanna get in touch, it's easy! -* Twitter: [@hubeRsen](http://twitter.com/hubeRsen) -* Website: [https://paschi.dev](https://paschi.dev) +Mitey - PHP Wrapper for the mite API +==================================== +This script provides easy access to the [mite API](http://mite.de/api/index.html) via php objects. You'll be able to use code hinting in your IDE. + +Requirements +------------ +Mitey requires the following +* PHP 5.3.0 or higher +* PHP cURL + +Quick start +----------- +Clone this repo into any webserver and call `examples/index.php`, there you can put in your mite API credentials and +test some methods and check the response values this script provides. If you are, for whatever reason, not able to +clone this, you can go to [http://dev.codeschmiede.de/Mitey/examples/](http://dev.codeschmiede.de/Mitey/examples/) for the example page. + +Usage +----- +```php +getCustomers(); +for ($customers->rewind(); $customers->valid(); $customers->next()) +{ + $customer = $customers->current(); + echo $customer->name . "
"; +} + +// all this methods will return iterator objects +$iterator = $mite->getArchivedProjects(); +$iterator = $mite->getCustomers(); +$iterator = $mite->getProjects(); +$iterator = $mite->getTimes(); +$iterator = $mite->getGroupedTimes(array('week, project')); +$iterator = $mite->getUsers(); +``` + +Get customer details of a specified account +```php +getCustomer(1234); // 1234 is the needed customer id +// $customer contains a object of type MiteCustomer +``` + +Add new stuff to your mite account (customers, projects, times) +```php +addCustomer(array( + 'name' => 'My new customer name', + 'note' => 'Generated via Mitey, totally easy. Whoop whoop.' +)); + +// new project +$newProject = $mite->addProject(array( + 'name' => 'My brand new project', + 'note' => 'Generated via Mitey, totally easy. Whoop whoop.', + 'customer_id' => $newCustomer->id +)); + +// new time entry +$newtime = $mite->addTime( + date('Y-m-d'), // date of time entry + 666, // time in seconds + 'My workdescription, created via Mitey, whoop whoop.', + $mite->getMyself()->id, // user id + $newProject->id, // optional: project id + false // optional: service id +); +``` + +Look into the code `Mite/Mite.php` for a complete list of methods this library provides. + +Contact +------- +In case you wanna get in touch, it's easy! +* Twitter: [@hubeRsen](http://twitter.com/hubeRsen) +* Website: [https://paschi.dev](https://paschi.dev) diff --git a/examples/index.php b/examples/index.php index 1990b38..6ef82cf 100644 --- a/examples/index.php +++ b/examples/index.php @@ -31,7 +31,7 @@