Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/some changes #11

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[![Issue Count](https://codeclimate.com/github/atk4/api/badges/issue_count.svg)](https://codeclimate.com/github/atk4/api/issues)

[![License](https://poser.pugx.org/atk4/api/license)](https://packagist.org/packages/atk4/api)
[![GitHub release](https://img.shields.io/github/release/atk4/api.svg?maxAge=2592000)](https://packagist.org/packages/atk4/api)
[![GitHub release](https://img.shields.io/github/release/atk4/api.svg)](https://packagist.org/packages/atk4/api)

End-to-end implementation for your RESTful API and RPC. Provides a very simple means for you to define API end-points for the application that already uses [Agile Data](https://github.com/atk4/data).

Expand All @@ -26,7 +26,7 @@ $api->get('/ping', function() {
});

// Methods can accept arguments, and everything is type-safe.
$api->get('/hello/:name', function (name) {
$api->get('/ping/:name', function (name) {
return "Hello, $name";
});
```
Expand Down
59 changes: 12 additions & 47 deletions examples/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,6 @@
include '../vendor/autoload.php';

$api = new \atk4\api\Api();

class Country extends \atk4\data\Model
{
public $table = 'country';

public function init()
{
parent::init();
$this->addField('name', ['actual'=>'nicename', 'required'=>true, 'type'=>'string']);
$this->addField('sys_name', ['actual'=>'name', 'system'=>true]);

$this->addField('iso', ['caption'=>'ISO', 'required'=>true, 'type'=>'string']);
$this->addField('iso3', ['caption'=>'ISO3', 'required'=>true, 'type'=>'string']);
$this->addField('numcode', ['caption'=>'ISO Numeric Code', 'type'=>'number', 'required'=>true]);
$this->addField('phonecode', ['caption'=>'Phone Prefix', 'type'=>'number']);

$this->addHook('beforeSave', function ($m) {
if (!$m['sys_name']) {
$m['sys_name'] = strtoupper($m['name']);
}
});
}

public function validate($intent = null)
{
$errors = parent::validate($intent);

if (strlen($this['iso']) !== 2) {
$errors['iso'] = 'Must be exactly 2 characters';
}

if (strlen($this['iso3']) !== 3) {
$errors['iso3'] = 'Must be exactly 3 characters';
}

// look if name is unique
$c = clone $this;
$c->unload();
$c->tryLoadBy('name', $this['name']);
if ($c->loaded() && $c->id != $this->id) {
$errors['name'] = 'Country name must be unique';
}

return $errors;
}
}
session_start();
$db = new \atk4\data\Persistence_SQL('mysql:dbname=atk4;host=localhost', 'root', '');

Expand All @@ -59,4 +13,15 @@ public function validate($intent = null)
return "Hello, $hello";
});

$api->rest('/client', new Country($db));
// Basic usage of REST client
$api->rest('/clients', new \atk4\api\tests\Country($db));

// Tweak our model accordingly
$api->rest('/clients2', function () use ($db) {
$c = new \atk4\api\tests\Country($db);
$c->setLimit(10);
$c->setOrder('name');
$c->addCondition('id', '<', 100);

return $c;
});
23 changes: 23 additions & 0 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/
class Api
{
use \atk4\core\AppScopeTrait;

/** @var \Zend\Diactoros\ServerRequest Request object */
public $request;

Expand Down Expand Up @@ -404,6 +406,27 @@ public function rest($pattern, $model = null)
$this->delete($pattern.'/:id', $f);
}

public function prepareModel($model)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this may not be required. Not sure what is this for..

{
if (!$model) {
throw new Exception(['Model must be specified', 'model'=>$model]);
}

if (is_callable($model)) {
$model = call_user_func($model);
} elseif (!is_object($model)) {
if ($this->api) {
return $this->api->factory($model);
}
}

if (!$model) {
throw new Exception(['Model value is incorrect', 'model'=>$model]);
}

return $model;
}

/**
* Our own exception handling.
*
Expand Down
19 changes: 19 additions & 0 deletions tests/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace atk4\api\tests;

class Client extends \atk4\data\Model
{
public $table = 'country';

public function init()
{
parent::init();
$this->addField('name');

$this->hasMany('Invoices', new Invoice());

$this->hasMany('InvoicesDue', (new Invoice($this->persistence))->addCondition('is_paid', false))
->addField('total_due', ['aggregate'=>'sum', 'field'=>'total']);
}
}
49 changes: 49 additions & 0 deletions tests/Country.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace atk4\api\tests;

class Country extends \atk4\data\Model
{
public $table = 'country';

public function init()
{
parent::init();
$this->addField('name', ['actual'=>'nicename', 'required'=>true, 'type'=>'string']);
$this->addField('sys_name', ['actual'=>'name', 'system'=>true]);

$this->addField('iso', ['caption'=>'ISO', 'required'=>true, 'type'=>'string']);
$this->addField('iso3', ['caption'=>'ISO3', 'required'=>true, 'type'=>'string']);
$this->addField('numcode', ['caption'=>'ISO Numeric Code', 'type'=>'number', 'required'=>true]);
$this->addField('phonecode', ['caption'=>'Phone Prefix', 'type'=>'number']);

$this->addHook('beforeSave', function ($m) {
if (!$m['sys_name']) {
$m['sys_name'] = strtoupper($m['name']);
}
});
}

public function validate($intent = null)
{
$errors = parent::validate($intent);

if (strlen($this['iso']) !== 2) {
$errors['iso'] = 'Must be exactly 2 characters';
}

if (strlen($this['iso3']) !== 3) {
$errors['iso3'] = 'Must be exactly 3 characters';
}

// look if name is unique
$c = clone $this;
$c->unload();
$c->tryLoadBy('name', $this['name']);
if ($c->loaded() && $c->id != $this->id) {
$errors['name'] = 'Country name must be unique';
}

return $errors;
}
}
22 changes: 22 additions & 0 deletions tests/Invoice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace atk4\api\tests;

class Invoice extends \atk4\data\Model
{
public $table = 'invoice';
public $title_field = 'ref_no';

public function init()
{
parent::init();
$this->addField('ref_no');

$this->hasOne('client_id', new Client());
$this->addField('is_paid', ['type'=>'boolean', 'default'=>false]);
$this->addField('is_shipped', ['type'=>'boolean', 'default'=>false]);

$this->hasMany('Lines', new Line())
->addField('total', ['aggregate'=>'sum']);
}
}
18 changes: 18 additions & 0 deletions tests/Line.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace atk4\api\tests;

class Line extends \atk4\data\Model
{
public $table = 'line';

public function init()
{
parent::init();
$this->addField('ref_no');

$this->hasOne('client_id', new Client());
$this->hasMany('Lines', new self())
->addField('total', ['aggregate'=>'sum']);
}
}