Skip to content

Commit

Permalink
Merge pull request #202 from Synchro/master
Browse files Browse the repository at this point in the history
More PSR-2 changes!
  • Loading branch information
calcinai authored Oct 19, 2016
2 parents d0341f3 + c93a687 commit 524f4bc
Show file tree
Hide file tree
Showing 19 changed files with 109 additions and 66 deletions.
9 changes: 3 additions & 6 deletions examples/partner.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

// If no session found
if ($oauth_session === null) {

$url = new URL($xero, URL::OAUTH_REQUEST_TOKEN);

$request = new Request($xero, $url);
Expand All @@ -53,7 +52,6 @@

printf('<a href="%s">Click here to Authorize</a>', $xero->getAuthorizeURL($oauth_response['oauth_token']));
exit;

} elseif (isset($oauth_session['session_handle']) && !isset($oauth_session['expires'])) {
// If session is expired refresh the token
$url = new URL($xero, URL::OAUTH_ACCESS_TOKEN);
Expand All @@ -77,9 +75,7 @@
$xero->getOAuthClient()
->setToken($oauth_response['oauth_token'])
->setTokenSecret($oauth_response['oauth_token_secret']);

} else {

$xero->getOAuthClient()
->setToken($oauth_session['token'])
->setTokenSecret($oauth_session['token_secret']);
Expand Down Expand Up @@ -108,7 +104,6 @@
//Just for demo purposes
header(sprintf('Location: http%s://%s%s', (isset($_SERVER['HTTPS']) ? 's' : ''), $_SERVER['HTTP_HOST'], $uri_parts[0]));
exit;

}
}

Expand All @@ -135,7 +130,9 @@ function getOAuthSession()
}

// If the session is expired or expiring, unset the expires key
if ($_SESSION['oauth']['expires'] !== null && $_SESSION['oauth']['expires'] <= (time() + 100)) {
if ($_SESSION['oauth']['expires'] !== null
&& $_SESSION['oauth']['expires'] <= (time() + 100)
) {
unset($_SESSION['oauth']['expires']);
}

Expand Down
21 changes: 15 additions & 6 deletions examples/public.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@
$oauth_response['oauth_token_secret']
);

