Skip to content

Commit

Permalink
This is a draft commit and should not be merged without properly rebase.
Browse files Browse the repository at this point in the history
  • Loading branch information
guzzilar committed Jun 24, 2019
1 parent 21898d8 commit 0d3592e
Show file tree
Hide file tree
Showing 43 changed files with 1,115 additions and 708 deletions.
24 changes: 6 additions & 18 deletions lib/Account.php
Original file line number Diff line number Diff line change
@@ -1,37 +1,25 @@
<?php
namespace Omise;

use Omise\Res\OmiseApiResource;

class Account extends OmiseApiResource
class Account extends \Omise\ApiResource
{
const ENDPOINT = 'account';
const OBJECT_NAME = 'account';

/**
* Retrieves an account.
* Retrieves Omise account info.
*
* @return Omise\Account
*/
public static function retrieve()
{
return parent::g_retrieve(get_class(), self::getUrl());
return parent::resourceRetrieve();
}

/**
* @see Omise\Res\OmiseApiResource::g_reload()
* @see Omise\ApiResource::resourceReload()
*/
public function reload()
{
parent::g_reload(self::getUrl());
}

/**
* @param string $id
*
* @return string
*/
private static function getUrl($id = '')
{
return \Omise\ApiRequestor::OMISE_API_URL . self::ENDPOINT . '/' . $id;
parent::resourceReload();
}
}
24 changes: 6 additions & 18 deletions lib/Balance.php
Original file line number Diff line number Diff line change
@@ -1,37 +1,25 @@
<?php
namespace Omise;

use Omise\Res\OmiseApiResource;

class Balance extends OmiseApiResource
class Balance extends \Omise\ApiResource
{
const ENDPOINT = 'balance';
const OBJECT_NAME = 'balance';

/**
* Retrieves a current balance in the account.
* Retrieves a current balance in the given account.
*
* @return Omise\Balance
*/
public static function retrieve()
{
return parent::g_retrieve(get_class(), self::getUrl());
return parent::resourceRetrieve();
}

/**
* @see Omise\Res\OmiseApiResource::g_reload()
* @see Omise\ApiResource::resourceReload()
*/
public function reload()
{
parent::g_reload(self::getUrl());
}

/**
* @param string $id
*
* @return string
*/
private static function getUrl($id = '')
{
return \Omise\ApiRequestor::OMISE_API_URL . self::ENDPOINT . '/' . $id;
parent::resourceReload();
}
}
30 changes: 10 additions & 20 deletions lib/Capabilities.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php
namespace Omise;

use Omise\Res\OmiseApiResource;
use Omise\ApiResource;

class Capabilities extends OmiseApiResource
class Capabilities extends ApiResource
{
const ENDPOINT = 'capability';
const OBJECT_NAME = 'capability';

/**
* @var array of the filterable keys.
Expand All @@ -14,7 +14,7 @@ class Capabilities extends OmiseApiResource
'backend' => ['currency', 'type', 'chargeAmount']
];

protected function __construct()
public function __construct()
{
parent::__construct();
$this->setupFilterShortcuts();
Expand Down Expand Up @@ -48,22 +48,22 @@ protected function setupFilterShortcuts()
*/
public static function retrieve()
{
return parent::g_retrieve(get_class(), self::getUrl());
return parent::resourceRetrieve();
}

/**
* @see Omise\Res\OmiseApiResource::g_reload()
* @see Omise\ApiResource::resourceReload()
*/
public function reload()
{
parent::g_reload(self::getUrl());
parent::resourceReload();
}

/**
* Retrieves array of payment backends. Optionally pass in as many filter functions as you want
* (muliple arguments, or a single array)
*
* @param [func1,fun2,...] OR func1, func2,...
* @param [func1,fun2,...] OR func1, func2,...
*
* @return array
*/
Expand Down Expand Up @@ -165,18 +165,8 @@ private static function argsToVariadic($argArray)
/**
* @return string
*/
private static function getUrl()
protected function credential()
{
return \Omise\ApiRequestor::OMISE_API_URL . self::ENDPOINT;
}

/**
* Returns the public key.
*
* @return string
*/
protected function getResourceKey()
{
return $this->_publickey;
return 'pkey_test_5fcnkn4378pmguq5zpy';
}
}
88 changes: 40 additions & 48 deletions lib/Charge.php
Original file line number Diff line number Diff line change
@@ -1,51 +1,60 @@
<?php
namespace Omise;

use Omise\Res\OmiseApiResource;
use Omise\Collection;
use Omise\Refund;
use Omise\RefundList;
use Omise\Resource;
use Omise\ScheduleList;
use Omise\Scheduler;
use Omise\Search;

