recreate project
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace PayPal\Common;
|
||||
|
||||
/**
|
||||
* Class ArrayUtil
|
||||
* Util Class for Arrays
|
||||
*
|
||||
* @package PayPal\Common
|
||||
*/
|
||||
class ArrayUtil
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @param array $arr
|
||||
* @return true if $arr is an associative array
|
||||
*/
|
||||
public static function isAssocArray(array $arr)
|
||||
{
|
||||
foreach ($arr as $k => $v) {
|
||||
if (is_int($k)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,307 @@
|
||||
<?php
|
||||
|
||||
namespace PayPal\Common;
|
||||
use PayPal\Validation\JsonValidator;
|
||||
use PayPal\Validation\ModelAccessorValidator;
|
||||
|
||||
/**
|
||||
* Generic Model class that all API domain classes extend
|
||||
* Stores all member data in a Hashmap that enables easy
|
||||
* JSON encoding/decoding
|
||||
*/
|
||||
class PayPalModel
|
||||
{
|
||||
|
||||
private $_propMap = array();
|
||||
|
||||
/**
|
||||
* OAuth Credentials to use for this call
|
||||
*
|
||||
* @var \PayPal\Auth\OAuthTokenCredential $credential
|
||||
*/
|
||||
protected static $credential;
|
||||
|
||||
/**
|
||||
* Sets Credential
|
||||
*
|
||||
* @deprecated Pass ApiContext to create/get methods instead
|
||||
* @param \PayPal\Auth\OAuthTokenCredential $credential
|
||||
*/
|
||||
public static function setCredential($credential)
|
||||
{
|
||||
self::$credential = $credential;
|
||||
}
|
||||
|
||||
/**
|
||||
* Default Constructor
|
||||
*
|
||||
* You can pass data as a json representation or array object. This argument eliminates the need
|
||||
* to do $obj->fromJson($data) later after creating the object.
|
||||
*
|
||||
* @param null $data
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function __construct($data = null)
|
||||
{
|
||||
switch (gettype($data)) {
|
||||
case "NULL":
|
||||
break;
|
||||
case "string":
|
||||
JsonValidator::validate($data);
|
||||
$this->fromJson($data);
|
||||
break;
|
||||
case "array":
|
||||
$this->fromArray($data);
|
||||
break;
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of Object from Array or Json String. It is generally used when your json
|
||||
* contains an array of this object
|
||||
*
|
||||
* @param mixed $data Array object or json string representation
|
||||
* @return array
|
||||
*/
|
||||
public static function getList($data)
|
||||
{
|
||||
// Return Null if Null
|
||||
if ($data === null) { return null; }
|
||||
|
||||
if (is_a($data, get_class(new \stdClass()))) {
|
||||
//This means, root element is object
|
||||
return new static(json_encode($data));
|
||||
}
|
||||
|
||||
$list = array();
|
||||
|
||||
if (is_array($data)) {
|
||||
$data = json_encode($data);
|
||||
}
|
||||
|
||||
if (JsonValidator::validate($data)) {
|
||||
// It is valid JSON
|
||||
$decoded = json_decode($data);
|
||||
if ($decoded === null) {
|
||||
return $list;
|
||||
}
|
||||
if (is_array($decoded)) {
|
||||
foreach ($decoded as $k => $v) {
|
||||
$list[] = self::getList($v);
|
||||
}
|
||||
}
|
||||
if (is_a($decoded, get_class(new \stdClass()))) {
|
||||
//This means, root element is object
|
||||
$list[] = new static(json_encode($decoded));
|
||||
}
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic Get Method
|
||||
*
|
||||
* @param $key
|
||||
* @return mixed
|
||||
*/
|
||||
public function __get($key)
|
||||
{
|
||||
if ($this->__isset($key)) {
|
||||
return $this->_propMap[$key];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic Set Method
|
||||
*
|
||||
* @param $key
|
||||
* @param $value
|
||||
*/
|
||||
public function __set($key, $value)
|
||||
{
|
||||
if (!is_array($value) && $value === null) {
|
||||
$this->__unset($key);
|
||||
} else {
|
||||
$this->_propMap[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the input key into a valid Setter Method Name
|
||||
*
|
||||
* @param $key
|
||||
* @return mixed
|
||||
*/
|
||||
private function convertToCamelCase($key)
|
||||
{
|
||||
return str_replace(' ', '', ucwords(str_replace(array('_', '-'), ' ', $key)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic isSet Method
|
||||
*
|
||||
* @param $key
|
||||
* @return bool
|
||||
*/
|
||||
public function __isset($key)
|
||||
{
|
||||
return isset($this->_propMap[$key]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic Unset Method
|
||||
*
|
||||
* @param $key
|
||||
*/
|
||||
public function __unset($key)
|
||||
{
|
||||
unset($this->_propMap[$key]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts Params to Array
|
||||
*
|
||||
* @param $param
|
||||
* @return array
|
||||
*/
|
||||
private function _convertToArray($param)
|
||||
{
|
||||
$ret = array();
|
||||
foreach ($param as $k => $v) {
|
||||
if ($v instanceof PayPalModel) {
|
||||
$ret[$k] = $v->toArray();
|
||||
} else if (sizeof($v) <= 0 && is_array($v)) {
|
||||
$ret[$k] = array();
|
||||
} else if (is_array($v)) {
|
||||
$ret[$k] = $this->_convertToArray($v);
|
||||
} else {
|
||||
$ret[$k] = $v;
|
||||
}
|
||||
}
|
||||
// If the array is empty, which means an empty object,
|
||||
// we need to convert array to StdClass object to properly
|
||||
// represent JSON String
|
||||
if (sizeof($ret) <= 0) {
|
||||
$ret = new PayPalModel();
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fills object value from Array list
|
||||
*
|
||||
* @param $arr
|
||||
* @return $this
|
||||
*/
|
||||
public function fromArray($arr)
|
||||
{
|
||||
if (!empty($arr)) {
|
||||
// Iterate over each element in array
|
||||
foreach ($arr as $k => $v) {
|
||||
// If the value is an array, it means, it is an object after conversion
|
||||
if (is_array($v)) {
|
||||
// Determine the class of the object
|
||||
if (($clazz = ReflectionUtil::getPropertyClass(get_class($this), $k)) != null){
|
||||
// If the value is an associative array, it means, its an object. Just make recursive call to it.
|
||||
if (empty($v)){
|
||||
if (ReflectionUtil::isPropertyClassArray(get_class($this), $k)) {
|
||||
// It means, it is an array of objects.
|
||||
$this->assignValue($k, array());
|
||||
continue;
|
||||
}
|
||||
$o = new $clazz();
|
||||
//$arr = array();
|
||||
$this->assignValue($k, $o);
|
||||
} elseif (ArrayUtil::isAssocArray($v)) {
|
||||
/** @var self $o */
|
||||
$o = new $clazz();
|
||||
$o->fromArray($v);
|
||||
$this->assignValue($k, $o);
|
||||
} else {
|
||||
// Else, value is an array of object/data
|
||||
$arr = array();
|
||||
// Iterate through each element in that array.
|
||||
foreach ($v as $nk => $nv) {
|
||||
if (is_array($nv)) {
|
||||
$o = new $clazz();
|
||||
$o->fromArray($nv);
|
||||
$arr[$nk] = $o;
|
||||
} else {
|
||||
$arr[$nk] = $nv;
|
||||
}
|
||||
}
|
||||
$this->assignValue($k, $arr);
|
||||
}
|
||||
} else {
|
||||
$this->assignValue($k, $v);
|
||||
}
|
||||
} else {
|
||||
$this->assignValue($k, $v);
|
||||
}
|
||||
}
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function assignValue($key, $value)
|
||||
{
|
||||
$setter = 'set'. $this->convertToCamelCase($key);
|
||||
// If we find the setter, use that, otherwise use magic method.
|
||||
if (method_exists($this, $setter)) {
|
||||
$this->$setter($value);
|
||||
} else {
|
||||
$this->__set($key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fills object value from Json string
|
||||
*
|
||||
* @param $json
|
||||
* @return $this
|
||||
*/
|
||||
public function fromJson($json)
|
||||
{
|
||||
return $this->fromArray(json_decode($json, true));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns array representation of object
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
return $this->_convertToArray($this->_propMap);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns object JSON representation
|
||||
*
|
||||
* @param int $options http://php.net/manual/en/json.constants.php
|
||||
* @return string
|
||||
*/
|
||||
public function toJSON($options = 0)
|
||||
{
|
||||
// Because of PHP Version 5.3, we cannot use JSON_UNESCAPED_SLASHES option
|
||||
// Instead we would use the str_replace command for now.
|
||||
// TODO: Replace this code with return json_encode($this->toArray(), $options | 64); once we support PHP >= 5.4
|
||||
if (version_compare(phpversion(), '5.4.0', '>=') === true) {
|
||||
return json_encode($this->toArray(), $options | 64);
|
||||
}
|
||||
return str_replace('\\/', '/', json_encode($this->toArray(), $options));
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic Method for toString
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return $this->toJSON(128);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
namespace PayPal\Common;
|
||||
|
||||
use PayPal\Rest\ApiContext;
|
||||
use PayPal\Rest\IResource;
|
||||
use PayPal\Transport\PayPalRestCall;
|
||||
|
||||
/**
|
||||
* Class PayPalResourceModel
|
||||
* An Executable PayPalModel Class
|
||||
*
|
||||
* @property \PayPal\Api\Links[] links
|
||||
* @package PayPal\Common
|
||||
*/
|
||||
class PayPalResourceModel extends PayPalModel implements IResource
|
||||
{
|
||||
|
||||
/**
|
||||
* Sets Links
|
||||
*
|
||||
* @param \PayPal\Api\Links[] $links
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setLinks($links)
|
||||
{
|
||||
$this->links = $links;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets Links
|
||||
*
|
||||
* @return \PayPal\Api\Links[]
|
||||
*/
|
||||
public function getLinks()
|
||||
{
|
||||
return $this->links;
|
||||
}
|
||||
|
||||
public function getLink($rel)
|
||||
{
|
||||
foreach ($this->links as $link) {
|
||||
if ($link->getRel() == $rel) {
|
||||
return $link->getHref();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Append Links to the list.
|
||||
*
|
||||
* @param \PayPal\Api\Links $links
|
||||
* @return $this
|
||||
*/
|
||||
public function addLink($links)
|
||||
{
|
||||
if (!$this->getLinks()) {
|
||||
return $this->setLinks(array($links));
|
||||
} else {
|
||||
return $this->setLinks(
|
||||
array_merge($this->getLinks(), array($links))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove Links from the list.
|
||||
*
|
||||
* @param \PayPal\Api\Links $links
|
||||
* @return $this
|
||||
*/
|
||||
public function removeLink($links)
|
||||
{
|
||||
return $this->setLinks(
|
||||
array_diff($this->getLinks(), array($links))
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Execute SDK Call to Paypal services
|
||||
*
|
||||
* @param string $url
|
||||
* @param string $method
|
||||
* @param string $payLoad
|
||||
* @param array $headers
|
||||
* @param ApiContext $apiContext
|
||||
* @param PayPalRestCall $restCall
|
||||
* @param array $handlers
|
||||
* @return string json response of the object
|
||||
*/
|
||||
protected static function executeCall($url, $method, $payLoad, $headers = array(), $apiContext = null, $restCall = null, $handlers = array('PayPal\Handler\RestHandler'))
|
||||
{
|
||||
//Initialize the context and rest call object if not provided explicitly
|
||||
$apiContext = $apiContext ? $apiContext : new ApiContext(self::$credential);
|
||||
$restCall = $restCall ? $restCall : new PayPalRestCall($apiContext);
|
||||
|
||||
//Make the execution call
|
||||
$json = $restCall->execute($handlers, $url, $method, $payLoad, $headers);
|
||||
return $json;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace PayPal\Common;
|
||||
|
||||
/**
|
||||
* Class PayPalUserAgent
|
||||
* PayPalUserAgent generates User Agent for curl requests
|
||||
*
|
||||
* @package PayPal\Common
|
||||
*/
|
||||
class PayPalUserAgent
|
||||
{
|
||||
|
||||
/**
|
||||
* Returns the value of the User-Agent header
|
||||
* Add environment values and php version numbers
|
||||
*
|
||||
* @param string $sdkName
|
||||
* @param string $sdkVersion
|
||||
* @return string
|
||||
*/
|
||||
public static function getValue($sdkName, $sdkVersion)
|
||||
{
|
||||
|
||||
$featureList = array(
|
||||
'platform-ver=' . PHP_VERSION,
|
||||
'bit=' . self::_getPHPBit(),
|
||||
'os=' . str_replace(' ', '_', php_uname('s') . ' ' . php_uname('r')),
|
||||
'machine=' . php_uname('m')
|
||||
);
|
||||
if (defined('OPENSSL_VERSION_TEXT')) {
|
||||
$opensslVersion = explode(' ', OPENSSL_VERSION_TEXT);
|
||||
$featureList[] = 'crypto-lib-ver=' . $opensslVersion[1];
|
||||
}
|
||||
if (function_exists('curl_version')) {
|
||||
$curlVersion = curl_version();
|
||||
$featureList[] = 'curl=' . $curlVersion['version'];
|
||||
}
|
||||
return sprintf("PayPalSDK/%s %s (%s)", $sdkName, $sdkVersion, implode('; ', $featureList));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets PHP Bit version
|
||||
*
|
||||
* @return int|string
|
||||
*/
|
||||
private static function _getPHPBit()
|
||||
{
|
||||
switch (PHP_INT_SIZE) {
|
||||
case 4:
|
||||
return '32';
|
||||
case 8:
|
||||
return '64';
|
||||
default:
|
||||
return PHP_INT_SIZE;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
<?php
|
||||
|
||||
namespace PayPal\Common;
|
||||
use PayPal\Exception\PayPalConfigurationException;
|
||||
|
||||
/**
|
||||
* Class ReflectionUtil
|
||||
*
|
||||
* @package PayPal\Common
|
||||
*/
|
||||
class ReflectionUtil
|
||||
{
|
||||
|
||||
/**
|
||||
* Reflection Methods
|
||||
*
|
||||
* @var \ReflectionMethod[]
|
||||
*/
|
||||
private static $propertiesRefl = array();
|
||||
|
||||
/**
|
||||
* Properties Type
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
private static $propertiesType = array();
|
||||
|
||||
|
||||
/**
|
||||
* Gets Property Class of the given property.
|
||||
* If the class is null, it returns null.
|
||||
* If the property is not found, it returns null.
|
||||
*
|
||||
* @param $class
|
||||
* @param $propertyName
|
||||
* @return null|string
|
||||
* @throws PayPalConfigurationException
|
||||
*/
|
||||
public static function getPropertyClass($class, $propertyName)
|
||||
{
|
||||
if ($class == get_class(new PayPalModel())) {
|
||||
// Make it generic if PayPalModel is used for generating this
|
||||
return get_class(new PayPalModel());
|
||||
}
|
||||
|
||||
// If the class doesn't exist, or the method doesn't exist, return null.
|
||||
if (!class_exists($class) || !method_exists($class, self::getter($class, $propertyName))) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (($annotations = self::propertyAnnotations($class, $propertyName)) && isset($annotations['return'])) {
|
||||
$param = $annotations['return'];
|
||||
}
|
||||
|
||||
if (isset($param)) {
|
||||
$anno = preg_split("/[\s\[\]]+/", $param);
|
||||
return $anno[0];
|
||||
} else {
|
||||
throw new PayPalConfigurationException("Getter function for '$propertyName' in '$class' class should have a proper return type.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the Property is of type array or an object
|
||||
*
|
||||
* @param $class
|
||||
* @param $propertyName
|
||||
* @return null|boolean
|
||||
* @throws PayPalConfigurationException
|
||||
*/
|
||||
public static function isPropertyClassArray($class, $propertyName)
|
||||
{
|
||||
// If the class doesn't exist, or the method doesn't exist, return null.
|
||||
if (!class_exists($class) || !method_exists($class, self::getter($class, $propertyName))) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (($annotations = self::propertyAnnotations($class, $propertyName)) && isset($annotations['return'])) {
|
||||
$param = $annotations['return'];
|
||||
}
|
||||
|
||||
if (isset($param)) {
|
||||
return substr($param, -strlen('[]'))==='[]';
|
||||
} else {
|
||||
throw new PayPalConfigurationException("Getter function for '$propertyName' in '$class' class should have a proper return type.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves Annotations of each property
|
||||
*
|
||||
* @param $class
|
||||
* @param $propertyName
|
||||
* @throws \RuntimeException
|
||||
* @return mixed
|
||||
*/
|
||||
public static function propertyAnnotations($class, $propertyName)
|
||||
{
|
||||
$class = is_object($class) ? get_class($class) : $class;
|
||||
if (!class_exists('ReflectionProperty')) {
|
||||
throw new \RuntimeException("Property type of " . $class . "::{$propertyName} cannot be resolved");
|
||||
}
|
||||
|
||||
if ($annotations =& self::$propertiesType[$class][$propertyName]) {
|
||||
return $annotations;
|
||||
}
|
||||
|
||||
if (!($refl =& self::$propertiesRefl[$class][$propertyName])) {
|
||||
$getter = self::getter($class, $propertyName);
|
||||
$refl = new \ReflectionMethod($class, $getter);
|
||||
self::$propertiesRefl[$class][$propertyName] = $refl;
|
||||
}
|
||||
|
||||
// todo: smarter regexp
|
||||
if ( !preg_match_all(
|
||||
'~\@([^\s@\(]+)[\t ]*(?:\(?([^\n@]+)\)?)?~i',
|
||||
$refl->getDocComment(),
|
||||
$annots,
|
||||
PREG_PATTERN_ORDER)) {
|
||||
return null;
|
||||
}
|
||||
foreach ($annots[1] as $i => $annot) {
|
||||
$annotations[strtolower($annot)] = empty($annots[2][$i]) ? TRUE : rtrim($annots[2][$i], " \t\n\r)");
|
||||
}
|
||||
|
||||
return $annotations;
|
||||
}
|
||||
|
||||
/**
|
||||
* preg_replace_callback callback function
|
||||
*
|
||||
* @param $match
|
||||
* @return string
|
||||
*/
|
||||
private static function replace_callback($match)
|
||||
{
|
||||
return ucwords($match[2]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the properly formatted getter function name based on class name and property
|
||||
* Formats the property name to a standard getter function
|
||||
*
|
||||
* @param string $class
|
||||
* @param string $propertyName
|
||||
* @return string getter function name
|
||||
*/
|
||||
public static function getter($class, $propertyName)
|
||||
{
|
||||
return method_exists($class, "get" . ucfirst($propertyName)) ?
|
||||
"get" . ucfirst($propertyName) :
|
||||
"get" . preg_replace_callback("/([_\-\s]?([a-z0-9]+))/", "self::replace_callback", $propertyName);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user