printf('<a href="%s">Click here to Authorize</a>', $xero->getAuthorizeURL($oauth_response['oauth_token']));
printf(
'<a href="%s">Click here to Authorize</a>',
$xero->getAuthorizeURL($oauth_response['oauth_token'])
);
exit;

} else {
$xero->getOAuthClient()
->setToken($oauth_session['token'])
Expand All @@ -71,7 +73,14 @@
$uri_parts = explode('?', $_SERVER['REQUEST_URI']);

//Just for demo purposes
header(sprintf('Location: http%s://%s%s', (isset($_SERVER['HTTPS']) ? 's' : ''), $_SERVER['HTTP_HOST'], $uri_parts[0]));
header(
sprintf(
'Location: http%s://%s%s',
(isset($_SERVER['HTTPS']) ? 's' : ''),
$_SERVER['HTTP_HOST'],
$uri_parts[0]
)
);
exit;
}
}
Expand All @@ -98,9 +107,9 @@ function setOAuthSession($token, $secret, $expires = null)
function getOAuthSession()
{
//If it doesn't exist or is expired, return null
if (!isset($_SESSION['oauth']) ||
($_SESSION['oauth']['expires'] !== null &&
$_SESSION['oauth']['expires'] <= time())
if (!isset($_SESSION['oauth'])
|| ($_SESSION['oauth']['expires'] !== null
&& $_SESSION['oauth']['expires'] <= time())
) {
return null;
}
Expand Down
6 changes: 0 additions & 6 deletions generator/objects/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,22 +241,16 @@ public function searchByKey($key, $namespace_hint = '')

if (isset($this->search_keys[$ns_key])) {
return $this->search_keys[$ns_key];

} elseif (isset($this->search_keys[$plural_ns_key])) {
return $this->search_keys[$plural_ns_key];

} elseif (isset($this->search_keys[$singular_ns_key])) {
return $this->search_keys[$singular_ns_key];

} elseif (isset($this->search_keys[$key])) {
return $this->search_keys[$key];

} elseif (isset($this->search_keys[$plural_key])) {
return $this->search_keys[$plural_key];

} elseif (isset($this->search_keys[$singular_key])) {
return $this->search_keys[$singular_key];

} else {
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion generator/objects/ParsedObjectInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ interface ParsedObjectInterface
{
public function getName();
public function setName($name);
}
}
8 changes: 6 additions & 2 deletions src/XeroPHP/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use XeroPHP\Remote\Request;
use XeroPHP\Remote\URL;


abstract class Application
{
protected static $_config_defaults = [
Expand Down Expand Up @@ -335,7 +334,12 @@ private function savePropertiesDirectly(Remote\Object $object)
public function delete(Remote\Object $object)
{
if (!$object::supportsMethod(Request::METHOD_DELETE)) {
throw new Exception(sprintf('%s doesn\'t support [DELETE] via the API', get_class($object)));
throw new Exception(
sprintf(
'%s doesn\'t support [DELETE] via the API',
get_class($object)
)
);
}

$uri = sprintf('%s/%s', $object::getResourceURI(), $object->getGUID());
Expand Down
4 changes: 2 additions & 2 deletions src/XeroPHP/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public static function arrayToXML(array $array, $key_override = null)
$element = self::arrayToXML($element, self::singularize($key));
}
}

} else {
//Element escaping for the http://www.w3.org/TR/REC-xml/#sec-predefined-ent
//Full DOMDocument not really necessary as we don't use attributes (which are more strict)
Expand Down Expand Up @@ -90,7 +89,8 @@ public static function XMLToArray(\SimpleXMLElement $sxml)

/**
* This function is based on Wave\Inflector::singularize().
* It only contains a fraction of the rules from its predecessor, so only good for a quick basic singularisation.
* It only contains a fraction of the rules from its predecessor,
* so only good for a quick basic singularisation.
*
* @param $string
* @return mixed
Expand Down
2 changes: 1 addition & 1 deletion src/XeroPHP/Remote/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

class Exception extends \XeroPHP\Exception
{
}
}
33 changes: 26 additions & 7 deletions src/XeroPHP/Remote/OAuth/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ public function sign(Request $request)
throw new Exception('Invalid signing location specified.');
}

//Reset so this instance of the Client can sign subsequent requests. Mainly for the nonce.
//Reset so this instance of the Client can sign subsequent requests.
//Mainly for the nonce.
$this->resetOAuthParams();
}

Expand Down Expand Up @@ -143,16 +144,29 @@ private function getSignature()
{
switch ($this->getSignatureMethod()) {
case self::SIGNATURE_RSA_SHA1:
$signature = RSASHA1::generateSignature($this->config, $this->getSBS(), $this->getSigningSecret());
$signature = RSASHA1::generateSignature(
$this->config,
$this->getSBS(),
$this->getSigningSecret()
);
break;
case self::SIGNATURE_HMAC_SHA1:
$signature = HMACSHA1::generateSignature($this->config, $this->getSBS(), $this->getSigningSecret());
$signature = HMACSHA1::generateSignature(
$this->config,
$this->getSBS(),
$this->getSigningSecret()
);
break;
case self::SIGNATURE_PLAINTEXT:
$signature = PLAINTEXT::generateSignature($this->config, $this->getSBS(), $this->getSigningSecret());
$signature = PLAINTEXT::generateSignature(
$this->config, $this->getSBS(),
$this->getSigningSecret()
);
break;
default:
throw new Exception("Invalid signature method [{$this->config['signature_method']}]");
throw new Exception(
"Invalid signature method [{$this->config['signature_method']}]"
);
}

return $signature;
Expand All @@ -179,7 +193,12 @@ public function getSBS()
$url = $this->request->getUrl()->getFullURL();

//Every second thing seems to need escaping!
return sprintf('%s&%s&%s', $this->request->getMethod(), Helpers::escape($url), Helpers::escape($sbs_string));
return sprintf(
'%s&%s&%s',
$this->request->getMethod(),
Helpers::escape($url),
Helpers::escape($sbs_string)
);
}

/**
Expand Down Expand Up @@ -212,7 +231,7 @@ private function getNonce($length = 20)
$parts = explode('.', microtime(true));
$nonce = base_convert($parts[1], 10, 36);

for($i = 0; $i < $length - strlen($nonce); $i++) {
for ($i = 0; $i < $length - strlen($nonce); $i++) {
$nonce .= base_convert(mt_rand(0, 35), 10, 36);
}

Expand Down
2 changes: 1 addition & 1 deletion src/XeroPHP/Remote/OAuth/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

class Exception extends \XeroPHP\Exception
{
}
}
2 changes: 1 addition & 1 deletion src/XeroPHP/Remote/OAuth/SignatureMethod/HMACSHA1.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ public static function generateSignature(array $config, $sbs, $secret)
{
return base64_encode(hash_hmac('sha1', $sbs, $secret, true));
}
}
}
28 changes: 19 additions & 9 deletions src/XeroPHP/Remote/Object.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ abstract class Object implements ObjectInterface, \JsonSerializable, \ArrayAcces
protected $_associated_objects;

/**
* Holds a ref to the application that was used to load the object, enables shorthand $object->save();
* Holds a ref to the application that was used to load the object,
* enables shorthand $object->save();
*
* @var Application $_application
*/
Expand All @@ -73,7 +74,8 @@ public function __construct(Application $application = null)
}

/**
* This should be compulsory in the constructor int he future, but will have to be like this for BC until the next major version.
* This should be compulsory in the constructor int he future,
* but will have to be like this for BC until the next major version.
*
* @param Application $application
*/
Expand Down Expand Up @@ -177,7 +179,8 @@ public function fromStringArray($input_array, $replace_data = false)
continue;
}

//Fix for an earlier assumption that the API didn't return more than two levels of nested objects.
//Fix for an earlier assumption that the API didn't return more than
//two levels of nested objects.
//Handles Invoice > Contact > Address etc. in one build.
if (is_array($input_array[$property]) && Helpers::isAssoc($input_array[$property]) === false) {
$collection = new Collection();
Expand Down Expand Up @@ -225,7 +228,6 @@ public function toStringArray()
} else {
$out[$property] = self::castToString($type, $this->_data[$property]);
}

}
return $out;
}
Expand Down Expand Up @@ -337,8 +339,13 @@ public function validate($check_children = true)

