From 7fc55bfa81f16f8eb4d58c8b123aa4ffbf3a2ba1 Mon Sep 17 00:00:00 2001 From: Derek Rosenzweig Date: Fri, 19 Jun 2015 19:36:35 -0400 Subject: [PATCH 1/7] Added .gitignore file to ignore PhpStorm project files. Cleaned up formatting of VoteSmart class. --- .gitignore | 1 + VoteSmart.php | 186 +++++++++++++++++++++++--------------------------- 2 files changed, 86 insertions(+), 101 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..723ef36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file diff --git a/VoteSmart.php b/VoteSmart.php index 52b565b..a8cd7ef 100644 --- a/VoteSmart.php +++ b/VoteSmart.php @@ -17,108 +17,92 @@ * contribution for the slimmed down version of this lib. * */ -class VoteSmart { - - protected $iface; // Interface(URL) used to gain the data - protected $xml; // Raw XML - protected $xmlObj; // SimpleXML object - - /** - * function __construct - * - * Initialize object(optional) - * - * @param string $method optional 'CandidateBio.getBio' - * @param array $args optional Array('candidateId' => '54321') - */ - public function __construct($method = null, $args = null) { - - if ($method && $args) { - - $this->query($method, $args); - - } - - } - - /** - * function getXml - * - * Return raw XML string - * - * @return string - */ - public function getXml() { - - return $this->xml; - - } - - /** - * function getXmlObj - * - * Return SimpleXML object - * - * @return object SimpleXMLElement - */ - public function getXmlObj() { - - return $this->xmlObj; - +class VoteSmart +{ + protected $iface; // Interface(URL) used to gain the data + protected $xml; // Raw XML + protected $xmlObj; // SimpleXML object + + /** + * function __construct + * + * Initialize object(optional) + * + * @param string $method optional 'CandidateBio.getBio' + * @param array $args optional Array('candidateId' => '54321') + */ + public function __construct($method = null, $args = null) + { + if ($method && $args) { + $this->query($method, $args); } - - /** - * function getIface - * - * Return string of URL queried - * - * @return string - */ - public function getIface() { - - return $this->iface; - + } + + /** + * function query + * + * Query API backend and return SimpleXML object. This + * function can be reused repeatedly + * + * @param string $method CandidateBio.getBio' + * @param array $args Array('candidateId' => '54321') + * @return object SimpleXMLElement + */ + public function query($method, $args = Array()) + { + $terms = ""; + + if (! empty($args)) { + foreach ($args as $n => $v) { + $terms .= '&' . $n . '=' . $v; + } } - - /** - * function query - * - * Query API backend and return SimpleXML object. This - * function can be reused repeatedly - * - * @param string $method CandidateBio.getBio' - * @param array $args Array('candidateId' => '54321') - * @return object SimpleXMLElement - */ - public function query($method, $args = Array()) { - - $terms = ""; - - if(!empty($args)) { - - foreach($args as $n => $v) { - - $terms .= '&' . $n . '=' . $v; - - } - } - - $this->iface = _APISERVER_ . "/" . $method . "?key=" . _KEY_ . "&o=" . _OUTPUT_ . $terms; - - if (!$this->xml = file_get_contents($this->iface)) { - - return false; - - } else { - - // Let's use the SimpleXML to drop the whole XML - // output into an object we can later interact with easilly - $this->xmlObj = new SimpleXMLElement($this->xml, LIBXML_NOCDATA); - - return $this->xmlObj; - - } - + $this->iface = _APISERVER_ . "/" . $method . "?key=" . _KEY_ . "&o=" . _OUTPUT_ . $terms; + + if (! $this->xml = file_get_contents($this->iface)) { + return false; + } else { + // Let's use the SimpleXML to drop the whole XML + // output into an object we can later interact with easilly + $this->xmlObj = new SimpleXMLElement($this->xml, LIBXML_NOCDATA); + + return $this->xmlObj; } - + } + + /** + * function getXml + * + * Return raw XML string + * + * @return string + */ + public function getXml() + { + return $this->xml; + } + + /** + * function getXmlObj + * + * Return SimpleXML object + * + * @return object SimpleXMLElement + */ + public function getXmlObj() + { + return $this->xmlObj; + } + + /** + * function getIface + * + * Return string of URL queried + * + * @return string + */ + public function getIface() + { + return $this->iface; + } } From c1e188cf9126f1f5f0f3d7e1c766879bed63f740 Mon Sep 17 00:00:00 2001 From: Derek Rosenzweig Date: Fri, 19 Jun 2015 21:04:41 -0400 Subject: [PATCH 2/7] Refactored VoteSmart object, removed dependency on config.php. The major change here is that you can no longer query directly from the constructor. The constructor now takes in 2 parameters: 1.) the output type, with a default of 'XML' 2.) the location in the global $_ENV array where to check for the API token supplied by VoteSmart is stored, with a default of 'VOTESMART_API_KEY' You can also set these directly using the methods setOutputType(), setEnvKey() and setApiToken(). --- VoteSmart.php | 222 ++++++++++++++++++++++++++++++++++++++------------ config.php | 19 ----- 2 files changed, 172 insertions(+), 69 deletions(-) delete mode 100644 config.php diff --git a/VoteSmart.php b/VoteSmart.php index a8cd7ef..4f96662 100644 --- a/VoteSmart.php +++ b/VoteSmart.php @@ -1,12 +1,11 @@ */ class VoteSmart { - protected $iface; // Interface(URL) used to gain the data - protected $xml; // Raw XML - protected $xmlObj; // SimpleXML object + /** + * The API endpoint where requests are sent. + * + * @static + * @var string + */ + public static $API_SERVER = "http://api.votesmart.org"; /** - * function __construct + * Possible return formats from API endpoint. * - * Initialize object(optional) + * @static + * @var array + */ + public static $OUTPUT_TYPES = [ + 'XML' + ]; + + /** + * The full query URL. * - * @param string $method optional 'CandidateBio.getBio' - * @param array $args optional Array('candidateId' => '54321') + * @var string */ - public function __construct($method = null, $args = null) - { - if ($method && $args) { - $this->query($method, $args); - } - } + protected $iface; /** - * function query + * function getIface * - * Query API backend and return SimpleXML object. This - * function can be reused repeatedly + * Return string of URL queried * - * @param string $method CandidateBio.getBio' - * @param array $args Array('candidateId' => '54321') - * @return object SimpleXMLElement + * @return string */ - public function query($method, $args = Array()) + public function getIface() { - $terms = ""; - - if (! empty($args)) { - foreach ($args as $n => $v) { - $terms .= '&' . $n . '=' . $v; - } - } - $this->iface = _APISERVER_ . "/" . $method . "?key=" . _KEY_ . "&o=" . _OUTPUT_ . $terms; - - if (! $this->xml = file_get_contents($this->iface)) { - return false; - } else { - // Let's use the SimpleXML to drop the whole XML - // output into an object we can later interact with easilly - $this->xmlObj = new SimpleXMLElement($this->xml, LIBXML_NOCDATA); - - return $this->xmlObj; - } + return $this->iface; } + /** + * Raw XML + * + * @var string + */ + protected $xml; + /** * function getXml * @@ -82,10 +77,17 @@ public function getXml() return $this->xml; } + /** + * SimpleXML object + * + * @var SimpleXMLElement + */ + protected $xmlObj; + /** * function getXmlObj * - * Return SimpleXML object + * Return SimpleXMLElement object * * @return object SimpleXMLElement */ @@ -95,14 +97,134 @@ public function getXmlObj() } /** - * function getIface + * The array key in $_ENV where you are storing your VoteSmart API token. * - * Return string of URL queried + * @var string + */ + protected $envKey = null; + + /** + * Sets the array key in $_ENV where you are storing your VoteSmart API token, and updates + * the apiKey stored in this object to point to the new location in $_ENV. * - * @return string + * @throws InvalidArgumentException iff the output type isn't a string + * @throws Exception iff the API token can't be found in $_ENV + * + * @param string $envKey The array key in $_ENV where the VoteSmart token exists. */ - public function getIface() + public function setEnvKey($envKey) { - return $this->iface; + if (! is_string($envKey)) { + throw new InvalidArgumentException( + "The environment key '{$envKey}' is expecting a string" + ); + } + if (empty($_ENV[$envKey])) { + throw new Exception( + "VoteSmart requires an API authentication token. Place it in your application's `\$_ENV['".$envKey."']` variable." + ); + } + $this->envKey = $envKey; + $apiToken = $_ENV[$this->envKey]; + $this->setApiToken($apiToken); + } + + /** + * Authentication token provided to you by VoteSmart. Place it in your application's + * `$_ENV['VOTESMART_API_KEY']` variable so that it is globally available. For example, + * in the Laravel framework, you would put this in your .env.php file. + * + * If you've stored your key in another $_ENV key already, you can pass in the key where + * the API token can be found in the constructor. + * + * @var string + */ + protected $apiToken = null; + + /** + * Sets the API token. + * + * @param string $apiToken Sets the API key. Required. + */ + public function setApiToken($apiToken) + { + $this->apiToken = $apiToken; + } + + /** + * The type of output to request from VoteSmart. + * + * @var string + */ + protected $outputType = null; + + /** + * Set the expected output type. + * + * @throws InvalidArgumentException iff the output type isn't a string + * @throws Exception iff the output type isn't supported + * + * @param string $outputType The expected output type from VoteSmart. + */ + public function setOutputType($outputType) + { + if (! is_string($outputType)) { + throw new InvalidArgumentException( + "The output type '{$outputType}' is unsupported." + ); + } + if (! in_array($outputType, static::$OUTPUT_TYPES)) { + throw new Exception( + "The output type '{$outputType}' is unsupported." + ); + } + $this->outputType = $outputType; + } + + /** + * function __construct + * + * Initialize object. + * + * @param string $outputType optional The type of output to request from VoteSmart. Default is 'XML'. + * @param string $envKey optional Key of your Authentication key stored in $_ENV var. Default is 'VOTESMART_API_KEY'. + */ + public function __construct($outputType = 'XML', $envKey = 'VOTESMART_API_KEY') + { + $this->setEnvKey($envKey); + $this->setOutputType($outputType); + } + + /** + * function query + * + * Query API backend and return SimpleXML object. This + * function can be reused repeatedly + * + * @param string $method required 'CandidateBio.getBio' + * @param array $args optional Array('candidateId' => '54321') + * + * @return false|SimpleXMLElement false if it can't get the contents, a SimpleXMLElement otherwise + */ + public function query($method, $args = Array()) + { + $terms = ""; + + if (! empty($args)) { + foreach ($args as $n => $v) { + $terms .= "&{$n}={$v}"; + } + } + $this->iface = static::$API_SERVER . "/" . $method . "?key=" . $this->apiToken . "&o=" . $this->outputType . $terms; + + if (! $this->xml = file_get_contents($this->iface)) { + return false; + } else { + // Let's use the SimpleXML to drop the whole XML + // output into an object we can later interact with easily + $this->xmlObj = new SimpleXMLElement($this->xml, LIBXML_NOCDATA); + + return $this->xmlObj; + } } } diff --git a/config.php b/config.php deleted file mode 100644 index 319733f..0000000 --- a/config.php +++ /dev/null @@ -1,19 +0,0 @@ -= 1) { - error_reporting(E_ALL ^ E_NOTICE); // All but notice errors -} else { - error_reporting(E_NONE); -} - -define("_APISERVER_", "http://api.votesmart.org"); // Without trailing slash and with protocol From 2f5bcb8fe95d1ec674f03afb2d676360f9d3ab48 Mon Sep 17 00:00:00 2001 From: Derek Rosenzweig Date: Fri, 19 Jun 2015 21:24:00 -0400 Subject: [PATCH 3/7] Updated the README with updated information. This refactor is not backwards compatible. --- README.rst | 66 +++++++++++++++++++++++++++++------------------------- 1 file changed, 36 insertions(+), 30 deletions(-) diff --git a/README.rst b/README.rst index bacf94d..5dac734 100644 --- a/README.rst +++ b/README.rst @@ -1,6 +1,6 @@ php-votesmart ======================= -A PHP library for use with the Vote Smart API(http://votesmart.org/share/api). +A PHP library for use with the Vote Smart API (http://votesmart.org/share/api). ------------ Requirements @@ -11,43 +11,49 @@ The PHP libraries require PHP 5 with the SimpleXML extension and ``allow_url_fop ------------ Usage ------------ -Using the libraries is fairly simple. You initialize the object with the name one of the methods and any arguments needed in an array. Then a call go ``getXmlObj()`` can be made to retrieve a SimpleXML object to work with. Let's say you wanted to get information on a bill...:: +Using the libraries is fairly simple. You initialize the object, and call ``query()`` to make the call and retrieve the +SimpleXMLElement object created from the response from VoteSmart. If there is no response, or the request fails, +the output is boolean ``false``. +. Then a call go ``getXmlObj()`` can be made to retrieve a SimpleXML object to work with.:: // Initialize the VoteSmart object - $obj = new VoteSmart( - 'CandidateBio.getBio', - Array( - 'candidateId' => 9026 - )); - - // Get the SimpleXML object + $obj = new VoteSmart(); + + // Make the query with required parameters, with the name of one of the methods, and any required or optional + // arguments in an array. Let's say you wanted to get information on a bill. For example: + $x = $obj->query( + 'CandidateBio.getBio', + Array( + 'candidateId' => 9026 + ) + ); + + // Once a query has been made, you can also get the stored SimpleXMLElement object $x = $obj->getXmlObj(); Now ``$xml_object`` is a SimpleXML object representative of the XML structure. Here's a small cut from the XML itself.:: - - Project Vote Smart - Bio - Rep. Stephen Scalise - http://votesmart.org/bio.php?can_id=9026 - - - 9026 - - - http://votesmart.org/canphoto/9026.JPG - Stephen - Steve - J. - Scalise - - - 10/06/1965 - - - Male - [...] - + + Project Vote Smart - Bio - Rep. Stephen Scalise + http://votesmart.org/bio.php?can_id=9026 + + + 9026 + + http://votesmart.org/canphoto/9026.JPG + Stephen + Steve + J. + Scalise + + 10/06/1965 + + + Male + [...] + Let's say you wanted to get the candidateId of the candidate. You could simply access it like this:: From 3315b9adbbb4dce0fe3e85840fbc42a3fde3ab50 Mon Sep 17 00:00:00 2001 From: Derek Rosenzweig Date: Fri, 19 Jun 2015 21:25:51 -0400 Subject: [PATCH 4/7] fixing a small flub in the previous commit --- README.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.rst b/README.rst index 5dac734..cd95432 100644 --- a/README.rst +++ b/README.rst @@ -13,8 +13,7 @@ Usage ------------ Using the libraries is fairly simple. You initialize the object, and call ``query()`` to make the call and retrieve the SimpleXMLElement object created from the response from VoteSmart. If there is no response, or the request fails, -the output is boolean ``false``. -. Then a call go ``getXmlObj()`` can be made to retrieve a SimpleXML object to work with.:: +the output is boolean ``false``:: // Initialize the VoteSmart object $obj = new VoteSmart(); From b886346c636931daca71b4751403da2a7fd4c6b6 Mon Sep 17 00:00:00 2001 From: Derek Rosenzweig Date: Fri, 19 Jun 2015 21:59:19 -0400 Subject: [PATCH 5/7] Added basic JSON functionality. You can now pass in 'JSON' as a valid outputType. Updated the README to contain more useful information. --- README.rst | 22 +++++++--- VoteSmart.php | 117 ++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 116 insertions(+), 23 deletions(-) diff --git a/README.rst b/README.rst index cd95432..357913b 100644 --- a/README.rst +++ b/README.rst @@ -11,13 +11,21 @@ The PHP libraries require PHP 5 with the SimpleXML extension and ``allow_url_fop ------------ Usage ------------ -Using the libraries is fairly simple. You initialize the object, and call ``query()`` to make the call and retrieve the -SimpleXMLElement object created from the response from VoteSmart. If there is no response, or the request fails, -the output is boolean ``false``:: +First, you need your API token. Once you get that from VoteSmart.org, store the API token in a file which is loaded +by your application. It should be defined as:: - // Initialize the VoteSmart object + $_ENV['VOTESMART_API_KEY'] + +Using the libraries is fairly simple. You initialize the object, and call ``query()`` to make the call parse the +response from VoteSmart. If there is no response, or the request fails, the output is boolean ``false``:: + + // Initialize the VoteSmart object. The default output type is XML. $obj = new VoteSmart(); + // You can also pass in optional to change the expected output type, and the location where the API token is located + // inside the $_ENV global variable. In this case, your VoteSmart API key would be stored in $_ENV['SOME_KEY']. + $obj = new VoteSmart('JSON', 'SOME_KEY'); + // Make the query with required parameters, with the name of one of the methods, and any required or optional // arguments in an array. Let's say you wanted to get information on a bill. For example: $x = $obj->query( @@ -27,9 +35,13 @@ the output is boolean ``false``:: ) ); - // Once a query has been made, you can also get the stored SimpleXMLElement object + // Once a query has been made, you can also get the stored decoded response. + // If your output type was XML, you could access the SimpleXMLElement object via $x = $obj->getXmlObj(); + // If your output type was JSON, you could access the array via + $x = $obj->getJsonObj(); + Now ``$xml_object`` is a SimpleXML object representative of the XML structure. Here's a small cut from the XML itself.:: diff --git a/VoteSmart.php b/VoteSmart.php index 4f96662..738715f 100644 --- a/VoteSmart.php +++ b/VoteSmart.php @@ -21,6 +21,12 @@ */ class VoteSmart { + //-------------------------------------------------------------------------- + // + // Static Variables + // + //-------------------------------------------------------------------------- + /** * The API endpoint where requests are sent. * @@ -36,9 +42,16 @@ class VoteSmart * @var array */ public static $OUTPUT_TYPES = [ - 'XML' + 'XML', + 'JSON' ]; + //-------------------------------------------------------------------------- + // + // Variables and get/set functions + // + //-------------------------------------------------------------------------- + /** * The full query URL. * @@ -85,17 +98,49 @@ public function getXml() protected $xmlObj; /** - * function getXmlObj - * * Return SimpleXMLElement object * - * @return object SimpleXMLElement + * @return SimpleXMLElement */ public function getXmlObj() { return $this->xmlObj; } + /** + * Raw JSON + * + * @var string + */ + protected $json; + + /** + * Return raw JSON string + * + * @return string + */ + public function getJson() + { + return $this->json; + } + + /** + * Decoded JSON data + * + * @var array + */ + protected $jsonObj; + + /** + * Return decoded JSON array. + * + * @return array + */ + public function getJsonObj() + { + return $this->jsonObj; + } + /** * The array key in $_ENV where you are storing your VoteSmart API token. * @@ -181,30 +226,62 @@ public function setOutputType($outputType) $this->outputType = $outputType; } + //-------------------------------------------------------------------------- + // + // Methods + // + //-------------------------------------------------------------------------- + + /* Private Methods */ + + /* Protected Methods */ + /** - * function __construct + * Use SimpleXML to drop the whole XML output into an object we can later interact with easily. + * Stores both the XML and the constructed SimpleXMLElement object. * - * Initialize object. + * @param string $xml + */ + protected function parseXml($xml) + { + $this->xml = $xml; + $this->xmlObj = new SimpleXMLElement($this->xml, LIBXML_NOCDATA); + } + + /** + * Take in a JSON string and decode it. Store both the JSON and the decoded array. + * + * @param string $json + */ + protected function parseJson($json) + { + $this->json = $json; + $this->jsonObj = json_decode($this->json); + } + + /* Public Methods */ + + /** + * Initialize the VoteSmart object and ready it to make queries. * * @param string $outputType optional The type of output to request from VoteSmart. Default is 'XML'. * @param string $envKey optional Key of your Authentication key stored in $_ENV var. Default is 'VOTESMART_API_KEY'. */ public function __construct($outputType = 'XML', $envKey = 'VOTESMART_API_KEY') { - $this->setEnvKey($envKey); $this->setOutputType($outputType); + $this->setEnvKey($envKey); } /** - * function query - * - * Query API backend and return SimpleXML object. This - * function can be reused repeatedly + * Query API backend and parse the response, either as JSON or XML depending on the instance's outputType + * attribute. Returns false if it can't get the contents, a SimpleXMLElement if the output type is XML, + * or an array if the output type is JSON. * * @param string $method required 'CandidateBio.getBio' * @param array $args optional Array('candidateId' => '54321') * - * @return false|SimpleXMLElement false if it can't get the contents, a SimpleXMLElement otherwise + * @return false|SimpleXMLElement|array */ public function query($method, $args = Array()) { @@ -217,14 +294,18 @@ public function query($method, $args = Array()) } $this->iface = static::$API_SERVER . "/" . $method . "?key=" . $this->apiToken . "&o=" . $this->outputType . $terms; - if (! $this->xml = file_get_contents($this->iface)) { + $response = file_get_contents($this->iface); + if (! $response) { return false; - } else { - // Let's use the SimpleXML to drop the whole XML - // output into an object we can later interact with easily - $this->xmlObj = new SimpleXMLElement($this->xml, LIBXML_NOCDATA); + } - return $this->xmlObj; + if ($this->outputType == 'JSON') { + $this->parseJson($response); + return $this->responseArr; } + + // Parse as XML by default. + $this->parseXml($response); + return $this->xmlObj; } } From 43ddc911e5096db4595de869f2484f52b8faa360 Mon Sep 17 00:00:00 2001 From: Derek Rosenzweig Date: Fri, 19 Jun 2015 23:33:40 -0400 Subject: [PATCH 6/7] Refactored VoteSmart class by renaming it Api, and giving it a namespace of VoteSmart. Moved Api class to src directory Created composer.json file and composer.lock files Updated .gitignore to ignore Composer created vendor directory --- .gitignore | 3 ++- composer.json | 12 ++++++++++++ composer.lock | 19 +++++++++++++++++++ VoteSmart.php => src/Api.php | 6 ++++-- 4 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 composer.json create mode 100644 composer.lock rename VoteSmart.php => src/Api.php (99%) diff --git a/.gitignore b/.gitignore index 723ef36..bcc0b7a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.idea \ No newline at end of file +.idea +vendor/ \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..98d2cad --- /dev/null +++ b/composer.json @@ -0,0 +1,12 @@ +{ + "name": "ozzyogkush/php-votesmart", + "description": "A PHP library for accessing VoteSmart.org via their API.", + "keywords": ["votesmart", "php", "api"], + "license": "BSD", + "require": { + "php": ">=5.3.0" + }, + "autoload": { + "psr-4": {"VoteSmart\\" : "src/"} + } +} \ No newline at end of file diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..8a8b001 --- /dev/null +++ b/composer.lock @@ -0,0 +1,19 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "This file is @generated automatically" + ], + "hash": "189f86c01904cd88bec379f45fa94b3b", + "packages": [], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": { + "php": ">=5.3.0" + }, + "platform-dev": [] +} diff --git a/VoteSmart.php b/src/Api.php similarity index 99% rename from VoteSmart.php rename to src/Api.php index 738715f..3deeee8 100644 --- a/VoteSmart.php +++ b/src/Api.php @@ -1,4 +1,4 @@ - + * @class Api + * @name Api */ -class VoteSmart +class Api { //-------------------------------------------------------------------------- // From 69bf2d06d2bfc4a182407f7bb946f8ba93bfe28c Mon Sep 17 00:00:00 2001 From: Derek Rosenzweig Date: Fri, 19 Jun 2015 23:59:13 -0400 Subject: [PATCH 7/7] Small bug fix to take namespaced classes into account --- src/Api.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Api.php b/src/Api.php index 3deeee8..9f2bf1f 100644 --- a/src/Api.php +++ b/src/Api.php @@ -95,14 +95,14 @@ public function getXml() /** * SimpleXML object * - * @var SimpleXMLElement + * @var \SimpleXMLElement */ protected $xmlObj; /** * Return SimpleXMLElement object * - * @return SimpleXMLElement + * @return \SimpleXMLElement */ public function getXmlObj() { @@ -154,20 +154,20 @@ public function getJsonObj() * Sets the array key in $_ENV where you are storing your VoteSmart API token, and updates * the apiKey stored in this object to point to the new location in $_ENV. * - * @throws InvalidArgumentException iff the output type isn't a string - * @throws Exception iff the API token can't be found in $_ENV + * @throws \InvalidArgumentException iff the output type isn't a string + * @throws \Exception iff the API token can't be found in $_ENV * * @param string $envKey The array key in $_ENV where the VoteSmart token exists. */ public function setEnvKey($envKey) { if (! is_string($envKey)) { - throw new InvalidArgumentException( + throw new \InvalidArgumentException( "The environment key '{$envKey}' is expecting a string" ); } if (empty($_ENV[$envKey])) { - throw new Exception( + throw new \Exception( "VoteSmart requires an API authentication token. Place it in your application's `\$_ENV['".$envKey."']` variable." ); } @@ -208,20 +208,20 @@ public function setApiToken($apiToken) /** * Set the expected output type. * - * @throws InvalidArgumentException iff the output type isn't a string - * @throws Exception iff the output type isn't supported + * @throws \InvalidArgumentException iff the output type isn't a string + * @throws \Exception iff the output type isn't supported * * @param string $outputType The expected output type from VoteSmart. */ public function setOutputType($outputType) { if (! is_string($outputType)) { - throw new InvalidArgumentException( + throw new \InvalidArgumentException( "The output type '{$outputType}' is unsupported." ); } if (! in_array($outputType, static::$OUTPUT_TYPES)) { - throw new Exception( + throw new \Exception( "The output type '{$outputType}' is unsupported." ); } @@ -247,7 +247,7 @@ public function setOutputType($outputType) protected function parseXml($xml) { $this->xml = $xml; - $this->xmlObj = new SimpleXMLElement($this->xml, LIBXML_NOCDATA); + $this->xmlObj = new \SimpleXMLElement($this->xml, LIBXML_NOCDATA); } /** @@ -283,7 +283,7 @@ public function __construct($outputType = 'XML', $envKey = 'VOTESMART_API_KEY') * @param string $method required 'CandidateBio.getBio' * @param array $args optional Array('candidateId' => '54321') * - * @return false|SimpleXMLElement|array + * @return false|\SimpleXMLElement|array */ public function query($method, $args = Array()) {