Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .github/workflows/citests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ jobs:
extensions: xml, curl, dom, mbstring
- name: Install Composer dependencies
run: composer install
- name: Running unit test
run: ./vendor/bin/phpunit Tests
- name: Running unit tests
run: ./vendor/bin/phpunit Tests/Unit
- name: Running integration tests
run: ./vendor/bin/phpunit Tests/Int
env:
API_KEY: ${{ secrets.API_KEY }}
12 changes: 7 additions & 5 deletions Payload/API.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
<?php
namespace Payload;

require_once('Attr.php');

class API {
const version = '0.4.0';
class API
{
const version = '1.0.0';
public static $api_key;
public static $api_url = 'https://api.payload.com';
public static $api_version;
public static function __callStatic($name, $arguments) {
assert( $name == 'attr' );
public static function __callStatic($name, $arguments)
{
assert($name == 'attr');
return new Attr();
}
}
?>
159 changes: 117 additions & 42 deletions Payload/ARMObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,95 +3,170 @@

require_once('ARMRequest.php');

class ARMObject {
private static $_object_cache = array();
public static $spec = array();
public static $type = null;
class ARMObject
{
private static $_object_cache = [];
private static $_registry = [];
public static $spec = [];
private static $_default_params = [];

public static function setDefaultParams($params) {
self::$_default_params[get_called_class()] = $params;
}

public static function getDefaultParams() {
return self::$_default_params[get_called_class()] ?? [];
}

public static function getRegistry()
{
if (empty(self::$_registry)) {
foreach (glob(__DIR__ . '/*.php') as $file) {
require_once($file);
}
foreach (get_declared_classes() as $class) {
if (is_subclass_of($class, self::class) && isset($class::$spec['object'])) {
self::$_registry[$class::$spec['object']] = $class;
}
}
}
return self::$_registry;
}

public static function new($data) {
public static function new($data)
{
$class = get_called_class();
if ( isset($data['id']) && isset(self::$_object_cache[$data['id']]) ) {
if (isset($data['id']) && isset(self::$_object_cache[$data['id']])) {
$cached_object = self::$_object_cache[$data['id']];
$cached_object->data($data);
return $cached_object;
}
return new $class($data);
}

function __construct($data) {
if ($data !== null)
function __construct($data)
{
if ($data !== null) {
$this->data($data);
}
}

private static function _build_request($cls = null, $apply_polymorphic = false)
{
$cls = $cls ?: get_called_class();
$req = new ARMRequest($cls);
if ($apply_polymorphic && isset($cls::$spec['polymorphic_type'])) {
$req->filter_by(['type' => $cls::$spec['polymorphic_type']]);
}
return $req;
}

public function __get($name) {
if (array_key_exists($name, $this->_data))
public function __get($name)
{
if (array_key_exists($name, $this->_data)) {
return $this->_data[$name];
else
} else {
throw new \Exception("$name does not exist");
}
}

public function json() {
public function json()
{
return json_encode($this->data(), JSON_PRETTY_PRINT);
}


public function data($data=null) {
if ( $data !== null ) {
public function data($data = null)
{
if ($data !== null) {
$this->_data = Utils::data2object($data);
if ( isset($data['id']) )
if (isset($data['id'])) {
self::$_object_cache[$this->id] = $this;
}
} else {
return Utils::object2data($this->_data);
}
}

public function update($update) {
return (new ARMRequest(get_called_class()))->request('put',
array('id'=>$this->id, 'json'=>$update));
public function update($update)
{
return static::_build_request(static::class)->request(
'put',
['id'=>$this->id, 'json'=>$update]
);
}

public function delete($update=null) {
if ($update !== null){
return (new ARMRequest(get_called_class()))->request('delete',
array('id'=>$update->id));
public function delete($update = null)
{
if ($update !== null) {
return static::_build_request(static::class)->request(
'delete',
['id'=>$update->id]
);
} else {
return (new ARMRequest(get_called_class()))->request('delete',
array('id'=>$this->id));
return static::_build_request(static::class)->request(
'delete',
['id'=>$this->id]
);
}
}

public static function get($id) {
return (new ARMRequest(get_called_class()))->get($id);
public static function get($id)
{
return static::_build_request()->get($id);
}

public static function filter_by(...$filters) {
if ( isset(get_called_class()::$spec['polymorphic_type']) )
array_push($filters, ['type' => get_called_class()::$spec['polymorphic_type']]);
public static function all()
{
return static::_build_request()->all();
}

$req = new ARMRequest(get_called_class());
return call_user_func_array(array($req, 'filter_by'), $filters);
public static function filter_by(...$filters)
{
$req = static::_build_request(null, true);
return call_user_func_array([$req, 'filter_by'], $filters);
}

public static function create($obj) {
if ( isset(get_called_class()::$spec['polymorphic_type']) )
public static function create($obj)
{
if (isset(get_called_class()::$spec['polymorphic_type'])) {
$obj['type'] = get_called_class()::$spec['polymorphic_type'];
}

return (new ARMRequest(get_called_class()))->create($obj);
return static::_build_request()->create($obj);
}

public static function select(...$attrs) {
$req = new ARMRequest(get_called_class());
return call_user_func_array(array($req, 'select'), $attrs);
public static function select(...$attrs)
{
$req = static::_build_request(null, true);
return call_user_func_array([$req, 'select'], $attrs);
}

public static function delete_all(...$objs) {
public static function order_by(...$attrs)
{
$req = static::_build_request(null, true);
return call_user_func_array([$req, 'order_by'], $attrs);
}

public static function limit($limit)
{
return static::_build_request(null, true)->limit($limit);
}

public static function offset($offset)
{
return static::_build_request(null, true)->offset($offset);
}

$func = function($value) {
public static function delete_all(...$objs)
{
$func = function ($value) {
return $value->id;
};

return (new ARMRequest(get_called_class()))->request('delete',
array('id'=>join("|",array_map($func, $objs))));
return static::_build_request()->request(
'delete',
['id'=>join("|", array_map($func, $objs))]
);
}
}
?>
Loading