class Charge extends OmiseApiResource
class Charge extends \Omise\ApiResource
{
const ENDPOINT = 'charges';
const OBJECT_NAME = 'charge';

/**
* Retrieves a charge.
* @param string $query
*
* @param string $id
* @return Omise\Search
*/
public static function search($query = '')
{
return Search::scope('charge')->query($query);
}

/**
* Retrieves a collection of charge objects.
*
* @return Omise\Charge
* @param array $query
*
* @return Omise\Collection
*/
public static function retrieve($id = '')
public static function all($query = array())
{
return parent::g_retrieve(get_class(), self::getUrl($id));
$resource = Resource::newObject(static::OBJECT_NAME);
$result = $resource->request()->get($resource->url(), $resource->credential(), $query);

return new Collection($result);
}

/**
* Search for charges.
* Retrieves a charge.
*
* @param string $query
* @param string $id
*
* @return Omise\Search
* @return Omise\Charge
*/
public static function search($query = '')
public static function retrieve($id)
{
return Search::scope('charge')->query($query);
return parent::resourceRetrieve($id);
}

/**
* @see Omise\Res\OmiseApiResource::g_reload()
* @see Omise\ApiResource::resourceReload()
*/
public function reload()
{
if ($this['object'] === 'charge') {
parent::g_reload(self::getUrl($this['id']));
} else {
parent::g_reload(self::getUrl());
}
parent::resourceReload();
}

/**
Expand All @@ -69,15 +78,15 @@ public static function schedule($params)
*/
public static function create($params)
{
return parent::g_create(get_class(), self::getUrl(), $params);
return parent::resourceCreate($params);
}

/**
* @see Omise\Res\OmiseApiResource::g_update()
* @see Omise\ApiResource::resourceUpdate()
*/
public function update($params)
{
parent::g_update(self::getUrl($this['id']), $params);
parent::resourceUpdate($params);
}

/**
Expand All @@ -87,9 +96,7 @@ public function update($params)
*/
public function capture()
{
$result = $this->apiRequestor->post(self::getUrl($this['id']).'/capture', parent::getResourceKey());
$this->refresh($result);

$this->refresh($this->chainRequest('post', 'capture'));
return $this;
}

Expand All @@ -100,7 +107,9 @@ public function capture()
*/
public function refund($params)
{
$result = $this->apiRequestor->post(self::getUrl($this['id']) . '/refunds', parent::getResourceKey(), $params);
$result = $this->chainRequest('post', 'refunds', $params);

$this->reload();
return new Refund($result);
}

Expand All @@ -111,26 +120,19 @@ public function refund($params)
*/
public function reverse()
{
$result = $this->apiRequestor->post(self::getUrl($this['id']).'/reverse', parent::getResourceKey());
$this->refresh($result);

$this->refresh($this->chainRequest('post', 'reverse'));
return $this;
}

/**
* list refunds
*
* @return Omise\RefundList
* @return \Omise\Collection Of Omise\Refund objects.
*/
public function refunds($options = array())
{
if (is_array($options) && ! empty($options)) {
$refunds = $this->apiRequestor->get(self::getUrl($this['id']) . '/refunds?' . http_build_query($options), parent::getResourceKey());
} else {
$refunds = $this['refunds'];
}

return new RefundList($refunds, $this['id']);
$refunds = is_array($options) && ! empty($options) ? $this->chainRequest('get', 'refunds', $options) : $this['refunds'];
return new Collection($refunds);
}

/**
Expand All @@ -148,14 +150,4 @@ public static function schedules($options = array())

return parent::g_retrieve('\Omise\ScheduleList', self::getUrl('schedules' . $options));
}

/**
* @param string $id
*
* @return string
*/
private static function getUrl($id = '')
{
return \Omise\ApiRequestor::OMISE_API_URL . self::ENDPOINT . '/' . $id;
}
}
64 changes: 64 additions & 0 deletions lib/Collection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
namespace Omise;

use Omise\Resource;

class Collection implements \Countable
{
/**
* @var string Of a collection name.
*/
protected $collection;

/**
* @var array
*/
protected $attributes = array();

/**
* @var array
*/
protected $items = array();

public function __construct($items)
{
$this->mapAttribute($items);

foreach ($items['data'] as $key => $value) {
$this->items[$key] = Resource::newObject($value['object'], $value);
}

$this->collection = $this->first()['object'];
}

/**
* @return Omise\Object In a first position of an array ($items).
*/
public function first()
{
return $this->items[0];
}

/**
* @implements \Countable
*
* @return int
*/
public function count()
{
return $this->attributes['total'];
}

protected function mapAttribute($items)
{
$this->attributes = array(
'from' => $items['from'],
'to' => $items['to'],
'offset' => $items['offset'],
'limit' => $items['limit'],
'total' => $items['total'],
'order' => $items['order'],
'location' => $items['location'],
);
}
}
Loading

0 comments on commit 0d3592e

Please sign in to comment.