forked from LiveCarta/PayPal-PHP-SDK
Enabled EC Parameters support
- Updated Api to enabled EC Parameters - Updated Tests - Updated Logging Manager - Added a feature to do validation on accessors.
This commit is contained in:
37
lib/PayPal/Validation/ArgumentValidator.php
Normal file
37
lib/PayPal/Validation/ArgumentValidator.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace PayPal\Validation;
|
||||
|
||||
/**
|
||||
* Class ArgumentValidator
|
||||
*
|
||||
* @package PayPal\Validation
|
||||
*/
|
||||
class ArgumentValidator
|
||||
{
|
||||
|
||||
/**
|
||||
* Helper method for validating an argument that will be used by this API in any requests.
|
||||
*
|
||||
* @param $argument mixed The object to be validated
|
||||
* @param $argumentName string|null The name of the argument.
|
||||
* This will be placed in the exception message for easy reference
|
||||
*/
|
||||
public static function validate($argument, $argumentName = null)
|
||||
{
|
||||
if (
|
||||
$argument != null &&
|
||||
(
|
||||
(gettype($argument) == 'string' && $argument == '')
|
||||
||
|
||||
is_array($argument) && empty($argument)
|
||||
)
|
||||
) {
|
||||
//Throw an Exception for string or array
|
||||
throw new \InvalidArgumentException("$argumentName cannot be null or empty");
|
||||
} elseif ($argument == null) {
|
||||
//Generic Exception
|
||||
throw new \InvalidArgumentException("$argumentName cannot be null");
|
||||
}
|
||||
}
|
||||
}
|
||||
44
lib/PayPal/Validation/ModelAccessorValidator.php
Normal file
44
lib/PayPal/Validation/ModelAccessorValidator.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace PayPal\Validation;
|
||||
|
||||
use PayPal\Common\PPModel;
|
||||
use PayPal\Core\PPConfigManager;
|
||||
use PayPal\Core\PPLoggingManager;
|
||||
|
||||
/**
|
||||
* Class ModelAccessorValidator
|
||||
*
|
||||
* @package PayPal\Validation
|
||||
*/
|
||||
class ModelAccessorValidator
|
||||
{
|
||||
/**
|
||||
* Helper method for validating if the class contains accessor methods (getter and setter) for a given attribute
|
||||
*
|
||||
* @param PPModel $class An object of PPModel
|
||||
* @param string $attributeName Attribute name
|
||||
* @return bool
|
||||
*/
|
||||
public static function validate(PPModel $class, $attributeName)
|
||||
{
|
||||
$mode = PPConfigManager::getInstance()->get('validation.level');
|
||||
if ($mode != 'disabled') {
|
||||
//If the mode is disabled, bypass the validation
|
||||
foreach (array('set' . $attributeName, 'get' . $attributeName) as $methodName) {
|
||||
//Check if both getter and setter exists for given attribute
|
||||
if (!method_exists($class, $methodName)) {
|
||||
//Delegate the error based on the choice
|
||||
$className = is_object($class) ? get_class($class) : (string)$class;
|
||||
$errorMessage = "Missing Accessor: $className:$methodName. Please let us know by creating an issue at https://github.com/paypal/rest-api-sdk-php/issues";
|
||||
PPLoggingManager::getInstance(__CLASS__)->warning($errorMessage);
|
||||
if ($mode == 'strict') {
|
||||
trigger_error($errorMessage, E_USER_NOTICE);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
26
lib/PayPal/Validation/UrlValidator.php
Normal file
26
lib/PayPal/Validation/UrlValidator.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace PayPal\Validation;
|
||||
|
||||
/**
|
||||
* Class UrlValidator
|
||||
*
|
||||
* @package PayPal\Validation
|
||||
*/
|
||||
class UrlValidator
|
||||
{
|
||||
|
||||
/**
|
||||
* Helper method for validating URLs that will be used by this API in any requests.
|
||||
*
|
||||
* @param $url
|
||||
* @param string|null $urlName
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public static function validate($url, $urlName = null)
|
||||
{
|
||||
if (filter_var($url, FILTER_VALIDATE_URL) === false) {
|
||||
throw new \InvalidArgumentException("$urlName is not a fully qualified URL");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user