From 90886052dd180acc72c7d505217321f3233ce744 Mon Sep 17 00:00:00 2001 From: Davi Marcondes Moreira Date: Wed, 8 Jun 2016 14:15:41 -0300 Subject: [PATCH] Small suggestions :wink: --- .gitignore | 2 +- README.md | 24 +++++-- examples/bootstrap.php | 2 +- src/Ebanx/Command/AbstractCommand.php | 33 ++++++---- src/Ebanx/Command/BankList.php | 21 ++++--- src/Ebanx/Command/Cancel.php | 21 ++++--- src/Ebanx/Command/Capture.php | 21 ++++--- src/Ebanx/Command/DocumentBalance.php | 21 ++++--- src/Ebanx/Command/Exchange.php | 21 ++++--- src/Ebanx/Command/Factory.php | 37 +++++------ src/Ebanx/Command/PrintHtml.php | 22 ++++--- src/Ebanx/Command/Query.php | 21 ++++--- src/Ebanx/Command/Refund.php | 30 +++++---- src/Ebanx/Command/RefundOrCancel.php | 21 ++++--- src/Ebanx/Command/Request/Checkout.php | 23 ++++--- src/Ebanx/Command/Request/Direct.php | 23 ++++--- src/Ebanx/Command/Token.php | 21 ++++--- src/Ebanx/Command/Validator.php | 73 +++++++++++----------- src/Ebanx/Command/Zipcode.php | 21 ++++--- src/Ebanx/Config.php | 77 ++++++++++++----------- src/Ebanx/Ebanx.php | 35 ++++++----- src/Ebanx/Http/Client.php | 86 ++++++++++++++------------ src/autoload.php | 50 --------------- 23 files changed, 386 insertions(+), 320 deletions(-) delete mode 100644 src/autoload.php diff --git a/.gitignore b/.gitignore index b20f5ba..073e37a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ build composer.lock -vendor \ No newline at end of file +vendor diff --git a/README.md b/README.md index c759fae..573ed64 100644 --- a/README.md +++ b/README.md @@ -1,38 +1,50 @@ # EBANX PHP Library [![Build Status](https://travis-ci.org/ebanx-integration/ebanx-php.svg?branch=master)](https://travis-ci.org/ebanx-integration/ebanx-php) [![Code Climate](https://codeclimate.com/github/ebanx-integration/ebanx-php/badges/gpa.svg)](https://codeclimate.com/github/ebanx-integration/ebanx-php) [![Test Coverage](https://codeclimate.com/github/ebanx-integration/ebanx-php/badges/coverage.svg)](https://codeclimate.com/github/ebanx-integration/ebanx-php/coverage) + EBANX is the market leader in e-commerce payment solutions for International Merchants selling online to Brazil. This library enables you to integrate EBANX with any PHP application. ## Requirements + * PHP >= 5.3 * cURL ## Installation + ### Composer + The EBANX library is available on Packagist (https://packagist.org/packages/ebanx/ebanx), therefore you can install it by simply updating your composer.json file: ``` json { "require" : { - "ebanx/ebanx": "dev-master" + "ebanx/ebanx": "v1.9.0" } } ``` -After that run _composer install_ and wait for it to finish. Include the Composer + +> **Notice:** Remember to always check our [Changelog](#changelog) and available releases on [our source on Packagist](https://packagist.org/packages/ebanx/ebanx) to always install the most up-to-date versions. + +After that run _composer install --no-dev_ and wait for it to finish. Include the Composer generated autoloader from 'vendor/autoload.php' and you're ready to use the library. ### Git Repository -Clone the git repository anywhere you want and include the EBANX library autoloader -from 'src/autoload.php'. + +Clone the git repository anywhere you want, then run _composer install --no-dev_ to install all dependencies +and, finally, include the EBANX library by requiring the 'vendor/autoload.php' file. ## Usage + ### Setup + To use the EBANX PHP library you need to setup your integration key. + ``` php \Ebanx\Config::setIntegrationKey('your-integration-key'); ``` If you need to change other settings, you can use the following function call: + ``` php \Ebanx\Config::set([ 'integrationKey' => 'your-integration-key' @@ -41,12 +53,14 @@ If you need to change other settings, you can use the following function call: ``` You can change the following settings: + * integrationKey: your integration key. It will be different in test and production modes. * testMode: enable or disable the test mode. The default value is _false_. * directMode: enable or disable the direct checkout mode. The default value is _false_. To create a new API request, just call one of the following methods on the \Ebanx\Ebanx class and supply it with the request parameters: + * \Ebanx\Ebanx::doCancel * \Ebanx\Ebanx::doCapture * \Ebanx\Ebanx::doExchange @@ -59,6 +73,7 @@ class and supply it with the request parameters: * \Ebanx\Ebanx::doZipcode doRequest command example: + ``` php require_once __DIR__ . 'vendor/autoload.php'; @@ -75,6 +90,7 @@ $request = \Ebanx\Ebanx::doRequest([ ``` ## Changelog + * **1.9.0**: added getBankList operation * **1.8.0**: added documentBalance operation * **1.7.0**: added hardcoded autoloader, removed Guzzle to avoid dependencies diff --git a/examples/bootstrap.php b/examples/bootstrap.php index 4bd59fc..43df016 100644 --- a/examples/bootstrap.php +++ b/examples/bootstrap.php @@ -1,6 +1,6 @@ 'b9fa8b1d4231889a1445e1ebfdfa2a3558ef3cc651d523eb1b12cdf8af3afc518f17b36edf5e8cfc59a8338bb9241aacd427', diff --git a/src/Ebanx/Command/AbstractCommand.php b/src/Ebanx/Command/AbstractCommand.php index b1dbabf..bced7ff 100644 --- a/src/Ebanx/Command/AbstractCommand.php +++ b/src/Ebanx/Command/AbstractCommand.php @@ -32,47 +32,56 @@ namespace Ebanx\Command; /** - * The abstract command class + * The abstract command class. * * @author Gustavo Henrique Mascarenhas Machado gustavo@ebanx.com */ abstract class AbstractCommand { /** - * Associative array of params + * Associative array of params. + * * @var array */ protected $params = array(); /** - * The HTTP method + * The HTTP method. + * * @var string */ protected $method = 'POST'; /** - * The action URL address + * The action URL address. + * * @var string */ protected $action = null; /** - * The response type - HTML or JSON + * The response type - HTML or JSON. + * * @var string */ - protected $_responseType = 'JSON'; + protected $responseType = 'JSON'; /** - * Validates the request parameters - * @param Ebanx\Command\Validator $validator The validator instance + * Validates the request parameters. + * + * @param \Ebanx\Command\Validator $validator The validator instance + * * @return mixed + * * @throws InvalidArgumentException */ abstract protected function validate($validator); /** - * Executes the command in the EBANX API - * @param array $params The request parameters + * Executes the command in the EBANX API. + * + * @param array $params The request parameters + * * @return mixed */ public function execute($params) @@ -86,8 +95,8 @@ public function execute($params) $client->setParams($this->params) ->setMethod($this->method) ->setAction($this->action) - ->setResponseType($this->_responseType); + ->setResponseType($this->responseType); return $client->send(); } -} \ No newline at end of file +} diff --git a/src/Ebanx/Command/BankList.php b/src/Ebanx/Command/BankList.php index 6955077..bcbdada 100644 --- a/src/Ebanx/Command/BankList.php +++ b/src/Ebanx/Command/BankList.php @@ -31,33 +31,40 @@ namespace Ebanx\Command; +use Ebanx\Command\AbstractCommand; + /** - * Command for the 'exchange' action + * Command for the 'exchange' action. * * @author Gustavo Henrique Mascarenhas Machado gustavo@ebanx.com */ -class BankList extends \Ebanx\Command\AbstractCommand +class BankList extends AbstractCommand { /** - * The HTTP method + * The HTTP method. + * * @var string */ protected $method = 'GET'; /** - * The action URL address + * The action URL address. + * * @var string */ protected $action = 'getBankList'; /** - * Validates the request parameters - * @param Ebanx\Command\Validator $validator The validator instance + * Validates the request parameters. + * + * @param \Ebanx\Command\Validator $validator The validator instance + * * @return mixed + * * @throws InvalidArgumentException */ protected function validate($validator) { $validator->validatePresence('country_code'); } -} \ No newline at end of file +} diff --git a/src/Ebanx/Command/Cancel.php b/src/Ebanx/Command/Cancel.php index a231e21..0ff6d33 100644 --- a/src/Ebanx/Command/Cancel.php +++ b/src/Ebanx/Command/Cancel.php @@ -31,33 +31,40 @@ namespace Ebanx\Command; +use Ebanx\Command\AbstractCommand; + /** - * Command for the 'cancel' action + * Command for the 'cancel' action. * * @author Gustavo Henrique Mascarenhas Machado gustavo@ebanx.com */ -class Cancel extends \Ebanx\Command\AbstractCommand +class Cancel extends AbstractCommand { /** - * The HTTP method + * The HTTP method. + * * @var string */ protected $method = 'POST'; /** - * The action URL address + * The action URL address. + * * @var string */ protected $action = 'cancel'; /** - * Validates the request parameters - * @param Ebanx\Command\Validator $validator The validator instance + * Validates the request parameters. + * + * @param \Ebanx\Command\Validator $validator The validator instance + * * @return mixed + * * @throws InvalidArgumentException */ protected function validate($validator) { $validator->validatePresence('hash'); } -} \ No newline at end of file +} diff --git a/src/Ebanx/Command/Capture.php b/src/Ebanx/Command/Capture.php index f3ef960..5a0a6e4 100644 --- a/src/Ebanx/Command/Capture.php +++ b/src/Ebanx/Command/Capture.php @@ -31,33 +31,40 @@ namespace Ebanx\Command; +use Ebanx\Command\AbstractCommand; + /** - * Command for the 'capture' action + * Command for the 'capture' action. * * @author Gustavo Henrique Mascarenhas Machado gustavo@ebanx.com */ -class Capture extends \Ebanx\Command\AbstractCommand +class Capture extends AbstractCommand { /** - * The HTTP method + * The HTTP method. + * * @var string */ protected $method = 'GET'; /** - * The action URL address + * The action URL address. + * * @var string */ protected $action = 'capture'; /** - * Validates the request parameters - * @param Ebanx\Command\Validator $validator The validator instance + * Validates the request parameters. + * + * @param \Ebanx\Command\Validator $validator The validator instance + * * @return mixed + * * @throws InvalidArgumentException */ protected function validate($validator) { $validator->validatePresenceOr('hash', 'merchant_payment_code'); } -} \ No newline at end of file +} diff --git a/src/Ebanx/Command/DocumentBalance.php b/src/Ebanx/Command/DocumentBalance.php index cfb2332..2298829 100644 --- a/src/Ebanx/Command/DocumentBalance.php +++ b/src/Ebanx/Command/DocumentBalance.php @@ -31,29 +31,36 @@ namespace Ebanx\Command; +use Ebanx\Command\AbstractCommand; + /** - * Command for the 'documentBalance' action + * Command for the 'documentBalance' action. * * @author Heitor Dolinski heitor@ebanx.com */ -class DocumentBalance extends \Ebanx\Command\AbstractCommand +class DocumentBalance extends AbstractCommand { /** - * The HTTP method + * The HTTP method. + * * @var string */ protected $method = 'GET'; /** - * The action URL address + * The action URL address. + * * @var string */ protected $action = 'documentbalance'; /** - * Validates the request parameters - * @param Ebanx\Command\Validator $validator The validator instance + * Validates the request parameters. + * + * @param \Ebanx\Command\Validator $validator The validator instance + * * @return mixed + * * @throws InvalidArgumentException */ protected function validate($validator) @@ -61,4 +68,4 @@ protected function validate($validator) $validator->validatePresence('document'); $validator->validatePresence('currency_code'); } -} \ No newline at end of file +} diff --git a/src/Ebanx/Command/Exchange.php b/src/Ebanx/Command/Exchange.php index eb4736e..f4f91d3 100644 --- a/src/Ebanx/Command/Exchange.php +++ b/src/Ebanx/Command/Exchange.php @@ -31,33 +31,40 @@ namespace Ebanx\Command; +use Ebanx\Command\AbstractCommand; + /** - * Command for the 'exchange' action + * Command for the 'exchange' action. * * @author Gustavo Henrique Mascarenhas Machado gustavo@ebanx.com */ -class Exchange extends \Ebanx\Command\AbstractCommand +class Exchange extends AbstractCommand { /** - * The HTTP method + * The HTTP method. + * * @var string */ protected $method = 'GET'; /** - * The action URL address + * The action URL address. + * * @var string */ protected $action = 'exchange'; /** - * Validates the request parameters - * @param Ebanx\Command\Validator $validator The validator instance + * Validates the request parameters. + * + * @param \Ebanx\Command\Validator $validator The validator instance + * * @return mixed + * * @throws InvalidArgumentException */ protected function validate($validator) { $validator->validatePresence('currency_code'); } -} \ No newline at end of file +} diff --git a/src/Ebanx/Command/Factory.php b/src/Ebanx/Command/Factory.php index d408ef8..41a1526 100644 --- a/src/Ebanx/Command/Factory.php +++ b/src/Ebanx/Command/Factory.php @@ -32,16 +32,19 @@ namespace Ebanx\Command; /** - * The commands factory class + * The commands factory class. * * @author Gustavo Henrique Mascarenhas Machado gustavo@ebanx.com */ class Factory { /** - * Returns an instance of the command class - * @param string $name The command name in the form 'doCommand' + * Returns an instance of the command class. + * + * @param string $name The command name in the form 'doCommand' + * * @return \Ebanx\Command\AbstractCommand + * * @throws RuntimeException */ public static function build($name) @@ -50,35 +53,27 @@ public static function build($name) $className = str_replace('do', '', $name); - if ($className == $name) - { + if ($className == $name) { $className = str_replace('get', '', $name); } $class .= $className; // Request command is different depending on the checkout method (Ebanx or direct) - if ($className == 'Request') - { + if ($className == 'Request') { // Use EBANX direct - if (\Ebanx\Config::getDirectMode() == true) - { + if (\Ebanx\Config::getDirectMode() == true) { $class .= '\\Direct'; - } - // Use EBANX checkout - else - { + } else { + // Use EBANX checkout $class .= '\\Checkout'; } } - - if (class_exists($class)) - { - return new $class(); - } - else - { + + if (!class_exists($class)) { throw new \RuntimeException("Command '$className' doesn't exist."); } + + return new $class(); } -} \ No newline at end of file +} diff --git a/src/Ebanx/Command/PrintHtml.php b/src/Ebanx/Command/PrintHtml.php index fac9525..b40ec72 100644 --- a/src/Ebanx/Command/PrintHtml.php +++ b/src/Ebanx/Command/PrintHtml.php @@ -32,38 +32,44 @@ namespace Ebanx\Command; /** - * Command for the 'boleto/printHTML' action + * Command for the 'boleto/printHTML' action. * * @author Gustavo Henrique Mascarenhas Machado gustavo@ebanx.com */ class PrintHtml extends \Ebanx\Command\AbstractCommand { /** - * The HTTP method + * The HTTP method. + * * @var string */ protected $method = 'GET'; /** - * The action URL address + * The action URL address. + * * @var string */ protected $action = 'boleto/printHTML'; /** - * The response type - HTML or JSON + * The response type - HTML or JSON. + * * @var string */ - protected $_responseType = 'HTML'; + protected $responseType = 'HTML'; /** - * Validates the request parameters - * @param Ebanx\Command\Validator $validator The validator instance + * Validates the request parameters. + * + * @param \Ebanx\Command\Validator $validator The validator instance + * * @return mixed + * * @throws InvalidArgumentException */ protected function validate($validator) { $validator->validatePresence('hash'); } -} \ No newline at end of file +} diff --git a/src/Ebanx/Command/Query.php b/src/Ebanx/Command/Query.php index 37807a8..a95dc2a 100644 --- a/src/Ebanx/Command/Query.php +++ b/src/Ebanx/Command/Query.php @@ -31,33 +31,40 @@ namespace Ebanx\Command; +use Ebanx\Command\AbstractCommand; + /** - * Command for the 'query' action + * Command for the 'query' action. * * @author Gustavo Henrique Mascarenhas Machado gustavo@ebanx.com */ -class Query extends \Ebanx\Command\AbstractCommand +class Query extends AbstractCommand { /** - * The HTTP method + * The HTTP method. + * * @var string */ protected $method = 'GET'; /** - * The action URL address + * The action URL address. + * * @var string */ protected $action = 'query'; /** - * Validates the request parameters - * @param Ebanx\Command\Validator $validator The validator instance + * Validates the request parameters. + * + * @param \Ebanx\Command\Validator $validator The validator instance + * * @return mixed + * * @throws InvalidArgumentException */ protected function validate($validator) { $validator->validatePresenceOr('hash', 'merchant_payment_code'); } -} \ No newline at end of file +} diff --git a/src/Ebanx/Command/Refund.php b/src/Ebanx/Command/Refund.php index 012674a..9c6c968 100644 --- a/src/Ebanx/Command/Refund.php +++ b/src/Ebanx/Command/Refund.php @@ -31,29 +31,36 @@ namespace Ebanx\Command; +use Ebanx\Command\AbstractCommand; + /** - * Command for the 'refund' action + * Command for the 'refund' action. * * @author Gustavo Henrique Mascarenhas Machado gustavo@ebanx.com */ -class Refund extends \Ebanx\Command\AbstractCommand +class Refund extends AbstractCommand { /** - * The HTTP method + * The HTTP method. + * * @var string */ protected $method = 'POST'; /** - * The action URL address + * The action URL address. + * * @var string */ protected $action = 'refund'; /** - * Validates the request parameters - * @param Ebanx\Command\Validator $validator The validator instance + * Validates the request parameters. + * + * @param \Ebanx\Command\Validator $validator The validator instance + * * @return mixed + * * @throws InvalidArgumentException */ protected function validate($validator) @@ -61,16 +68,13 @@ protected function validate($validator) $validator->validatePresence('operation'); // Validation for a new refund request - if ($this->params['operation'] == 'request') - { + if ($this->params['operation'] == 'request') { $validator->validatePresence('hash'); $validator->validatePresence('amount'); $validator->validatePresence('description'); - } - // Validation a cancel refund request - else - { + } else { + // Validation a cancel refund request $validator->validatePresenceOr('merchant_refund_code', 'refund_id'); } } -} \ No newline at end of file +} diff --git a/src/Ebanx/Command/RefundOrCancel.php b/src/Ebanx/Command/RefundOrCancel.php index 35e6c36..18ff397 100644 --- a/src/Ebanx/Command/RefundOrCancel.php +++ b/src/Ebanx/Command/RefundOrCancel.php @@ -31,29 +31,36 @@ namespace Ebanx\Command; +use Ebanx\Command\AbstractCommand; + /** - * Command for the 'refundOrCancel' action + * Command for the 'refundOrCancel' action. * * @author Gustavo Henrique Mascarenhas Machado gustavo@ebanx.com */ -class RefundOrCancel extends \Ebanx\Command\AbstractCommand +class RefundOrCancel extends AbstractCommand { /** - * The HTTP method + * The HTTP method. + * * @var string */ protected $method = 'POST'; /** - * The action URL address + * The action URL address. + * * @var string */ protected $action = 'refundOrCancel'; /** - * Validates the request parameters - * @param Ebanx\Command\Validator $validator The validator instance + * Validates the request parameters. + * + * @param \Ebanx\Command\Validator $validator The validator instance + * * @return mixed + * * @throws InvalidArgumentException */ protected function validate($validator) @@ -61,4 +68,4 @@ protected function validate($validator) $validator->validatePresence('hash'); $validator->validatePresence('description'); } -} \ No newline at end of file +} diff --git a/src/Ebanx/Command/Request/Checkout.php b/src/Ebanx/Command/Request/Checkout.php index 9b0627a..0d72338 100644 --- a/src/Ebanx/Command/Request/Checkout.php +++ b/src/Ebanx/Command/Request/Checkout.php @@ -31,30 +31,37 @@ namespace Ebanx\Command\Request; +use Ebanx\Command\AbstractCommand; + /** - * Command for the 'request' action + * Command for the 'request' action. * * @author Gustavo Henrique Mascarenhas Machado gustavo@ebanx.com */ -class Checkout extends \Ebanx\Command\AbstractCommand +class Checkout extends AbstractCommand { /** - * The HTTP method + * The HTTP method. + * * @var string */ protected $method = 'POST'; /** - * The action URL address + * The action URL address. + * * @var string */ protected $action = 'request'; /** - * Validates the request parameters - * @param Ebanx\Command\Validator $validator The validator instance + * Validates the request parameters. + * + * @param \Ebanx\Command\Validator $validator The validator instance + * * @return mixed - * @throws InvalidArgumentException + * + * @throws \InvalidArgumentException */ protected function validate($validator) { @@ -65,4 +72,4 @@ protected function validate($validator) $validator->validatePresence('email'); $validator->validatePresence('payment_type_code'); } -} \ No newline at end of file +} diff --git a/src/Ebanx/Command/Request/Direct.php b/src/Ebanx/Command/Request/Direct.php index 7f9c355..7178be2 100644 --- a/src/Ebanx/Command/Request/Direct.php +++ b/src/Ebanx/Command/Request/Direct.php @@ -31,30 +31,37 @@ namespace Ebanx\Command\Request; +use Ebanx\Command\AbstractCommand; + /** - * Command for the 'direct' action + * Command for the 'direct' action. * * @author Gustavo Henrique Mascarenhas Machado gustavo@ebanx.com */ -class Direct extends \Ebanx\Command\AbstractCommand +class Direct extends AbstractCommand { /** - * The HTTP method + * The HTTP method. + * * @var string */ protected $method = 'POST'; /** - * The action URL address + * The action URL address. + * * @var string */ protected $action = 'direct'; /** - * Validates the request parameters - * @param Ebanx\Command\Validator $validator The validator instance + * Validates the request parameters. + * + * @param \Ebanx\Command\Validator $validator The validator instance + * * @return mixed - * @throws InvalidArgumentException + * + * @throws \InvalidArgumentException */ protected function validate($validator) { @@ -71,4 +78,4 @@ protected function validate($validator) $this->params['integration_key'] = \Ebanx\Config::getIntegrationKey(); $this->params = array('request_body' => json_encode($this->params)); } -} \ No newline at end of file +} diff --git a/src/Ebanx/Command/Token.php b/src/Ebanx/Command/Token.php index b9c7f97..ebc11ae 100644 --- a/src/Ebanx/Command/Token.php +++ b/src/Ebanx/Command/Token.php @@ -31,29 +31,36 @@ namespace Ebanx\Command; +use Ebanx\Command\AbstractCommand; + /** - * Command for the 'token' action + * Command for the 'token' action. * * @author Gustavo Henrique Mascarenhas Machado gustavo@ebanx.com */ -class Token extends \Ebanx\Command\AbstractCommand +class Token extends AbstractCommand { /** - * The HTTP method + * The HTTP method. + * * @var string */ protected $method = 'POST'; /** - * The action URL address + * The action URL address. + * * @var string */ protected $action = 'token'; /** - * Validates the request parameters - * @param Ebanx\Command\Validator $validator The validator instance + * Validates the request parameters. + * + * @param \Ebanx\Command\Validator $validator The validator instance + * * @return mixed + * * @throws InvalidArgumentException */ protected function validate($validator) @@ -68,4 +75,4 @@ protected function validate($validator) $this->params['integration_key'] = \Ebanx\Config::getIntegrationKey(); $this->params = array('request_body' => json_encode($this->params)); } -} \ No newline at end of file +} diff --git a/src/Ebanx/Command/Validator.php b/src/Ebanx/Command/Validator.php index f874967..18c38ca 100644 --- a/src/Ebanx/Command/Validator.php +++ b/src/Ebanx/Command/Validator.php @@ -39,7 +39,8 @@ class Validator { /** - * The request parameters + * The request parameters. + * * @var array */ protected $params; @@ -50,15 +51,17 @@ public function __construct($params) } /** - * Verifies if a parameter was supplied - * @param string $key The parameter name (array key) - * @return boolean - * @throws InvalidArgumentException + * Verifies if a parameter was supplied. + * + * @param string $key The parameter name (array key) + * + * @return bool + * + * @throws \InvalidArgumentException */ public function validatePresence($key) { - if ($this->exists($key)) - { + if ($this->exists($key)) { return true; } @@ -66,26 +69,27 @@ public function validatePresence($key) } /** - * Verifies if one of the parameters was supplied - * @param string $key1 The first parameter name (array key) - * @param string $key1 The second parameter name (array key) - * @return boolean + * Verifies if one of the parameters was supplied. + * + * @param string $key1 The first parameter name (array key) + * @param string $key2 The second parameter name (array key) + * + * @return bool + * * @throws InvalidArgumentException */ public function validatePresenceOr($key1, $key2) { - if ($this->exists($key1)) - { + if ($this->exists($key1)) { // Throw an exception if both parameters exist - if ($this->exists($key2)) - { - throw new \InvalidArgumentException("Either parameter '$key1' or '$key2' must be supplied, but not both."); + if ($this->exists($key2)) { + throw new \InvalidArgumentException( + "Either parameter '$key1' or '$key2' must be supplied, but not both." + ); } return true; - } - else if ($this->exists($key2)) - { + } elseif ($this->exists($key2)) { return true; } @@ -93,39 +97,34 @@ public function validatePresenceOr($key1, $key2) } /** - * Verifies if a parameter exists - * @param string $key The parameter name (array key) - * @return boolean + * Verifies if a parameter exists. + * + * @param string $key The parameter name (array key) + * + * @return bool */ public function exists($key) { // Checks if we need to verify a nested array - if (preg_match('/\.+/', $key)) - { - $keys = explode('.', $key); + if (preg_match('/\.+/', $key)) { + $keys = explode('.', $key); $levels = count($keys); // Quick workaround - if ($levels == 4) - { - return isset($this->params[$keys[0]][$keys[1]][$keys[2]][$keys3]); - } - else if ($levels == 3) - { + if ($levels == 4) { + return isset($this->params[$keys[0]][$keys[1]][$keys[2]][$keys[3]]); + } elseif ($levels == 3) { return isset($this->params[$keys[0]][$keys[1]][$keys[2]]); - } - else - { + } else { return isset($this->params[$keys[0]][$keys[1]]); } } // Non-nested array validation - if (array_key_exists($key, $this->params)) - { + if (array_key_exists($key, $this->params)) { return true; } return false; } -} \ No newline at end of file +} diff --git a/src/Ebanx/Command/Zipcode.php b/src/Ebanx/Command/Zipcode.php index 73c3a03..53a81d4 100644 --- a/src/Ebanx/Command/Zipcode.php +++ b/src/Ebanx/Command/Zipcode.php @@ -31,33 +31,40 @@ namespace Ebanx\Command; +use Ebanx\Command\AbstractCommand; + /** - * Command for the 'token' action + * Command for the 'token' action. * * @author Gustavo Henrique Mascarenhas Machado gustavo@ebanx.com */ -class Zipcode extends \Ebanx\Command\AbstractCommand +class Zipcode extends AbstractCommand { /** - * The HTTP method + * The HTTP method. + * * @var string */ protected $method = 'GET'; /** - * The action URL address + * The action URL address. + * * @var string */ protected $action = 'zipcode'; /** - * Validates the request parameters - * @param Ebanx\Command\Validator $validator The validator instance + * Validates the request parameters. + * + * @param \Ebanx\Command\Validator $validator The validator instance + * * @return mixed + * * @throws InvalidArgumentException */ protected function validate($validator) { $validator->validatePresence('zipcode'); } -} \ No newline at end of file +} diff --git a/src/Ebanx/Config.php b/src/Ebanx/Config.php index 4eaba81..c46ad52 100644 --- a/src/Ebanx/Config.php +++ b/src/Ebanx/Config.php @@ -32,26 +32,28 @@ namespace Ebanx; /** - * Config class (singleton, registry) + * Config class (singleton, registry). * * @author Gustavo Henrique Mascarenhas Machado gustavo@ebanx.com */ class Config { /** - * The API URLs for test and production + * The API URLs for test and production. */ - const URL_TEST = 'https://sandbox.ebanx.com/ws/'; + const URL_TEST = 'https://sandbox.ebanx.com/ws/'; const URL_PRODUCTION = 'https://api.ebanx.com/ws/'; /** - * The config object instance + * The config object instance. + * * @var \Ebanx\Config */ protected static $instance = null; /** - * Library settings array + * Library settings array. + * * @var array */ protected static $settings = array(); @@ -63,36 +65,38 @@ class Config protected function __construct() { self::$settings['directMode'] = false; - self::$settings['testMode'] = false; + self::$settings['testMode'] = false; self::$settings['httpClient'] = '\\Ebanx\\Http\\Client'; } /** - * Get the class instance (singleton) + * Get the class instance (singleton). + * * @return \Ebanx\Config */ public static function getInstance() { - if (self::$instance == null) - { - self::$instance = new Config(); + if (self::$instance == null) { + self::$instance = new self(); } return self::$instance; } /** - * Gets a setting value + * Gets a setting value. + * * @param string $key The setting name + * * @return mixed - * @throws InvalidArgumentException + * + * @throws \InvalidArgumentException */ public static function get($key) { self::getInstance(); - if (array_key_exists($key, self::$settings)) - { + if (array_key_exists($key, self::$settings)) { return self::$settings[$key]; } @@ -100,10 +104,10 @@ public static function get($key) } /** - * Sets a setting - * @param string $key The setting name - * @param mixed $value The setting value - * @return void + * Sets a setting. + * + * @param string $key The setting name + * @param mixed $value The setting value */ public static function set() { @@ -111,23 +115,21 @@ public static function set() $args = func_get_args(); - if (is_array($args[0])) - { - foreach ($args[0] as $key => $value) - { + if (is_array($args[0])) { + foreach ($args[0] as $key => $value) { self::$settings[$key] = $value; } - } - else - { + } else { self::$settings[$args[0]] = $args[1]; } } /** - * Magic method to get and set settings - * @param string $name The method name - * @param string $args The method arguments + * Magic method to get and set settings. + * + * @param string $name The method name + * @param string $args The method arguments + * * @return mixed */ public static function __callStatic($name, $args) @@ -135,29 +137,26 @@ public static function __callStatic($name, $args) // From 'getIntegrationKey' to 'integrationKey' $key = lcfirst(preg_replace('/^get|^set/', '', $name)); - // Magic getter method - if (preg_match('/^get[\w]+/', $name)) - { + if (preg_match('/^get[\w]+/', $name)) { + // Magic getter method return self::get($key); - } - // Magic setter method - else if (preg_match('/^set[\w]+/', $name)) - { + } elseif (preg_match('/^set[\w]+/', $name)) { + // Magic setter method self::set($key, $args[0]); } } /** - * Gets the current API URL + * Gets the current API URL. + * * @return string */ public static function getURL() { - if (self::$settings['testMode'] == true) - { + if (self::$settings['testMode'] == true) { return self::URL_TEST; } return self::URL_PRODUCTION; } -} \ No newline at end of file +} diff --git a/src/Ebanx/Ebanx.php b/src/Ebanx/Ebanx.php index 36b1bf6..101b654 100644 --- a/src/Ebanx/Ebanx.php +++ b/src/Ebanx/Ebanx.php @@ -32,40 +32,41 @@ namespace Ebanx; /** - * The EBANX API client + * The EBANX API client. * * @author Gustavo Henrique Mascarenhas Machado gustavo@ebanx.com */ class Ebanx { /** - * Library version + * Library version. + * * @var string */ const VERSION = '1.9.0'; /** - * Magic method that calls the Command Factory + * Magic method that calls the Command Factory. + * * @param string $name The method name * @param string $args The method arguments ($args[0] for the parameters array) + * * @return mixed - * @throws InvalidArgumentException + * + * @throws \InvalidArgumentException */ public static function __callStatic($name, $args) { - if (preg_match('/^do|get[\w]+/', $name)) - { - if (!isset($args[0])) - { - throw new \InvalidArgumentException('The command call received no arguments.'); - } - - $command = \Ebanx\Command\Factory::build($name); - return $command->execute($args[0]); - } - else - { + if (!preg_match('/^do|get[\w]+/', $name)) { throw new \InvalidArgumentException("The command $name doesn't exist."); } + + if (!isset($args[0])) { + throw new \InvalidArgumentException('The command call received no arguments.'); + } + + $command = \Ebanx\Command\Factory::build($name); + + return $command->execute($args[0]); } -} \ No newline at end of file +} diff --git a/src/Ebanx/Http/Client.php b/src/Ebanx/Http/Client.php index 7ff1ab5..64a93b8 100644 --- a/src/Ebanx/Http/Client.php +++ b/src/Ebanx/Http/Client.php @@ -32,49 +32,55 @@ namespace Ebanx\Http; use Ebanx\Config; -use GuzzleHttp; /** - * HTTP client class, wrapper for curl_* functions + * HTTP client class, wrapper for curl_* functions. * * @author Gustavo Henrique Mascarenhas Machado gustavo@ebanx.com */ class Client { /** - * The request HTTP method + * The request HTTP method. + * * @var string */ protected $method; /** - * The allowed HTTP methods + * The allowed HTTP methods. + * * @var array */ protected $allowedMethods = array('POST', 'GET'); /** - * The HTTP action (URI) + * The HTTP action (URI). + * * @var string */ protected $action; /** - * The request parameters + * The request parameters. + * * @var array */ protected $params; /** - * Flag to call json_decode on response - * @var boolean + * Flag to call json_decode on response. + * + * @var bool */ protected $decodeResponse = false; /** - * Set the request parameters + * Set the request parameters. + * * @param array $params The request parameters - * @return Ebanx\Http\Client + * + * @return \Ebanx\Http\Client */ public function setParams($params) { @@ -85,43 +91,49 @@ public function setParams($params) } /** - * Set the request HTTP method + * Set the request HTTP method. + * * @param string $method The request HTTP method - * @return Ebanx\Http\Client - * @throws InvalidArgumentException + * + * @return \Ebanx\Http\Client + * + * @throws \InvalidArgumentException */ public function setMethod($method) { - if (!in_array(strtoupper($method), $this->allowedMethods)) - { - throw new \InvalidArgumentException("The HTTP Client doesn't accept $method requests."); + if (!in_array(strtoupper($method), $this->allowedMethods)) { + throw new \InvalidArgumentException("The HTTP Client doesn't accept $method requests."); } $this->method = $method; + return $this; } /** - * Set the request target URI + * Set the request target URI. + * * @param string $action The target URI - * @return Ebanx\Http\Client + * + * @return \Ebanx\Http\Client */ public function setAction($action) { - $this->action = Config::getURL() . $action; + $this->action = Config::getURL().$action; + return $this; } /** - * Set the decodeResponse flag depending on the response type (JSON or HTML) + * Set the decodeResponse flag depending on the response type (JSON or HTML). + * * @param string $responseType The response type (JSON or HTML) - * @return Ebanx\Http\Client + * + * @return \Ebanx\Http\Client */ - public function setResponseType($responseType) { - if (strtoupper($responseType) == 'JSON') - { + if (strtoupper($responseType) == 'JSON') { $this->decodeResponse = true; } @@ -129,34 +141,32 @@ public function setResponseType($responseType) } /** - * Sends the HTTP request - * @return StdClass + * Sends the HTTP request. + * + * @return \stdClass */ public function send() { - if (!ini_get('allow_url_fopen')) - { + if (!ini_get('allow_url_fopen')) { throw new \RuntimeException('allow_url_fopen must be enabled to use PHP streams.'); } $params = http_build_query($this->params); - $uri = ($this->method == 'GET') ? ($this->action . '?' . $params) : $this->action; + $uri = ($this->method == 'GET') ? ($this->action.'?'.$params) : $this->action; $context = stream_context_create(array( 'http' => array( - 'method' => $this->method - , 'header' => "Content-Type: application/x-www-form-urlencoded\r\n" . - "User-Agent: EBANX PHP Library " . \Ebanx\Ebanx::VERSION . "\r\n" - , 'content' => ($this->method == 'GET') ? '' : $params - ) + 'method' => $this->method, + 'header' => "Content-Type: application/x-www-form-urlencoded\r\n". + 'User-Agent: EBANX PHP Library '.\Ebanx\Ebanx::VERSION."\r\n", + 'content' => ($this->method == 'GET') ? '' : $params, + ), )); $response = file_get_contents($uri, false, $context); - if ($response && strlen($response)) - { - if ($this->decodeResponse) - { + if ($response && strlen($response)) { + if ($this->decodeResponse) { return json_decode($response); } diff --git a/src/autoload.php b/src/autoload.php deleted file mode 100644 index d791843..0000000 --- a/src/autoload.php +++ /dev/null @@ -1,50 +0,0 @@ -