Initial commit

This commit is contained in:
aydiv
2013-03-06 18:42:06 +05:30
commit 69cb3c4dcb
101 changed files with 6222 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
<?php
namespace 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;
}
}

View File

@@ -0,0 +1,88 @@
<?php
namespace PayPal\Common;
use PayPal\Api\Payer;
/**
* Generic Model class that all API domain classes extend
* Stores all member data in a hashmap that enables easy
* JSON encoding/decoding
*/
class Model {
private $_propMap = array();
public function __get($key) {
return $this->_propMap[$key];
}
public function __set($key, $value) {
$this->_propMap[$key] = $value;
}
public function __isset($key) {
return isset($this->_propMap[$key]);
}
public function __unset($key) {
unset($this->_propMap[$key]);
}
private function _convertToArray($param) {
$ret = array();
foreach($param as $k => $v) {
if($v instanceof Model ) {
$ret[$k] = $v->toArray();
} else if (is_array($v)) {
$ret[$k] = $this->_convertToArray($v);
} else {
$ret[$k] = $v;
}
}
return $ret;
}
public function fromArray($arr) {
foreach($arr as $k => $v) {
if(is_array($v)) {
$clazz = ReflectionUtil::getPropertyClass(get_class($this), $k);
if(ArrayUtil::isAssocArray($v)) {
$o = new $clazz();
$o->fromArray($v);
$setterFunc = "set".ucfirst($k);
$this->$setterFunc($o);
} else {
$setterFunc = "set".ucfirst($k);
$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->$setterFunc($arr); //TODO: Cleaning up any current values in this case. Should be doing this allways
}
}else {
$this->$k = $v;
}
}
}
public function fromJson($json) {
$this->fromArray(json_decode($json, true));
}
public function toArray() {
return $this->_convertToArray($this->_propMap);
}
public function toJSON() {
return json_encode($this->toArray());
}
}

View File

@@ -0,0 +1,73 @@
<?php
namespace PayPal\Common;
class ReflectionUtil {
/**
* @var array|ReflectionMethod[]
*/
private static $propertiesRefl = array();
/**
* @var array|string[]
*/
private static $propertiesType = array();
/**
*
* @param string $class
* @param string $propertyName
*/
public static function getPropertyClass($class, $propertyName) {
if (($annotations = self::propertyAnnotations($class, $propertyName)) && isset($annotations['param'])) {
// if (substr($annotations['param'], -2) === '[]') {
// $param = substr($annotations['param'], 0, -2);
// }
$param = $annotations['param'];
}
if(isset($param)) {
$anno = explode(' ', $param);
return $anno[0];
} else {
return 'string';
}
}
/**
* @param string $class
* @param string $propertyName
* @throws RuntimeException
* @return string
*/
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;
}
$setterFunc = "set" . ucfirst($propertyName);
if (!($refl =& self::$propertiesRefl[$class][$propertyName])) {
$refl = new \ReflectionMethod($class, $setterFunc);
}
// 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;
}
}

View File

@@ -0,0 +1,44 @@
<?php
namespace PayPal\Common;
define('SDK_NAME', 'REST-SDK-PHP');
define('SDK_VERSION', '0.5.0');
class UserAgent {
/**
* Returns the value of the User-Agent header
* Add environment values and php version numbers
*/
public static function getValue() {
$featureList = array(
'Lang=PHP',
'V=' . PHP_VERSION,
'Bit=' . UserAgent::_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)", SDK_NAME, SDK_VERSION, implode(';', $featureList));
}
private static function _getPHPBit() {
switch(PHP_INT_SIZE) {
case 4:
return '32';
case 8:
return '64';
default:
return PHP_INT_SIZE;
}
}
}