if ($mandatory) {
if (!isset($this->_data[$property]) || empty($this->_data[$property])) {
throw new Exception(sprintf('%s::$%s is mandatory and is either missing or empty.',
get_class($this), $property));
throw new Exception(
sprintf(
'%s::$%s is mandatory and is either missing or empty.',
get_class($this),
$property
)
);
}

if ($check_children) {
Expand All @@ -347,7 +354,6 @@ public function validate($check_children = true)
/** @var self $obj */
$obj = $this->_data[$property];
$obj->validate();

} elseif ($this->_data[$property] instanceof Collection) {
foreach ($this->_data[$property] as $element) {
if ($element instanceof Object) {
Expand All @@ -371,7 +377,9 @@ public function validate($check_children = true)
public function save()
{
if ($this->_application === null) {
throw new Exception('->save() is only available on objects that have an injected application context.');
throw new Exception(
'->save() is only available on objects that have an injected application context.'
);
}
$this->_application->save($this);
}
Expand All @@ -384,7 +392,9 @@ public function save()
public function delete()
{
if ($this->_application === null) {
throw new Exception('->delete() is only available on objects that have an injected application context.');
throw new Exception(
'->delete() is only available on objects that have an injected application context.'
);
}
$this->_application->delete($this);
}
Expand Down
14 changes: 7 additions & 7 deletions src/XeroPHP/Remote/ObjectInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,32 @@ interface ObjectInterface
*
* @return string|null
*/
static function getGUIDProperty();
public static function getGUIDProperty();

/**
* Get a list of properties
*
* @return array
*/
static function getProperties();
public static function getProperties();

/**
* Get a list of the supported HTTP Methods
*
* @return array
*/
static function getSupportedMethods();
public static function getSupportedMethods();

/**
* return the URI of the resource (if any)
*
* @return string
*/
static function getResourceURI();
public static function getResourceURI();

static function getRootNodeName();
public static function getRootNodeName();

static function getAPIStem();
public static function getAPIStem();

static function isPageable();
public static function isPageable();
}
9 changes: 6 additions & 3 deletions src/XeroPHP/Remote/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,16 @@ public function findElementErrors($element, $element_index)
switch ((string) $property) {
case 'ValidationErrors':
if (is_array($value)) {
foreach ($value as $error)
foreach ($value as $error) {
$this->element_errors[$element_index] = trim($error['Message'], '.');
}
}
break;
case 'Warnings':
if (is_array($value)) {
foreach ($value as $warning)
foreach ($value as $warning) {
$this->element_warnings[$element_index] = trim($warning['Message'], '.');
}
}
break;

Expand Down Expand Up @@ -284,7 +286,8 @@ public function parseJSON()
break;

default:
//Happy to make the assumption that there will only be one root node with > than 2D children.
//Happy to make the assumption that there will only be one
//root node with > than 2D children.
if (is_array($root_child)) {
foreach ($root_child as $element) {
$this->elements[] = $element;
Expand Down
Loading

0 comments on commit 524f4bc

Please sign in to comment.