Skip to content

Commit

Permalink
Merge pull request #85 from calcinai/utc-support
Browse files Browse the repository at this point in the history
UTC support
  • Loading branch information
calcinai committed Sep 1, 2015
2 parents e09ef70 + 9d6517d commit 5a6207b
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 35 deletions.
8 changes: 7 additions & 1 deletion generator/objects/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ public function getPHPType($with_ns = false) {
return 'string';

case Object::PROPERTY_TYPE_DATE:
case Object::PROPERTY_TYPE_TIMESTAMP:
return '\DateTime';

case Object::PROPERTY_TYPE_OBJECT:
Expand All @@ -195,6 +196,7 @@ public function isHintable(){

switch($this->getType()){
case Object::PROPERTY_TYPE_DATE:
case Object::PROPERTY_TYPE_TIMESTAMP:
case Object::PROPERTY_TYPE_OBJECT:
return true;
default:
Expand All @@ -215,6 +217,10 @@ private function parseType(){
if(preg_match('/^((a\s)?bool|true\b|booelan)/i', $this->description))
$type = Object::PROPERTY_TYPE_BOOLEAN;

//Spelling errors in the docs
if(preg_match('/UTC$/', $this->getName()))
$type = Object::PROPERTY_TYPE_TIMESTAMP;

if(preg_match('/^Has[A-Z]\w+/', $this->getName()))
$type = Object::PROPERTY_TYPE_BOOLEAN;

Expand All @@ -231,7 +237,7 @@ private function parseType(){
if(preg_match('/(^int(eger)?\b)/i', $this->description))
$type = Object::PROPERTY_TYPE_INT;

if(preg_match('/(UTC|timestamp|\bdate\b)/i', $this->description))
if(preg_match('/(\bdate\b)/i', $this->description))
$type = Object::PROPERTY_TYPE_DATE;

if(preg_match('/Xero (generated )?(unique )?identifier/i', $this->description))
Expand Down
2 changes: 1 addition & 1 deletion src/XeroPHP/Models/Accounting/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ public static function getProperties() {
'PurchasesDefaultAccountCode' => array (false, self::PROPERTY_TYPE_STRING, null, false, false),
'SalesTrackingCategories' => array (false, self::PROPERTY_TYPE_OBJECT, 'Accounting\\TrackingCategory', true, false),
'PurchasesTrackingCategories' => array (false, self::PROPERTY_TYPE_OBJECT, 'Accounting\\TrackingCategory', true, false),
'UpdatedDateUTC' => array (false, self::PROPERTY_TYPE_DATE, '\\DateTime', false, false),
'UpdatedDateUTC' => array (false, self::PROPERTY_TYPE_TIMESTAMP, '\\DateTime', false, false),
'ContactGroups' => array (false, self::PROPERTY_TYPE_OBJECT, 'Accounting\\ContactGroup', true, false),
'Website' => array (false, self::PROPERTY_TYPE_STRING, null, false, false),
'BrandingTheme' => array (false, self::PROPERTY_TYPE_OBJECT, 'Accounting\\BrandingTheme', false, false),
Expand Down
2 changes: 1 addition & 1 deletion src/XeroPHP/Models/Accounting/CreditNote.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public static function getProperties() {
'SubTotal' => array (false, self::PROPERTY_TYPE_FLOAT, null, false, false),
'TotalTax' => array (false, self::PROPERTY_TYPE_FLOAT, null, false, false),
'Total' => array (false, self::PROPERTY_TYPE_FLOAT, null, false, false),
'UpdatedDateUTC' => array (false, self::PROPERTY_TYPE_DATE, '\\DateTime', false, false),
'UpdatedDateUTC' => array (false, self::PROPERTY_TYPE_TIMESTAMP, '\\DateTime', false, false),
'CurrencyCode' => array (false, self::PROPERTY_TYPE_STRING, null, false, false),
'FullyPaidOnDate' => array (false, self::PROPERTY_TYPE_DATE, '\\DateTime', false, false),
'CreditNoteID' => array (false, self::PROPERTY_TYPE_STRING, null, false, false),
Expand Down
10 changes: 5 additions & 5 deletions src/XeroPHP/Models/Accounting/Item/Purchase.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Purchase extends Remote\Object {
/**
* This property has been removed from the Xero API
*
* @property string UpdatedDateUTC
* @property \DateTime UpdatedDateUTC
* @deprecated
*/

Expand Down Expand Up @@ -99,7 +99,7 @@ public static function getProperties() {
'UnitPrice' => array (false, self::PROPERTY_TYPE_STRING, null, false, false),
'AccountCode' => array (false, self::PROPERTY_TYPE_STRING, null, false, false),
'TaxType' => array (false, self::PROPERTY_TYPE_ENUM, null, false, false),
'UpdatedDateUTC' => array (false, self::PROPERTY_TYPE_STRING, null, false, false)
'UpdatedDateUTC' => array (false, self::PROPERTY_TYPE_TIMESTAMP, '\\DateTime', false, false)
);
}

Expand Down Expand Up @@ -159,19 +159,19 @@ public function setTaxType($value) {
}

/**
* @return string
* @return \DateTime
* @deprecated
*/
public function getUpdatedDateUTC() {
return $this->_data['UpdatedDateUTC'];
}

/**
* @param string $value
* @param \DateTime $value
* @return Purchase
* @deprecated
*/
public function setUpdatedDateUTC($value) {
public function setUpdatedDateUTC(\DateTime $value) {
$this->propertyUpdated('UpdatedDateUTC', $value);
$this->_data['UpdatedDateUTC'] = $value;
return $this;
Expand Down
10 changes: 5 additions & 5 deletions src/XeroPHP/Models/Accounting/Item/Sale.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Sale extends Remote\Object {
/**
* This property has been removed from the Xero API
*
* @property string UpdatedDateUTC
* @property \DateTime UpdatedDateUTC
* @deprecated
*/

Expand Down Expand Up @@ -99,7 +99,7 @@ public static function getProperties() {
'UnitPrice' => array (false, self::PROPERTY_TYPE_STRING, null, false, false),
'AccountCode' => array (false, self::PROPERTY_TYPE_STRING, null, false, false),
'TaxType' => array (false, self::PROPERTY_TYPE_ENUM, null, false, false),
'UpdatedDateUTC' => array (false, self::PROPERTY_TYPE_STRING, null, false, false)
'UpdatedDateUTC' => array (false, self::PROPERTY_TYPE_TIMESTAMP, '\\DateTime', false, false)
);
}

Expand Down Expand Up @@ -159,19 +159,19 @@ public function setTaxType($value) {
}

/**
* @return string
* @return \DateTime
* @deprecated
*/
public function getUpdatedDateUTC() {
return $this->_data['UpdatedDateUTC'];
}

/**
* @param string $value
* @param \DateTime $value
* @return Sale
* @deprecated
*/
public function setUpdatedDateUTC($value) {
public function setUpdatedDateUTC(\DateTime $value) {
$this->propertyUpdated('UpdatedDateUTC', $value);
$this->_data['UpdatedDateUTC'] = $value;
return $this;
Expand Down
2 changes: 1 addition & 1 deletion src/XeroPHP/Models/Accounting/Organisation.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public static function getProperties() {
'SalesTaxPeriod' => array (false, self::PROPERTY_TYPE_ENUM, null, false, false),
'PeriodLockDate' => array (false, self::PROPERTY_TYPE_STRING, null, false, false),
'EndOfYearLockDate' => array (false, self::PROPERTY_TYPE_STRING, null, false, false),
'CreatedDateUTC' => array (false, self::PROPERTY_TYPE_DATE, '\\DateTime', false, false),
'CreatedDateUTC' => array (false, self::PROPERTY_TYPE_TIMESTAMP, '\\DateTime', false, false),
'OrganisationEntityType' => array (false, self::PROPERTY_TYPE_ENUM, null, false, false),
'Timezone' => array (false, self::PROPERTY_TYPE_STRING, null, false, false),
'ShortCode' => array (false, self::PROPERTY_TYPE_STRING, null, false, false),
Expand Down
2 changes: 1 addition & 1 deletion src/XeroPHP/Models/Accounting/Overpayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public static function getProperties() {
'SubTotal' => array (false, self::PROPERTY_TYPE_FLOAT, null, false, false),
'TotalTax' => array (false, self::PROPERTY_TYPE_FLOAT, null, false, false),
'Total' => array (false, self::PROPERTY_TYPE_FLOAT, null, false, false),
'UpdatedDateUTC' => array (false, self::PROPERTY_TYPE_DATE, '\\DateTime', false, false),
'UpdatedDateUTC' => array (false, self::PROPERTY_TYPE_TIMESTAMP, '\\DateTime', false, false),
'CurrencyCode' => array (false, self::PROPERTY_TYPE_STRING, null, false, false),
'FullyPaidOnDate' => array (false, self::PROPERTY_TYPE_DATE, '\\DateTime', false, false),
'OverpaymentID' => array (false, self::PROPERTY_TYPE_STRING, null, false, false),
Expand Down
2 changes: 1 addition & 1 deletion src/XeroPHP/Models/Accounting/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public static function getProperties() {
'IsReconciled' => array (false, self::PROPERTY_TYPE_STRING, null, false, false),
'Status' => array (false, self::PROPERTY_TYPE_ENUM, null, false, false),
'PaymentType' => array (false, self::PROPERTY_TYPE_ENUM, null, false, false),
'UpdatedDateUTC' => array (false, self::PROPERTY_TYPE_DATE, '\\DateTime', false, false),
'UpdatedDateUTC' => array (false, self::PROPERTY_TYPE_TIMESTAMP, '\\DateTime', false, false),
'PaymentID' => array (false, self::PROPERTY_TYPE_STRING, null, false, false)
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/XeroPHP/Models/Accounting/Prepayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public static function getProperties() {
'SubTotal' => array (false, self::PROPERTY_TYPE_FLOAT, null, false, false),
'TotalTax' => array (false, self::PROPERTY_TYPE_FLOAT, null, false, false),
'Total' => array (false, self::PROPERTY_TYPE_FLOAT, null, false, false),
'UpdatedDateUTC' => array (false, self::PROPERTY_TYPE_DATE, '\\DateTime', false, false),
'UpdatedDateUTC' => array (false, self::PROPERTY_TYPE_TIMESTAMP, '\\DateTime', false, false),
'CurrencyCode' => array (false, self::PROPERTY_TYPE_STRING, null, false, false),
'FullyPaidOnDate' => array (false, self::PROPERTY_TYPE_DATE, '\\DateTime', false, false),
'PrepaymentID' => array (false, self::PROPERTY_TYPE_STRING, null, false, false),
Expand Down
2 changes: 1 addition & 1 deletion src/XeroPHP/Models/Accounting/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public static function getProperties() {
'EmailAddress' => array (false, self::PROPERTY_TYPE_STRING, null, false, false),
'FirstName' => array (false, self::PROPERTY_TYPE_STRING, null, false, false),
'LastName' => array (false, self::PROPERTY_TYPE_STRING, null, false, false),
'UpdatedDateUTC' => array (false, self::PROPERTY_TYPE_DATE, '\\DateTime', false, false),
'UpdatedDateUTC' => array (false, self::PROPERTY_TYPE_TIMESTAMP, '\\DateTime', false, false),
'IsSubscriber' => array (false, self::PROPERTY_TYPE_BOOLEAN, null, false, false),
'OrganisationRole' => array (false, self::PROPERTY_TYPE_ENUM, null, false, false)
);
Expand Down
2 changes: 1 addition & 1 deletion src/XeroPHP/Models/Files/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public static function getProperties() {
'FolderId' => array (false, self::PROPERTY_TYPE_OBJECT, 'Files\\Folder', false, false),
'MimeType' => array (false, self::PROPERTY_TYPE_STRING, null, false, false),
'Size' => array (false, self::PROPERTY_TYPE_STRING, null, false, false),
'CreatedDateUTC' => array (false, self::PROPERTY_TYPE_DATE, '\\DateTime', false, false),
'CreatedDateUTC' => array (false, self::PROPERTY_TYPE_TIMESTAMP, '\\DateTime', false, false),
'UpdatedDateUTC' => array (false, self::PROPERTY_TYPE_DATE, '\\DateTime', false, false),
'User' => array (false, self::PROPERTY_TYPE_STRING, null, false, false),
'Id' => array (false, self::PROPERTY_TYPE_GUID, null, false, false)
Expand Down
2 changes: 1 addition & 1 deletion src/XeroPHP/Models/PayrollAU/Employee.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public static function getProperties() {
'TerminationDate' => array (false, self::PROPERTY_TYPE_DATE, '\\DateTime', false, false),
'EmployeeID' => array (false, self::PROPERTY_TYPE_STRING, null, false, false),
'Status' => array (false, self::PROPERTY_TYPE_ENUM, null, false, false),
'UpdatedDateUTC' => array (false, self::PROPERTY_TYPE_DATE, '\\DateTime', false, false)
'UpdatedDateUTC' => array (false, self::PROPERTY_TYPE_TIMESTAMP, '\\DateTime', false, false)
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/XeroPHP/Models/PayrollUS/Employee.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public static function getProperties() {
'OpeningBalances' => array (false, self::PROPERTY_TYPE_OBJECT, 'PayrollUS\\Employee\\OpeningBalance', true, false),
'EmployeeID' => array (false, self::PROPERTY_TYPE_STRING, null, false, false),
'Status' => array (false, self::PROPERTY_TYPE_ENUM, null, false, false),
'UpdatedDateUTC' => array (false, self::PROPERTY_TYPE_DATE, '\\DateTime', false, false)
'UpdatedDateUTC' => array (false, self::PROPERTY_TYPE_TIMESTAMP, '\\DateTime', false, false)
);
}

Expand Down
10 changes: 5 additions & 5 deletions src/XeroPHP/Models/PayrollUS/Paystub.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class Paystub extends Remote\Object {
/**
*
*
* @property string UpdatedDateUTC
* @property \DateTime UpdatedDateUTC
*/

/**
Expand Down Expand Up @@ -204,7 +204,7 @@ public static function getProperties() {
'Tax' => array (false, self::PROPERTY_TYPE_FLOAT, null, false, false),
'Reimbursements' => array (false, self::PROPERTY_TYPE_FLOAT, null, true, false),
'NetPay' => array (false, self::PROPERTY_TYPE_FLOAT, null, false, false),
'UpdatedDateUTC' => array (false, self::PROPERTY_TYPE_STRING, null, false, false),
'UpdatedDateUTC' => array (false, self::PROPERTY_TYPE_TIMESTAMP, '\\DateTime', false, false),
'EarningsLines' => array (false, self::PROPERTY_TYPE_OBJECT, 'PayrollUS\\Paystub\\EarningsLine', true, false),
'LeaveEarningsLines' => array (false, self::PROPERTY_TYPE_OBJECT, 'PayrollUS\\Paystub\\LeaveEarningsLine', true, false),
'TimesheetEarningsLines' => array (false, self::PROPERTY_TYPE_OBJECT, 'PayrollUS\\Paystub\\TimesheetEarningsLine', true, false),
Expand Down Expand Up @@ -410,17 +410,17 @@ public function setNetPay($value) {
}

/**
* @return string
* @return \DateTime
*/
public function getUpdatedDateUTC() {
return $this->_data['UpdatedDateUTC'];
}

/**
* @param string $value
* @param \DateTime $value
* @return Paystub
*/
public function setUpdatedDateUTC($value) {
public function setUpdatedDateUTC(\DateTime $value) {
$this->propertyUpdated('UpdatedDateUTC', $value);
$this->_data['UpdatedDateUTC'] = $value;
return $this;
Expand Down
25 changes: 16 additions & 9 deletions src/XeroPHP/Remote/Object.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ abstract class Object implements ObjectInterface, \JsonSerializable, \ArrayAcces
/**
*
*/
const PROPERTY_TYPE_STRING = 'string';
const PROPERTY_TYPE_INT = 'int';
const PROPERTY_TYPE_FLOAT = 'float';
const PROPERTY_TYPE_BOOLEAN = 'bool';
const PROPERTY_TYPE_ENUM = 'enum';
const PROPERTY_TYPE_GUID = 'guid';
const PROPERTY_TYPE_DATE = 'date';
const PROPERTY_TYPE_OBJECT = 'object';
const PROPERTY_TYPE_STRING = 'string';
const PROPERTY_TYPE_INT = 'int';
const PROPERTY_TYPE_FLOAT = 'float';
const PROPERTY_TYPE_BOOLEAN = 'bool';
const PROPERTY_TYPE_ENUM = 'enum';
const PROPERTY_TYPE_GUID = 'guid';
const PROPERTY_TYPE_DATE = 'date';
const PROPERTY_TYPE_TIMESTAMP = 'timestamp';
const PROPERTY_TYPE_OBJECT = 'object';

/**
* Container to the actual properties of the object
Expand Down Expand Up @@ -238,6 +239,7 @@ public static function castToString($type, $value) {
return $value ? 'true' : 'false';

case self::PROPERTY_TYPE_DATE:
case self::PROPERTY_TYPE_TIMESTAMP:
/** @var \DateTime $value */
return $value->format('c');

Expand All @@ -260,6 +262,9 @@ public static function castToString($type, $value) {
*/
public static function castFromString($type, $value, $php_type) {

//Here should maybe handle locale specific tz overrides in the future.
$timezone = null;

switch($type) {

case self::PROPERTY_TYPE_INT:
Expand All @@ -271,10 +276,12 @@ public static function castFromString($type, $value, $php_type) {
case self::PROPERTY_TYPE_BOOLEAN:
return in_array(strtolower($value), array('true', '1', 'yes'));

case self::PROPERTY_TYPE_TIMESTAMP:
$timezone = new \DateTimeZone('UTC');
case self::PROPERTY_TYPE_DATE:
if(preg_match('/Date\((?<timestamp>[0-9\+\.]+)\)/', $value, $matches)) //to catch stupid .net date serialisation
$value = $matches['timestamp'];
return new \DateTime($value);
return new \DateTime($value, $timezone);

case self::PROPERTY_TYPE_OBJECT:
$php_type = sprintf('\\XeroPHP\\Models\\%s', $php_type);
Expand Down

0 comments on commit 5a6207b

Please sign in to comment.