Removing Dependency from SDK Core Project

- Copied files required for Rest API SDK
- Removed PPApiContext and directly connected APIContext with PPConfigManager
- Removed duplicate data storage of configuration and credentials.
- Code Style Fixes
- Remove build.xml file as it is not required anymore
- Updated the samples
- Updated the documentations
This commit is contained in:
japatel
2014-10-06 11:16:47 -05:00
parent bb7654b0b8
commit 49b80f76af
150 changed files with 10975 additions and 6525 deletions

View File

@@ -0,0 +1,28 @@
<?php
namespace PayPal\Common;
/**
* Class PPArrayUtil
* Util Class for Arrays
*
* @package PayPal\Common
*/
class PPArrayUtil
{
/**
*
* @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;
}
}

View File

@@ -0,0 +1,148 @@
<?php
namespace PayPal\Common;
/**
* Generic Model class that all API domain classes extend
* Stores all member data in a Hashmap that enables easy
* JSON encoding/decoding
*/
class PPModel
{
private $_propMap = array();
/**
* Magic Get Method
*
* @param $key
* @return mixed
*/
public function __get($key)
{
return $this->_propMap[$key];
}
/**
* Magic Set Method
*
* @param $key
* @param $value
*/
public function __set($key, $value)
{
$this->_propMap[$key] = $value;
}
/**
* 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 PPModel) {
$ret[$k] = $v->toArray();
} else if (is_array($v)) {
$ret[$k] = $this->_convertToArray($v);
} else {
$ret[$k] = $v;
}
}
return $ret;
}
/**
* Fills object value from Array list
*
* @param $arr
* @return $this
*/
public function fromArray($arr)
{
foreach ($arr as $k => $v) {
if (is_array($v)) {
$clazz = PPReflectionUtil::getPropertyClass(get_class($this), $k);
if (PPArrayUtil::isAssocArray($v)) {
/** @var self $o */
$o = new $clazz();
$o->fromArray($v);
$this->__set($k, $o);
} else {
$arr = array();
foreach ($v as $nk => $nv) {
if (is_array($nv)) {
$o = new $clazz();
$o->fromArray($nv);
$arr[$nk] = $o;
} else {
$arr[$nk] = $nv;
}
}
$this->__set($k, $arr);
}
} else {
$this->$k = $v;
}
}
return $this;
}
/**
* 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)
{
return json_encode($this->toArray(), $options);
}
}

View File

@@ -0,0 +1,102 @@
<?php
namespace PayPal\Common;
/**
* Class PPReflectionUtil
*
* @package PayPal\Common
*/
class PPReflectionUtil
{
/**
* Reflection Methods
*
* @var \ReflectionMethod[]
*/
private static $propertiesRefl = array();
/**
* Properties Type
*
* @var string[]
*/
private static $propertiesType = array();
/**
* Gets Property Class
*
* @param $class
* @param $propertyName
* @return string
*/
public static function getPropertyClass($class, $propertyName)
{
if (($annotations = self::propertyAnnotations($class, $propertyName)) && isset($annotations['return'])) {
$param = $annotations['return'];
}
if (isset($param)) {
$anno = explode(' ', $param);
return $anno[0];
} else {
return 'string';
}
}
/**
* 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 = method_exists($class, "get" . ucfirst($propertyName)) ?
"get" . ucfirst($propertyName) :
"get" . preg_replace_callback("/([_\-\s]?([a-z0-9]+))/", "self::replace_callback", $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]);
}
}

View File

@@ -0,0 +1,59 @@
<?php
namespace PayPal\Common;
/**
* Class PPUserAgent
* PPUserAgent generates User Agent for curl requests
*
* @package PayPal\Common
*/
class PPUserAgent
{
/**
* 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(
'lang=PHP',
'v=' . 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[] = 'openssl=' . $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;
}
}
}