diff --git a/README.md b/README.md index 0f46cfc..d07e4ed 100644 --- a/README.md +++ b/README.md @@ -17,8 +17,7 @@ Require the Clockwork library: require 'class-Clockwork.php'; ### Sending a message - - $clockwork = new Clockwork( $API_KEY ); + $clockwork = new \Clockwork\Clockwork( $API_KEY ); $message = array( 'to' => '441234567891', 'message' => 'This is a test!' ); $result = $clockwork->send( $message ); @@ -26,7 +25,7 @@ Require the Clockwork library: We recommend you use batch sizes of 500 messages or fewer. By limiting the batch size it prevents any timeouts when sending. - $clockwork = new Clockwork( $API_KEY ); + $clockwork = new \Clockwork\Clockwork( $API_KEY ); $messages = array( array( 'to' => '441234567891', 'message' => 'This is a test!' ), array( 'to' => '441234567892', 'message' => 'This is a test 2!' ) @@ -103,7 +102,7 @@ For example, if you send to invalid phone number "abc": Check your available SMS balance: - $clockwork = new Clockwork( $API_KEY ); + $clockwork = new \Clockwork\Clockwork( $API_KEY ); $clockwork->checkBalance(); This will return: @@ -121,11 +120,11 @@ The Clockwork wrapper will throw a `ClockworkException` if the entire call faile try { - $clockwork = new Clockwork( 'invalid_key' ); + $clockwork = new \Clockwork\Clockwork( 'invalid_key' ); $message = array( 'to' => 'abc', 'message' => 'This is a test!' ); $result = $clockwork->send( $message ); } - catch( ClockworkException $e ) + catch( \Clockwork\ClockworkException $e ) { print $e->getMessage(); // Invalid API Key @@ -183,7 +182,7 @@ Set option values individually on each message. In this example, one message will be from Clockwork and the other from 84433: - $clockwork = new Clockwork( $API_KEY, $options ); + $clockwork = new \Clockwork\Clockwork( $API_KEY, $options ); $messages = array( array( 'to' => '441234567891', 'message' => 'This is a test!', 'from' => 'Clockwork' ), array( 'to' => '441234567892', 'message' => 'This is a test 2!', 'from' => '84433' ) @@ -207,7 +206,7 @@ If you're seeing this error there are two fixes available, the first is easy, si #### Disable SSL on Clockwork calls $options = array( 'ssl' => false ); - $clockwork = new Clockwork( $API_KEY, $options ); + $clockwork = new \Clockwork\Clockwork( $API_KEY, $options ); #### Setup SSL root certificates on your server diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..50c9208 --- /dev/null +++ b/composer.json @@ -0,0 +1,12 @@ +{ + "name": "weemen/clockwork-php", + "minimum-stability":"dev", + "require-dev": { + "phpunit/phpunit": "3.7.*" + }, + "autoload": { + "psr-0": { + "Clockwork\\": "src/" + } + } +} \ No newline at end of file diff --git a/class-Clockwork.php b/src/Clockwork/Clockwork.php similarity index 94% rename from class-Clockwork.php rename to src/Clockwork/Clockwork.php index b38b886..2bfcfa3 100755 --- a/class-Clockwork.php +++ b/src/Clockwork/Clockwork.php @@ -9,9 +9,8 @@ * @version 1.3.0 */ -if ( !class_exists('ClockworkException') ) { - require_once('class-ClockworkException.php'); -} +namespace Clockwork; +use ClockworkException; /** * Main Clockwork API Class @@ -173,7 +172,7 @@ public function send(array $sms) { $sms = array($sms); } - $req_doc = new DOMDocument('1.0', 'UTF-8'); + $req_doc = new \DOMDocument('1.0', 'UTF-8'); $root = $req_doc->createElement('Message'); $req_doc->appendChild($root); @@ -251,7 +250,7 @@ public function send(array $sms) { $req_xml = $req_doc->saveXML(); $resp_xml = $this->postToClockwork(self::API_SMS_METHOD, $req_xml); - $resp_doc = new DOMDocument(); + $resp_doc = new \DOMDocument(); $resp_doc->loadXML($resp_xml); $response = array(); @@ -317,7 +316,7 @@ public function send(array $sms) { */ public function checkCredit() { // Create XML doc for request - $req_doc = new DOMDocument('1.0', 'UTF-8'); + $req_doc = new \DOMDocument('1.0', 'UTF-8'); $root = $req_doc->createElement('Credit'); $req_doc->appendChild($root); $root->appendChild($req_doc->createElement('Key', $this->key)); @@ -327,7 +326,7 @@ public function checkCredit() { $resp_xml = $this->postToClockwork(self::API_CREDIT_METHOD, $req_xml); // Create XML doc for response - $resp_doc = new DOMDocument(); + $resp_doc = new \DOMDocument(); $resp_doc->loadXML($resp_xml); // Parse the response to find credit value @@ -365,7 +364,7 @@ public function checkCredit() { */ public function checkBalance() { // Create XML doc for request - $req_doc = new DOMDocument('1.0', 'UTF-8'); + $req_doc = new \DOMDocument('1.0', 'UTF-8'); $root = $req_doc->createElement('Balance'); $req_doc->appendChild($root); $root->appendChild($req_doc->createElement('Key', $this->key)); @@ -375,7 +374,7 @@ public function checkBalance() { $resp_xml = $this->postToClockwork(self::API_BALANCE_METHOD, $req_xml); // Create XML doc for response - $resp_doc = new DOMDocument(); + $resp_doc = new \DOMDocument(); $resp_doc->loadXML($resp_xml); // Parse the response to find balance value @@ -426,7 +425,7 @@ public function checkBalance() { */ public function checkKey() { // Create XML doc for request - $req_doc = new DOMDocument('1.0', 'UTF-8'); + $req_doc = new \DOMDocument('1.0', 'UTF-8'); $root = $req_doc->createElement('Authenticate'); $req_doc->appendChild($root); $root->appendChild($req_doc->createElement('Key', $this->key)); @@ -436,7 +435,7 @@ public function checkKey() { $resp_xml = $this->postToClockwork(self::API_AUTH_METHOD, $req_xml); // Create XML doc for response - $resp_doc = new DOMDocument(); + $resp_doc = new \DOMDocument(); $resp_doc->loadXML($resp_xml); // Parse the response to see if authenticated @@ -525,9 +524,9 @@ protected function xmlPost($url, $data) { $info = curl_getinfo($ch); if ($response === false || $info['http_code'] != 200) { - throw new Exception('HTTP Error calling Clockwork API - HTTP Status: ' . $info['http_code'] . ' - cURL Erorr: ' . curl_error($ch)); + throw new \Exception('HTTP Error calling Clockwork API - HTTP Status: ' . $info['http_code'] . ' - cURL Erorr: ' . curl_error($ch)); } elseif (curl_errno($ch) > 0) { - throw new Exception('HTTP Error calling Clockwork API - cURL Error: ' . curl_error($ch)); + throw new \Exception('HTTP Error calling Clockwork API - cURL Error: ' . curl_error($ch)); } curl_close($ch); @@ -553,17 +552,17 @@ protected function xmlPost($url, $data) { $fp = @fopen($url, 'rb', false, $ctx); if (!$fp) { ini_set('track_errors',$track); - throw new Exception("HTTP Error calling Clockwork API - fopen Error: $php_errormsg"); + throw new \Exception("HTTP Error calling Clockwork API - fopen Error: $php_errormsg"); } $response = @stream_get_contents($fp); if ($response === false) { ini_set('track_errors',$track); - throw new Exception("HTTP Error calling Clockwork API - stream Error: $php_errormsg"); + throw new \Exception("HTTP Error calling Clockwork API - stream Error: $php_errormsg"); } ini_set('track_errors',$track); return $response; } else { - throw new Exception("Clockwork requires PHP5 with cURL or HTTP stream support"); + throw new \Exception("Clockwork requires PHP5 with cURL or HTTP stream support"); } } diff --git a/class-ClockworkException.php b/src/Clockwork/ClockworkException.php similarity index 95% rename from class-ClockworkException.php rename to src/Clockwork/ClockworkException.php index bf8d63e..51680ff 100755 --- a/class-ClockworkException.php +++ b/src/Clockwork/ClockworkException.php @@ -6,7 +6,9 @@ * @copyright Mediaburst Ltd 2012 * @license ISC * @link http://www.clockworksms.com - */ + */ + +namespace Clockwork; /* * ClockworkException diff --git a/tests/Clockwork/ClockworkTest.php b/tests/Clockwork/ClockworkTest.php new file mode 100644 index 0000000..b1e4e90 --- /dev/null +++ b/tests/Clockwork/ClockworkTest.php @@ -0,0 +1,26 @@ +assertEquals($expectedResult,Clockwork::is_valid_msisdn($number)); + } +} \ No newline at end of file diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..5bfc905 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,2 @@ +