Skip to content

Commit

Permalink
Refactor getXmlValue out to a Helper class. Rename OAuthHelper for co…
Browse files Browse the repository at this point in the history
…nsistency
  • Loading branch information
kenguest committed May 20, 2020
1 parent f9d8e88 commit 4750e97
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 58 deletions.
36 changes: 18 additions & 18 deletions Services/OpenStreetMap/Changeset.php
Expand Up @@ -180,9 +180,9 @@ public function begin(string $message): void
&& !empty($consumer_secret)
&& !empty($oauth_token_secret)
) {
include_once 'Services/OpenStreetMap/OAuthHelper.php';
$timest = Services_OpenStreetMap_OAuthHelper::getOauthTimestamp();
$nonce = Services_OpenStreetMap_OAuthHelper::getOauthNonce();
include_once 'Services/OpenStreetMap/Helper/OAuth.php';
$timest = Services_OpenStreetMap_Helper_OAuth::getOauthTimestamp();
$nonce = Services_OpenStreetMap_Helper_OAuth::getOauthNonce();

$oAuthArray = [
'oauth_consumer_key' => $oauth_consumer_key,
Expand All @@ -193,19 +193,19 @@ public function begin(string $message): void
'oauth_version' => '1.0'
];

$authString = Services_OpenStreetMap_OAuthHelper::assocArrayToString($oAuthArray);
$authString = Services_OpenStreetMap_Helper_OAuth::assocArrayToString($oAuthArray);
$encoded = '';
if (is_string($authString)) {
$encoded = rawurlencode($authString);
}
$hashString = HTTP_Request2::METHOD_PUT . '&' . rawurlencode($url) . '&' . $encoded;

$oAuthArray['oauth_signature'] = Services_OpenStreetMap_OAuthHelper::getOauthSignature(
$oAuthArray['oauth_signature'] = Services_OpenStreetMap_Helper_OAuth::getOauthSignature(
$consumer_secret . '&' . $oauth_token_secret,
$hashString
);

$authStr = 'OAuth ' . Services_OpenStreetMap_OAuthHelper::assocArrayToString(
$authStr = 'OAuth ' . Services_OpenStreetMap_Helper_OAuth::assocArrayToString(
$oAuthArray,
'=',
', ',
Expand Down Expand Up @@ -334,9 +334,9 @@ public function commit(): bool
&& !empty($consumer_secret)
&& !empty($oauth_token_secret)
) {
include_once 'Services/OpenStreetMap/OAuthHelper.php';
$timest = Services_OpenStreetMap_OAuthHelper::getOauthTimestamp();
$nonce = Services_OpenStreetMap_OAuthHelper::getOauthNonce();
include_once 'Services/OpenStreetMap/Helper/OAuth.php';
$timest = Services_OpenStreetMap_Helper_OAuth::getOauthTimestamp();
$nonce = Services_OpenStreetMap_Helper_OAuth::getOauthNonce();

$oAuthArray = [
'oauth_consumer_key' => $oauth_consumer_key,
Expand All @@ -347,19 +347,19 @@ public function commit(): bool
'oauth_version' => '1.0'
];

$oAuthString = Services_OpenStreetMap_OAuthHelper::assocArrayToString($oAuthArray);
$oAuthString = Services_OpenStreetMap_Helper_OAuth::assocArrayToString($oAuthArray);
$reUrl = rawurlencode($url);
$reAuthString = '';
if (is_string($oAuthString)) {
$reAuthString = rawurlencode($oAuthString);
}
$hashString = HTTP_Request2::METHOD_POST . '&' . $reUrl . '&' . $reAuthString;
$oAuthArray['oauth_signature'] = Services_OpenStreetMap_OAuthHelper::getOauthSignature(
$oAuthArray['oauth_signature'] = Services_OpenStreetMap_Helper_OAuth::getOauthSignature(
$consumer_secret . '&' . $oauth_token_secret,
$hashString
);

$authStr = 'OAuth ' . Services_OpenStreetMap_OAuthHelper::assocArrayToString(
$authStr = 'OAuth ' . Services_OpenStreetMap_Helper_OAuth::assocArrayToString(
$oAuthArray,
'=',
', ',
Expand Down Expand Up @@ -421,9 +421,9 @@ public function commit(): bool
&& !empty($consumer_secret)
&& !empty($oauth_token_secret)
) {
include_once 'Services_OpenStreetMap_OAuthHelper.php';
$timest = Services_OpenStreetMap_OAuthHelper::getOauthTimestamp();
$nonce = Services_OpenStreetMap_OAuthHelper::getOauthNonce();
include_once 'Services_OpenStreetMap_Helper_OAuth.php';
$timest = Services_OpenStreetMap_Helper_OAuth::getOauthTimestamp();
$nonce = Services_OpenStreetMap_Helper_OAuth::getOauthNonce();

$oAuthArray = [
'oauth_consumer_key' => $oauth_consumer_key,
Expand All @@ -434,7 +434,7 @@ public function commit(): bool
'oauth_version' => '1.0'
];

$oauthString = Services_OpenStreetMap_OAuthHelper::assocArrayToString($oAuthArray);
$oauthString = Services_OpenStreetMap_Helper_OAuth::assocArrayToString($oAuthArray);
$reUrl = rawurlencode($url);
$reAuthString = '';
if (is_string($oauthString)) {
Expand All @@ -445,12 +445,12 @@ public function commit(): bool
$hashString = HTTP_Request2::METHOD_PUT . '&' . $reUrl . '&' . $reAuthString;
}

$oAuthArray['oauth_signature'] = Services_OpenStreetMap_OAuthHelper::getOauthSignature(
$oAuthArray['oauth_signature'] = Services_OpenStreetMap_Helper_OAuth::getOauthSignature(
$consumer_secret . '&' . $oauth_token_secret,
$hashString
);

$authStr = 'OAuth ' . Services_OpenStreetMap_OAuthHelper::assocArrayToString(
$authStr = 'OAuth ' . Services_OpenStreetMap_Helper_OAuth::assocArrayToString(
$oAuthArray,
'=',
', ',
Expand Down
48 changes: 13 additions & 35 deletions Services/OpenStreetMap/Config.php
Expand Up @@ -541,71 +541,72 @@ private function _checkCapabilities($capabilities): bool
return false;
}

$this->minVersion = (float) $this->getXmlValue($xml, 'version', 'minimum');
$this->maxVersion = (float) $this->getXmlValue($xml, 'version', 'maximum');
$helper = new Services_OpenStreetMap_Helper_Xml();
$this->minVersion = (float) $helper->getValue($xml, 'version', 'minimum');
$this->maxVersion = (float) $helper->getValue($xml, 'version', 'maximum');
if ($this->minVersion > $this->api_version
|| $this->api_version > $this->maxVersion
) {
throw new Services_OpenStreetMap_Exception(
'Specified API Version ' . $this->api_version . ' not supported.'
);
}
$this->timeout = (int) $this->getXmlValue($xml, 'timeout', 'seconds');
$this->timeout = (int) $helper->getValue($xml, 'timeout', 'seconds');

//changesets
$this->changesetMaximumElements = (int) $this->getXmlValue(
$this->changesetMaximumElements = (int) $helper->getValue(
$xml,
'changesets',
'maximum_elements'
);

// Maximum number of nodes per way.
$this->waynodesMaximum = (int) $this->getXmlValue(
$this->waynodesMaximum = (int) $helper->getValue(
$xml,
'waynodes',
'maximum'
);

// Number of tracepoints per way.
$this->tracepointsPerPage = (int) $this->getXmlValue(
$this->tracepointsPerPage = (int) $helper->getValue(
$xml,
'tracepoints',
'per_page'
);

// Max size of area that can be downloaded in one request.
$this->areaMaximum = (float) $this->getXmlValue(
$this->areaMaximum = (float) $helper->getValue(
$xml,
'area',
'maximum'
);

$this->noteAreaMaximum = (int) $this->getXmlValue(
$this->noteAreaMaximum = (int) $helper->getValue(
$xml,
'note_area',
'maximum'
);

$this->databaseStatus = $this->getXmlValue(
$this->databaseStatus = $helper->getValue(
$xml,
'status',
'database'
);

$this->apiStatus = $this->getXmlValue(
$this->apiStatus = $helper->getValue(
$xml,
'status',
'api'
);

$this->gpxStatus = $this->getXmlValue(
$this->gpxStatus = $helper->getValue(
$xml,
'status',
'gpx'
);

// What generated the XML.
$this->generator = '' . $this->getXmlValue(
$this->generator = '' . $helper->getValue(
$xml,
'osm',
'generator',
Expand Down Expand Up @@ -776,27 +777,4 @@ public function getGenerator(): string
{
return $this->generator;
}

/**
* Given SimpleXMLElement, retrieve tag value.
*
* @param SimpleXMLElement $xml Object
* @param string $tag name of tag
* @param string $attribute name of attribute
* @param mixed $default default value, optional
*
* @return string
*/
public function getXmlValue(
SimpleXMLElement $xml,
string $tag,
string $attribute,
$default = null
):?string {
$obj = $xml->xpath('//' . $tag);
if (empty($obj)) {
return $default;
}
return $obj[0]->attributes()->$attribute;
}
}
@@ -1,6 +1,6 @@
<?php
/**
* OAuthHelper.php
* Helper_OAuth.php
*
* PHP Version 5
*
Expand All @@ -9,19 +9,19 @@
* @author Ken Guest <kguest@php.net>
* @license BSD http://www.opensource.org/licenses/bsd-license.php
* @version Release: @package_version@
* @link OAuthHelper.php
* @link Helper_OAuth.php
*/

/**
* Services_OpenStreetMap_OAuthHelper
* Services_OpenStreetMap_Helper_OAuth
*
* @category Services
* @package Services_OpenStreetMap
* @author Valery Khvalov <khvalov@tut.by>
* @license BSD http://www.opensource.org/licenses/bsd-license.php
* @link OAuthHelper.php
* @link Helper_OAuth.php
*/
class Services_OpenStreetMap_OAuthHelper
class Services_OpenStreetMap_Helper_OAuth
{
/**
* Return time-stamp for oauth use
Expand Down
41 changes: 41 additions & 0 deletions Services/OpenStreetMap/Helper/Xml.php
@@ -0,0 +1,41 @@
<?php
/**
* Xml.php
* 20-May-2020
*
* PHP Version 5
*
* @category Xml
* @package Xml
* @author Ken Guest <ken@linux.ie>
* @license GPL (see http://www.gnu.org/licenses/gpl.txt)
* @version CVS: <cvs_id>
* @link Xml.php
* @todo
*/

class Services_OpenStreetMap_Helper_Xml
{
/**
* Given SimpleXMLElement, retrieve tag value.
*
* @param SimpleXMLElement $xml Object
* @param string $tag name of tag
* @param string $attribute name of attribute
* @param mixed $default default value, optional
*
* @return string
*/
public function getValue(
SimpleXMLElement $xml,
string $tag,
string $attribute,
$default = null
):?string {
$obj = $xml->xpath('//' . $tag);
if (empty($obj)) {
return $default;
}
return $obj[0]->attributes()->$attribute;
}
}

0 comments on commit 4750e97

Please sign in to comment.