Compare commits
4 Commits
51e11d70a4
...
79216bb727
Author | SHA1 | Date | |
---|---|---|---|
79216bb727 | |||
7c7e366473 | |||
cd6a949e23 | |||
bfaf2b354e |
@ -15,66 +15,104 @@ class Currencies {
|
||||
$output = compact('currencies');
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function show(Request $request, Response $response, ModelFactory $factory, $currency_id): Response {
|
||||
public function get(Request $request, Response $response, ModelFactory $factory, $currency_id): Response {
|
||||
$currency = $factory->find(Currency::class)->one($currency_id);
|
||||
$output = [
|
||||
'currency' => $currency->asArray()
|
||||
'get_data' => compact('currency_id'),
|
||||
'currency' => null
|
||||
];
|
||||
if ($currency) {
|
||||
$output['currency'] = $currency->asArray();
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function add(Request $request, Response $response, ModelFactory $factory): Response {
|
||||
$fields = [
|
||||
'code',
|
||||
'name'
|
||||
];
|
||||
$post = json_decode($request->getBody()->getContents());
|
||||
$data = array_intersect_key((array) $post, array_combine($fields, $fields));
|
||||
$currency = $factory->find(Currency::class)->where([['code', $data['code']]])->one();
|
||||
if (!$currency) {
|
||||
$currency = $factory->create(Currency::class, $data);
|
||||
$currency->save();
|
||||
$currencies = [];
|
||||
if (is_array($post)) {
|
||||
foreach ($post as $obj) {
|
||||
if (!is_object($obj)) {
|
||||
continue;
|
||||
}
|
||||
$currencies []= Currency::add($factory, $obj);
|
||||
}
|
||||
} else {
|
||||
$currencies []= Currency::add($factory, $post);
|
||||
}
|
||||
$output = [
|
||||
'post_data' => $post,
|
||||
'input' => $data,
|
||||
'currency' => $currency->asArray()
|
||||
'currencies' => $currencies
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function edit(Request $request, Response $response, ModelFactory $factory, $currency_id): Response {
|
||||
$currency = $factory->find(Currency::class)->one($currency_id);
|
||||
$fields = [
|
||||
'code',
|
||||
'name'
|
||||
];
|
||||
$post = json_decode($request->getBody()->getContents());
|
||||
$data = array_intersect_key((array) $post, array_combine($fields, $fields));
|
||||
$edited = false;
|
||||
foreach ($data as $field => $value) {
|
||||
if ($currency->{$field} != $value) {
|
||||
$currency->{$field} = $value;
|
||||
$edited = true;
|
||||
}
|
||||
}
|
||||
if ($edited) {
|
||||
$currency->save();
|
||||
}
|
||||
$output = [
|
||||
'get_data' => compact('currency_id'),
|
||||
'post_data' => $post,
|
||||
'input' => $data,
|
||||
'currency' => $currency->asArray()
|
||||
'currency' => null,
|
||||
'edited' => $edited
|
||||
];
|
||||
if ($currency) {
|
||||
$edited = $currency->edit($post);
|
||||
$output['currency'] = $currency->asArray();
|
||||
$output['edited'] = $edited;
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function delete(Request $request, Response $response, ModelFactory $factory, $currency_id): Response {
|
||||
$currency = $factory->find(Currency::class)->one($currency_id);
|
||||
$output = ['get_data' => compact('currency_id'), 'currency' => null, 'deleted' => false];
|
||||
if ($currency) {
|
||||
$output['currency'] = $currency->asArray();
|
||||
$status = $currency->delete();
|
||||
$output['deleted'] = $status;
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function getValues(Request $request, Response $response, ModelFactory $factory, $currency_id): Response {
|
||||
$currency = $factory->find(Currency::class)->one($currency_id);
|
||||
$output = [
|
||||
'get_data' => compact('currency_id'),
|
||||
'currency' => $currency->asArray()
|
||||
'currency' => null,
|
||||
'values' => []
|
||||
];
|
||||
$status = $currency->delete();
|
||||
$output['deleted'] = $status;
|
||||
if ($currency) {
|
||||
$output['currency'] = $currency->asArray();
|
||||
if ($currency->values()) {
|
||||
$output['values'] = array_map(function($item) {
|
||||
return $item->asArray();
|
||||
}, $currency->values());
|
||||
}
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function addValues(Request $request, Response $response, ModelFactory $factory, $currency_id): Response {
|
||||
$currency = $factory->find(Currency::class)->one($currency_id);
|
||||
$post = json_decode($request->getBody()->getContents());
|
||||
$output = [
|
||||
'get_data' => compact('currency_id'),
|
||||
'post_data' => $post,
|
||||
'currency' => null,
|
||||
'values' => []
|
||||
];
|
||||
if ($currency) {
|
||||
$output['currency'] = $currency->asArray();
|
||||
$values = [];
|
||||
if (is_array($post)) {
|
||||
foreach ($post as $obj) {
|
||||
if (!is_object($obj)) {
|
||||
continue;
|
||||
}
|
||||
$values []= $currency->addValue($obj);
|
||||
}
|
||||
} else {
|
||||
$values []= $currency->addValue($post);
|
||||
}
|
||||
$output['values'] = $values;
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
||||
|
113
app/common/Controller/Values.php
Normal file
113
app/common/Controller/Values.php
Normal file
@ -0,0 +1,113 @@
|
||||
<?php
|
||||
namespace ProVM\Money\Common\Controller;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use ProVM\Common\Factory\Model as ModelFactory;
|
||||
use ProVM\Common\Define\Controller\Json;
|
||||
use ProVM\Money\Value;
|
||||
|
||||
class Values {
|
||||
use Json;
|
||||
|
||||
public function __invoke(Request $request, Response $response, ModelFactory $factory): Response {
|
||||
$values = $factory->find(Value::class)->many();
|
||||
$output = compact('values');
|
||||
array_walk($output['values'], function(&$item) {
|
||||
$item = $item->asArray();
|
||||
});
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function get(Request $request, Response $response, ModelFactory $factory, $currency_id, $base_id, $date_time = null): Response {
|
||||
if ($date_time !== null) {
|
||||
$value = $factory->find(Value::class)->where([['currency_id', $currency_id], ['base_id', $base_id], ['date_time', $date_time]])->one();
|
||||
$output = [
|
||||
'get_data' => compact('currency_id', 'date_time', 'base_id'),
|
||||
'value' => null
|
||||
];
|
||||
if ($value) {
|
||||
$output['value'] = $value->asArray();
|
||||
}
|
||||
} else {
|
||||
$values = $factory->find(Value::class)->where([['currency_id', $currency_id], ['base_id', $base_id]])->many();
|
||||
$output = [
|
||||
'get_data' => compact('currency_id', 'date_time', 'base_id'),
|
||||
'values' => null
|
||||
];
|
||||
if ($values) {
|
||||
$vals = [];
|
||||
foreach ($values as $value) {
|
||||
$vals []= $value->asArray();
|
||||
}
|
||||
$output['values'] = $vals;
|
||||
}
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function add(Request $request, Response $response, ModelFactory $factory): Response {
|
||||
$post = json_decode($request->getBody()->getContents());
|
||||
$values = [];
|
||||
if (is_array($post)) {
|
||||
foreach ($post as $obj) {
|
||||
if (!is_object($obj)) {
|
||||
continue;
|
||||
}
|
||||
$values []= Value::add($factory, $obj);
|
||||
}
|
||||
} else {
|
||||
$values []= Value::add($factory, $post);
|
||||
}
|
||||
$output = [
|
||||
'post_data' => $post,
|
||||
'values' => $values
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function edit(Request $request, Response $response, ModelFactory $factory, $currency_id, $base_id, $date_time = null): Response {
|
||||
$post = json_decode($request->getBody()->getContents());
|
||||
$output = [
|
||||
'get_data' => compact('currency_id', 'date_time', 'base_id'),
|
||||
'post_data' => $post
|
||||
];
|
||||
if ($date_time !== null) {
|
||||
$value = $factory->find(Value::class)->where([['currency_id', $currency_id], ['base_id', $base_id], ['date_time', $date_time]])->one();
|
||||
$edited = false;
|
||||
if ($value) {
|
||||
$edited = $value->edit($post);
|
||||
$output['value'] = $value->asArray();
|
||||
$output['edited'] = $edited;
|
||||
}
|
||||
} else {
|
||||
$values = $factory->find(Value::class)->where([['currency_id', $currency_id], ['base_id', $base_id]])->many();
|
||||
$vals = [];
|
||||
foreach ($values as $value) {
|
||||
$edited = $value->edit($post);
|
||||
$vals []= ['value' => $value, 'edited' => $edited];
|
||||
}
|
||||
$output['values'] = $vals;
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function delete(Request $request, Response $response, ModelFactory $factory, $currency_id, $base_id, $date_time = null): Response {
|
||||
$output = ['get_data' => compact('currency_id', 'date_time', 'base_id')];
|
||||
if ($date_time !== null) {
|
||||
$value = $factory->find(Value::class)->where([['currency_id', $currency_id], ['base_id', $base_id], ['date_time', $date_time]])->one();
|
||||
if ($currency) {
|
||||
$output['value'] = $value->asArray();
|
||||
$status = $value->delete();
|
||||
$output['deleted'] = $status;
|
||||
}
|
||||
} else {
|
||||
$values = $factory->find(Value::class)->where([['currency_id', $currency_id], ['base_id', $base_id]])->many();
|
||||
$vals = [];
|
||||
foreach ($values as $value) {
|
||||
$val = ['value' => $value];
|
||||
$status = $value->delete();
|
||||
$val['deleted'] = $status;
|
||||
$vals []= $val;
|
||||
}
|
||||
$output['values'] = $vals;
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
@ -9,7 +9,8 @@
|
||||
"php-di/slim-bridge": "^3.1",
|
||||
"provm/models": "dev-master",
|
||||
"provm/controller": "dev-master",
|
||||
"vlucas/phpdotenv": "^5.3"
|
||||
"vlucas/phpdotenv": "^5.3",
|
||||
"nesbot/carbon": "^2.46"
|
||||
},
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
@ -19,7 +20,9 @@
|
||||
}
|
||||
],
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^8.5"
|
||||
"phpunit/phpunit": "^8.5",
|
||||
"robmorgan/phinx": "^0.12.5",
|
||||
"odan/phinx-migrations-generator": "^5.4"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
|
42
app/phinx.php
Normal file
42
app/phinx.php
Normal file
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
return
|
||||
[
|
||||
'paths' => [
|
||||
'migrations' => '%%PHINX_CONFIG_DIR%%/../db/migrations',
|
||||
'seeds' => '%%PHINX_CONFIG_DIR%%/../db/seeds'
|
||||
],
|
||||
'schema_file' => '%%PHINX_CONFIG_DIR%%/../db/schema.php',
|
||||
'environments' => [
|
||||
'default_migration_table' => 'phinxlog',
|
||||
'default_environment' => 'development',
|
||||
'production' => [
|
||||
'adapter' => 'mysql',
|
||||
'host' => 'localhost',
|
||||
'name' => 'production_db',
|
||||
'user' => 'root',
|
||||
'pass' => '',
|
||||
'port' => '3306',
|
||||
'charset' => 'utf8',
|
||||
],
|
||||
'development' => [
|
||||
'adapter' => 'mysql',
|
||||
'host' => 'localhost',
|
||||
'name' => 'money_dev',
|
||||
'user' => 'money',
|
||||
'pass' => 'money_pass',
|
||||
'port' => '3307',
|
||||
'charset' => 'utf8',
|
||||
],
|
||||
'testing' => [
|
||||
'adapter' => 'mysql',
|
||||
'host' => 'localhost',
|
||||
'name' => 'testing_db',
|
||||
'user' => 'root',
|
||||
'pass' => '',
|
||||
'port' => '3306',
|
||||
'charset' => 'utf8',
|
||||
]
|
||||
],
|
||||
'version_order' => 'creation'
|
||||
];
|
@ -2,5 +2,6 @@
|
||||
use ProVM\Money\Common\Controller\API;
|
||||
|
||||
include_once 'currencies.php';
|
||||
include_once 'values.php';
|
||||
|
||||
$app->get('/', API::class);
|
||||
|
@ -7,5 +7,11 @@ $app->group('/currencies', function($app) {
|
||||
});
|
||||
|
||||
$app->group('/currency/{currency_id}', function($app) {
|
||||
$app->get('[/]', [Currencies::class, 'show']);
|
||||
$app->put('/edit[/]', [Currencies::class, 'edit']);
|
||||
$app->delete('/delete[/]', [Currencies::class, 'delete']);
|
||||
$app->group('/values', function($app) {
|
||||
$app->post('/add[/]', [Currencies::class, 'addValues']);
|
||||
$app->get('[/]', [Currencies::class, 'getValues']);
|
||||
});
|
||||
$app->get('[/]', [Currencies::class, 'get']);
|
||||
});
|
||||
|
18
app/resources/routes/values.php
Normal file
18
app/resources/routes/values.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
use ProVM\Money\Common\Controller\Values;
|
||||
|
||||
$app->group('/values', function($app) {
|
||||
$app->post('/add[/]', [Values::class, 'add']);
|
||||
$app->get('[/]', Values::class);
|
||||
});
|
||||
|
||||
$app->group('/value/{currency_id}/{base_id}/{date_time}', function($app) {
|
||||
$app->put('/edit[/]', [Values::class, 'edit']);
|
||||
$app->delete('/delete[/]', [Values::class, 'delete']);
|
||||
$app->get('[/]', [Values::class, 'get']);
|
||||
});
|
||||
$app->group('/value/{currency_id}/{base_id}', function($app) {
|
||||
$app->put('/edit[/]', [Values::class, 'edit']);
|
||||
$app->delete('/delete[/]', [Values::class, 'delete']);
|
||||
$app->get('[/]', [Values::class, 'get']);
|
||||
});
|
12
db/migrations/20210315223928_inicial_load.php
Normal file
12
db/migrations/20210315223928_inicial_load.php
Normal file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
use Phinx\Db\Adapter\MysqlAdapter;
|
||||
|
||||
class InicialLoad extends Phinx\Migration\AbstractMigration
|
||||
{
|
||||
public function change()
|
||||
{
|
||||
$this->execute("ALTER DATABASE CHARACTER SET 'utf8mb4';");
|
||||
$this->execute("ALTER DATABASE COLLATE='utf8mb4_general_ci';");
|
||||
}
|
||||
}
|
40
db/migrations/20210315235617_create_currencies.php
Normal file
40
db/migrations/20210315235617_create_currencies.php
Normal file
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
use Phinx\Db\Adapter\MysqlAdapter;
|
||||
|
||||
class CreateCurrencies extends Phinx\Migration\AbstractMigration
|
||||
{
|
||||
public function change()
|
||||
{
|
||||
$this->table('currencies', [
|
||||
'id' => false,
|
||||
'primary_key' => ['id'],
|
||||
'engine' => 'InnoDB',
|
||||
'encoding' => 'utf8mb4',
|
||||
'collation' => 'utf8mb4_general_ci',
|
||||
'comment' => '',
|
||||
'row_format' => 'DYNAMIC',
|
||||
])
|
||||
->addColumn('id', 'integer', [
|
||||
'null' => false,
|
||||
'limit' => '10',
|
||||
'signed' => false,
|
||||
'identity' => 'enable',
|
||||
])
|
||||
->addColumn('code', 'string', [
|
||||
'null' => false,
|
||||
'limit' => 5,
|
||||
'collation' => 'utf8mb4_general_ci',
|
||||
'encoding' => 'utf8mb4',
|
||||
'after' => 'id',
|
||||
])
|
||||
->addColumn('name', 'string', [
|
||||
'null' => false,
|
||||
'limit' => 100,
|
||||
'collation' => 'utf8mb4_general_ci',
|
||||
'encoding' => 'utf8mb4',
|
||||
'after' => 'code',
|
||||
])
|
||||
->create();
|
||||
}
|
||||
}
|
77
db/migrations/20210315235815_create_values.php
Normal file
77
db/migrations/20210315235815_create_values.php
Normal file
@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
use Phinx\Db\Adapter\MysqlAdapter;
|
||||
|
||||
class CreateValues extends Phinx\Migration\AbstractMigration
|
||||
{
|
||||
public function change()
|
||||
{
|
||||
$this->table('values', [
|
||||
'id' => false,
|
||||
'primary_key' => ['currency_id', 'date_time', 'base_id'],
|
||||
'engine' => 'InnoDB',
|
||||
'encoding' => 'utf8mb4',
|
||||
'collation' => 'utf8mb4_general_ci',
|
||||
'comment' => '',
|
||||
'row_format' => 'DYNAMIC',
|
||||
])
|
||||
->addColumn('currency_id', 'integer', [
|
||||
'null' => false,
|
||||
'limit' => '10',
|
||||
'signed' => false,
|
||||
])
|
||||
->addColumn('date_time', 'datetime', [
|
||||
'null' => false,
|
||||
'after' => 'currency_id',
|
||||
])
|
||||
->addColumn('value', 'double', [
|
||||
'null' => false,
|
||||
'after' => 'date_time',
|
||||
])
|
||||
->addColumn('base_id', 'integer', [
|
||||
'null' => false,
|
||||
'limit' => '10',
|
||||
'signed' => false,
|
||||
'after' => 'value',
|
||||
])
|
||||
->addIndex(['currency_id'], [
|
||||
'name' => 'currency_id',
|
||||
'unique' => false,
|
||||
])
|
||||
->addIndex(['base_id'], [
|
||||
'name' => 'base_id',
|
||||
'unique' => false,
|
||||
])
|
||||
->create();
|
||||
$this->table('currencies', [
|
||||
'id' => false,
|
||||
'primary_key' => ['id'],
|
||||
'engine' => 'InnoDB',
|
||||
'encoding' => 'utf8mb4',
|
||||
'collation' => 'utf8mb4_general_ci',
|
||||
'comment' => '',
|
||||
'row_format' => 'DYNAMIC',
|
||||
])
|
||||
->addColumn('id', 'integer', [
|
||||
'null' => false,
|
||||
'limit' => '10',
|
||||
'signed' => false,
|
||||
'identity' => 'enable',
|
||||
])
|
||||
->addColumn('code', 'string', [
|
||||
'null' => false,
|
||||
'limit' => 5,
|
||||
'collation' => 'utf8mb4_general_ci',
|
||||
'encoding' => 'utf8mb4',
|
||||
'after' => 'id',
|
||||
])
|
||||
->addColumn('name', 'string', [
|
||||
'null' => false,
|
||||
'limit' => 100,
|
||||
'collation' => 'utf8mb4_general_ci',
|
||||
'encoding' => 'utf8mb4',
|
||||
'after' => 'code',
|
||||
])
|
||||
->create();
|
||||
}
|
||||
}
|
501
db/schema.php
Normal file
501
db/schema.php
Normal file
@ -0,0 +1,501 @@
|
||||
<?php
|
||||
|
||||
return array (
|
||||
'database' =>
|
||||
array (
|
||||
'default_character_set_name' => 'utf8mb4',
|
||||
0 => 'utf8mb4',
|
||||
'default_collation_name' => 'utf8mb4_general_ci',
|
||||
1 => 'utf8mb4_general_ci',
|
||||
),
|
||||
'tables' =>
|
||||
array (
|
||||
'phinxlog' =>
|
||||
array (
|
||||
'table' =>
|
||||
array (
|
||||
'table_name' => 'phinxlog',
|
||||
'engine' => 'InnoDB',
|
||||
'table_comment' => '',
|
||||
'table_collation' => 'utf8_general_ci',
|
||||
'character_set_name' => 'utf8',
|
||||
'row_format' => 'Dynamic',
|
||||
),
|
||||
'columns' =>
|
||||
array (
|
||||
'version' =>
|
||||
array (
|
||||
'TABLE_CATALOG' => 'def',
|
||||
'TABLE_NAME' => 'phinxlog',
|
||||
'COLUMN_NAME' => 'version',
|
||||
'ORDINAL_POSITION' => '1',
|
||||
'COLUMN_DEFAULT' => NULL,
|
||||
'IS_NULLABLE' => 'NO',
|
||||
'DATA_TYPE' => 'bigint',
|
||||
'CHARACTER_MAXIMUM_LENGTH' => NULL,
|
||||
'CHARACTER_OCTET_LENGTH' => NULL,
|
||||
'NUMERIC_PRECISION' => '19',
|
||||
'NUMERIC_SCALE' => '0',
|
||||
'DATETIME_PRECISION' => NULL,
|
||||
'CHARACTER_SET_NAME' => NULL,
|
||||
'COLLATION_NAME' => NULL,
|
||||
'COLUMN_TYPE' => 'bigint(20)',
|
||||
'COLUMN_KEY' => 'PRI',
|
||||
'EXTRA' => '',
|
||||
'PRIVILEGES' => 'select,insert,update,references',
|
||||
'COLUMN_COMMENT' => '',
|
||||
'IS_GENERATED' => 'NEVER',
|
||||
'GENERATION_EXPRESSION' => NULL,
|
||||
),
|
||||
'migration_name' =>
|
||||
array (
|
||||
'TABLE_CATALOG' => 'def',
|
||||
'TABLE_NAME' => 'phinxlog',
|
||||
'COLUMN_NAME' => 'migration_name',
|
||||
'ORDINAL_POSITION' => '2',
|
||||
'COLUMN_DEFAULT' => 'NULL',
|
||||
'IS_NULLABLE' => 'YES',
|
||||
'DATA_TYPE' => 'varchar',
|
||||
'CHARACTER_MAXIMUM_LENGTH' => '100',
|
||||
'CHARACTER_OCTET_LENGTH' => '300',
|
||||
'NUMERIC_PRECISION' => NULL,
|
||||
'NUMERIC_SCALE' => NULL,
|
||||
'DATETIME_PRECISION' => NULL,
|
||||
'CHARACTER_SET_NAME' => 'utf8',
|
||||
'COLLATION_NAME' => 'utf8_general_ci',
|
||||
'COLUMN_TYPE' => 'varchar(100)',
|
||||
'COLUMN_KEY' => '',
|
||||
'EXTRA' => '',
|
||||
'PRIVILEGES' => 'select,insert,update,references',
|
||||
'COLUMN_COMMENT' => '',
|
||||
'IS_GENERATED' => 'NEVER',
|
||||
'GENERATION_EXPRESSION' => NULL,
|
||||
),
|
||||
'start_time' =>
|
||||
array (
|
||||
'TABLE_CATALOG' => 'def',
|
||||
'TABLE_NAME' => 'phinxlog',
|
||||
'COLUMN_NAME' => 'start_time',
|
||||
'ORDINAL_POSITION' => '3',
|
||||
'COLUMN_DEFAULT' => 'NULL',
|
||||
'IS_NULLABLE' => 'YES',
|
||||
'DATA_TYPE' => 'timestamp',
|
||||
'CHARACTER_MAXIMUM_LENGTH' => NULL,
|
||||
'CHARACTER_OCTET_LENGTH' => NULL,
|
||||
'NUMERIC_PRECISION' => NULL,
|
||||
'NUMERIC_SCALE' => NULL,
|
||||
'DATETIME_PRECISION' => '0',
|
||||
'CHARACTER_SET_NAME' => NULL,
|
||||
'COLLATION_NAME' => NULL,
|
||||
'COLUMN_TYPE' => 'timestamp',
|
||||
'COLUMN_KEY' => '',
|
||||
'EXTRA' => '',
|
||||
'PRIVILEGES' => 'select,insert,update,references',
|
||||
'COLUMN_COMMENT' => '',
|
||||
'IS_GENERATED' => 'NEVER',
|
||||
'GENERATION_EXPRESSION' => NULL,
|
||||
),
|
||||
'end_time' =>
|
||||
array (
|
||||
'TABLE_CATALOG' => 'def',
|
||||
'TABLE_NAME' => 'phinxlog',
|
||||
'COLUMN_NAME' => 'end_time',
|
||||
'ORDINAL_POSITION' => '4',
|
||||
'COLUMN_DEFAULT' => 'NULL',
|
||||
'IS_NULLABLE' => 'YES',
|
||||
'DATA_TYPE' => 'timestamp',
|
||||
'CHARACTER_MAXIMUM_LENGTH' => NULL,
|
||||
'CHARACTER_OCTET_LENGTH' => NULL,
|
||||
'NUMERIC_PRECISION' => NULL,
|
||||
'NUMERIC_SCALE' => NULL,
|
||||
'DATETIME_PRECISION' => '0',
|
||||
'CHARACTER_SET_NAME' => NULL,
|
||||
'COLLATION_NAME' => NULL,
|
||||
'COLUMN_TYPE' => 'timestamp',
|
||||
'COLUMN_KEY' => '',
|
||||
'EXTRA' => '',
|
||||
'PRIVILEGES' => 'select,insert,update,references',
|
||||
'COLUMN_COMMENT' => '',
|
||||
'IS_GENERATED' => 'NEVER',
|
||||
'GENERATION_EXPRESSION' => NULL,
|
||||
),
|
||||
'breakpoint' =>
|
||||
array (
|
||||
'TABLE_CATALOG' => 'def',
|
||||
'TABLE_NAME' => 'phinxlog',
|
||||
'COLUMN_NAME' => 'breakpoint',
|
||||
'ORDINAL_POSITION' => '5',
|
||||
'COLUMN_DEFAULT' => '0',
|
||||
'IS_NULLABLE' => 'NO',
|
||||
'DATA_TYPE' => 'tinyint',
|
||||
'CHARACTER_MAXIMUM_LENGTH' => NULL,
|
||||
'CHARACTER_OCTET_LENGTH' => NULL,
|
||||
'NUMERIC_PRECISION' => '3',
|
||||
'NUMERIC_SCALE' => '0',
|
||||
'DATETIME_PRECISION' => NULL,
|
||||
'CHARACTER_SET_NAME' => NULL,
|
||||
'COLLATION_NAME' => NULL,
|
||||
'COLUMN_TYPE' => 'tinyint(1)',
|
||||
'COLUMN_KEY' => '',
|
||||
'EXTRA' => '',
|
||||
'PRIVILEGES' => 'select,insert,update,references',
|
||||
'COLUMN_COMMENT' => '',
|
||||
'IS_GENERATED' => 'NEVER',
|
||||
'GENERATION_EXPRESSION' => NULL,
|
||||
),
|
||||
),
|
||||
'indexes' =>
|
||||
array (
|
||||
'PRIMARY' =>
|
||||
array (
|
||||
1 =>
|
||||
array (
|
||||
'Table' => 'phinxlog',
|
||||
'Non_unique' => '0',
|
||||
'Key_name' => 'PRIMARY',
|
||||
'Seq_in_index' => '1',
|
||||
'Column_name' => 'version',
|
||||
'Collation' => 'A',
|
||||
'Sub_part' => NULL,
|
||||
'Packed' => NULL,
|
||||
'Null' => '',
|
||||
'Index_type' => 'BTREE',
|
||||
'Comment' => '',
|
||||
'Index_comment' => '',
|
||||
),
|
||||
),
|
||||
),
|
||||
'foreign_keys' => NULL,
|
||||
),
|
||||
'currencies' =>
|
||||
array (
|
||||
'table' =>
|
||||
array (
|
||||
'table_name' => 'currencies',
|
||||
'engine' => 'InnoDB',
|
||||
'table_comment' => '',
|
||||
'table_collation' => 'utf8mb4_general_ci',
|
||||
'character_set_name' => 'utf8mb4',
|
||||
'row_format' => 'Dynamic',
|
||||
),
|
||||
'columns' =>
|
||||
array (
|
||||
'id' =>
|
||||
array (
|
||||
'TABLE_CATALOG' => 'def',
|
||||
'TABLE_NAME' => 'currencies',
|
||||
'COLUMN_NAME' => 'id',
|
||||
'ORDINAL_POSITION' => '1',
|
||||
'COLUMN_DEFAULT' => NULL,
|
||||
'IS_NULLABLE' => 'NO',
|
||||
'DATA_TYPE' => 'int',
|
||||
'CHARACTER_MAXIMUM_LENGTH' => NULL,
|
||||
'CHARACTER_OCTET_LENGTH' => NULL,
|
||||
'NUMERIC_PRECISION' => '10',
|
||||
'NUMERIC_SCALE' => '0',
|
||||
'DATETIME_PRECISION' => NULL,
|
||||
'CHARACTER_SET_NAME' => NULL,
|
||||
'COLLATION_NAME' => NULL,
|
||||
'COLUMN_TYPE' => 'int(10) unsigned',
|
||||
'COLUMN_KEY' => 'PRI',
|
||||
'EXTRA' => 'auto_increment',
|
||||
'PRIVILEGES' => 'select,insert,update,references',
|
||||
'COLUMN_COMMENT' => '',
|
||||
'IS_GENERATED' => 'NEVER',
|
||||
'GENERATION_EXPRESSION' => NULL,
|
||||
),
|
||||
'code' =>
|
||||
array (
|
||||
'TABLE_CATALOG' => 'def',
|
||||
'TABLE_NAME' => 'currencies',
|
||||
'COLUMN_NAME' => 'code',
|
||||
'ORDINAL_POSITION' => '2',
|
||||
'COLUMN_DEFAULT' => NULL,
|
||||
'IS_NULLABLE' => 'NO',
|
||||
'DATA_TYPE' => 'varchar',
|
||||
'CHARACTER_MAXIMUM_LENGTH' => '5',
|
||||
'CHARACTER_OCTET_LENGTH' => '20',
|
||||
'NUMERIC_PRECISION' => NULL,
|
||||
'NUMERIC_SCALE' => NULL,
|
||||
'DATETIME_PRECISION' => NULL,
|
||||
'CHARACTER_SET_NAME' => 'utf8mb4',
|
||||
'COLLATION_NAME' => 'utf8mb4_general_ci',
|
||||
'COLUMN_TYPE' => 'varchar(5)',
|
||||
'COLUMN_KEY' => '',
|
||||
'EXTRA' => '',
|
||||
'PRIVILEGES' => 'select,insert,update,references',
|
||||
'COLUMN_COMMENT' => '',
|
||||
'IS_GENERATED' => 'NEVER',
|
||||
'GENERATION_EXPRESSION' => NULL,
|
||||
),
|
||||
'name' =>
|
||||
array (
|
||||
'TABLE_CATALOG' => 'def',
|
||||
'TABLE_NAME' => 'currencies',
|
||||
'COLUMN_NAME' => 'name',
|
||||
'ORDINAL_POSITION' => '3',
|
||||
'COLUMN_DEFAULT' => NULL,
|
||||
'IS_NULLABLE' => 'NO',
|
||||
'DATA_TYPE' => 'varchar',
|
||||
'CHARACTER_MAXIMUM_LENGTH' => '100',
|
||||
'CHARACTER_OCTET_LENGTH' => '400',
|
||||
'NUMERIC_PRECISION' => NULL,
|
||||
'NUMERIC_SCALE' => NULL,
|
||||
'DATETIME_PRECISION' => NULL,
|
||||
'CHARACTER_SET_NAME' => 'utf8mb4',
|
||||
'COLLATION_NAME' => 'utf8mb4_general_ci',
|
||||
'COLUMN_TYPE' => 'varchar(100)',
|
||||
'COLUMN_KEY' => '',
|
||||
'EXTRA' => '',
|
||||
'PRIVILEGES' => 'select,insert,update,references',
|
||||
'COLUMN_COMMENT' => '',
|
||||
'IS_GENERATED' => 'NEVER',
|
||||
'GENERATION_EXPRESSION' => NULL,
|
||||
),
|
||||
),
|
||||
'indexes' =>
|
||||
array (
|
||||
'PRIMARY' =>
|
||||
array (
|
||||
1 =>
|
||||
array (
|
||||
'Table' => 'currencies',
|
||||
'Non_unique' => '0',
|
||||
'Key_name' => 'PRIMARY',
|
||||
'Seq_in_index' => '1',
|
||||
'Column_name' => 'id',
|
||||
'Collation' => 'A',
|
||||
'Sub_part' => NULL,
|
||||
'Packed' => NULL,
|
||||
'Null' => '',
|
||||
'Index_type' => 'BTREE',
|
||||
'Comment' => '',
|
||||
'Index_comment' => '',
|
||||
),
|
||||
),
|
||||
),
|
||||
'foreign_keys' => NULL,
|
||||
),
|
||||
'values' =>
|
||||
array (
|
||||
'table' =>
|
||||
array (
|
||||
'table_name' => 'values',
|
||||
'engine' => 'InnoDB',
|
||||
'table_comment' => '',
|
||||
'table_collation' => 'utf8mb4_general_ci',
|
||||
'character_set_name' => 'utf8mb4',
|
||||
'row_format' => 'Dynamic',
|
||||
),
|
||||
'columns' =>
|
||||
array (
|
||||
'currency_id' =>
|
||||
array (
|
||||
'TABLE_CATALOG' => 'def',
|
||||
'TABLE_NAME' => 'values',
|
||||
'COLUMN_NAME' => 'currency_id',
|
||||
'ORDINAL_POSITION' => '1',
|
||||
'COLUMN_DEFAULT' => NULL,
|
||||
'IS_NULLABLE' => 'NO',
|
||||
'DATA_TYPE' => 'int',
|
||||
'CHARACTER_MAXIMUM_LENGTH' => NULL,
|
||||
'CHARACTER_OCTET_LENGTH' => NULL,
|
||||
'NUMERIC_PRECISION' => '10',
|
||||
'NUMERIC_SCALE' => '0',
|
||||
'DATETIME_PRECISION' => NULL,
|
||||
'CHARACTER_SET_NAME' => NULL,
|
||||
'COLLATION_NAME' => NULL,
|
||||
'COLUMN_TYPE' => 'int(10) unsigned',
|
||||
'COLUMN_KEY' => 'PRI',
|
||||
'EXTRA' => '',
|
||||
'PRIVILEGES' => 'select,insert,update,references',
|
||||
'COLUMN_COMMENT' => '',
|
||||
'IS_GENERATED' => 'NEVER',
|
||||
'GENERATION_EXPRESSION' => NULL,
|
||||
),
|
||||
'date_time' =>
|
||||
array (
|
||||
'TABLE_CATALOG' => 'def',
|
||||
'TABLE_NAME' => 'values',
|
||||
'COLUMN_NAME' => 'date_time',
|
||||
'ORDINAL_POSITION' => '2',
|
||||
'COLUMN_DEFAULT' => NULL,
|
||||
'IS_NULLABLE' => 'NO',
|
||||
'DATA_TYPE' => 'datetime',
|
||||
'CHARACTER_MAXIMUM_LENGTH' => NULL,
|
||||
'CHARACTER_OCTET_LENGTH' => NULL,
|
||||
'NUMERIC_PRECISION' => NULL,
|
||||
'NUMERIC_SCALE' => NULL,
|
||||
'DATETIME_PRECISION' => '0',
|
||||
'CHARACTER_SET_NAME' => NULL,
|
||||
'COLLATION_NAME' => NULL,
|
||||
'COLUMN_TYPE' => 'datetime',
|
||||
'COLUMN_KEY' => 'PRI',
|
||||
'EXTRA' => '',
|
||||
'PRIVILEGES' => 'select,insert,update,references',
|
||||
'COLUMN_COMMENT' => '',
|
||||
'IS_GENERATED' => 'NEVER',
|
||||
'GENERATION_EXPRESSION' => NULL,
|
||||
),
|
||||
'value' =>
|
||||
array (
|
||||
'TABLE_CATALOG' => 'def',
|
||||
'TABLE_NAME' => 'values',
|
||||
'COLUMN_NAME' => 'value',
|
||||
'ORDINAL_POSITION' => '3',
|
||||
'COLUMN_DEFAULT' => NULL,
|
||||
'IS_NULLABLE' => 'NO',
|
||||
'DATA_TYPE' => 'double',
|
||||
'CHARACTER_MAXIMUM_LENGTH' => NULL,
|
||||
'CHARACTER_OCTET_LENGTH' => NULL,
|
||||
'NUMERIC_PRECISION' => '22',
|
||||
'NUMERIC_SCALE' => NULL,
|
||||
'DATETIME_PRECISION' => NULL,
|
||||
'CHARACTER_SET_NAME' => NULL,
|
||||
'COLLATION_NAME' => NULL,
|
||||
'COLUMN_TYPE' => 'double',
|
||||
'COLUMN_KEY' => '',
|
||||
'EXTRA' => '',
|
||||
'PRIVILEGES' => 'select,insert,update,references',
|
||||
'COLUMN_COMMENT' => '',
|
||||
'IS_GENERATED' => 'NEVER',
|
||||
'GENERATION_EXPRESSION' => NULL,
|
||||
),
|
||||
'base_id' =>
|
||||
array (
|
||||
'TABLE_CATALOG' => 'def',
|
||||
'TABLE_NAME' => 'values',
|
||||
'COLUMN_NAME' => 'base_id',
|
||||
'ORDINAL_POSITION' => '4',
|
||||
'COLUMN_DEFAULT' => NULL,
|
||||
'IS_NULLABLE' => 'NO',
|
||||
'DATA_TYPE' => 'int',
|
||||
'CHARACTER_MAXIMUM_LENGTH' => NULL,
|
||||
'CHARACTER_OCTET_LENGTH' => NULL,
|
||||
'NUMERIC_PRECISION' => '10',
|
||||
'NUMERIC_SCALE' => '0',
|
||||
'DATETIME_PRECISION' => NULL,
|
||||
'CHARACTER_SET_NAME' => NULL,
|
||||
'COLLATION_NAME' => NULL,
|
||||
'COLUMN_TYPE' => 'int(10) unsigned',
|
||||
'COLUMN_KEY' => 'PRI',
|
||||
'EXTRA' => '',
|
||||
'PRIVILEGES' => 'select,insert,update,references',
|
||||
'COLUMN_COMMENT' => '',
|
||||
'IS_GENERATED' => 'NEVER',
|
||||
'GENERATION_EXPRESSION' => NULL,
|
||||
),
|
||||
),
|
||||
'indexes' =>
|
||||
array (
|
||||
'PRIMARY' =>
|
||||
array (
|
||||
1 =>
|
||||
array (
|
||||
'Table' => 'values',
|
||||
'Non_unique' => '0',
|
||||
'Key_name' => 'PRIMARY',
|
||||
'Seq_in_index' => '1',
|
||||
'Column_name' => 'date_time',
|
||||
'Collation' => 'A',
|
||||
'Sub_part' => NULL,
|
||||
'Packed' => NULL,
|
||||
'Null' => '',
|
||||
'Index_type' => 'BTREE',
|
||||
'Comment' => '',
|
||||
'Index_comment' => '',
|
||||
),
|
||||
2 =>
|
||||
array (
|
||||
'Table' => 'values',
|
||||
'Non_unique' => '0',
|
||||
'Key_name' => 'PRIMARY',
|
||||
'Seq_in_index' => '2',
|
||||
'Column_name' => 'currency_id',
|
||||
'Collation' => 'A',
|
||||
'Sub_part' => NULL,
|
||||
'Packed' => NULL,
|
||||
'Null' => '',
|
||||
'Index_type' => 'BTREE',
|
||||
'Comment' => '',
|
||||
'Index_comment' => '',
|
||||
),
|
||||
3 =>
|
||||
array (
|
||||
'Table' => 'values',
|
||||
'Non_unique' => '0',
|
||||
'Key_name' => 'PRIMARY',
|
||||
'Seq_in_index' => '3',
|
||||
'Column_name' => 'base_id',
|
||||
'Collation' => 'A',
|
||||
'Sub_part' => NULL,
|
||||
'Packed' => NULL,
|
||||
'Null' => '',
|
||||
'Index_type' => 'BTREE',
|
||||
'Comment' => '',
|
||||
'Index_comment' => '',
|
||||
),
|
||||
),
|
||||
'currency_id' =>
|
||||
array (
|
||||
1 =>
|
||||
array (
|
||||
'Table' => 'values',
|
||||
'Non_unique' => '1',
|
||||
'Key_name' => 'currency_id',
|
||||
'Seq_in_index' => '1',
|
||||
'Column_name' => 'currency_id',
|
||||
'Collation' => 'A',
|
||||
'Sub_part' => NULL,
|
||||
'Packed' => NULL,
|
||||
'Null' => '',
|
||||
'Index_type' => 'BTREE',
|
||||
'Comment' => '',
|
||||
'Index_comment' => '',
|
||||
),
|
||||
),
|
||||
'base_id' =>
|
||||
array (
|
||||
1 =>
|
||||
array (
|
||||
'Table' => 'values',
|
||||
'Non_unique' => '1',
|
||||
'Key_name' => 'base_id',
|
||||
'Seq_in_index' => '1',
|
||||
'Column_name' => 'base_id',
|
||||
'Collation' => 'A',
|
||||
'Sub_part' => NULL,
|
||||
'Packed' => NULL,
|
||||
'Null' => '',
|
||||
'Index_type' => 'BTREE',
|
||||
'Comment' => '',
|
||||
'Index_comment' => '',
|
||||
),
|
||||
),
|
||||
),
|
||||
'foreign_keys' =>
|
||||
array (
|
||||
'values_ibfk_1' =>
|
||||
array (
|
||||
'TABLE_NAME' => 'values',
|
||||
'COLUMN_NAME' => 'currency_id',
|
||||
'CONSTRAINT_NAME' => 'values_ibfk_1',
|
||||
'REFERENCED_TABLE_NAME' => 'currencies',
|
||||
'REFERENCED_COLUMN_NAME' => 'id',
|
||||
'UPDATE_RULE' => 'RESTRICT',
|
||||
'DELETE_RULE' => 'CASCADE',
|
||||
),
|
||||
'values_ibfk_2' =>
|
||||
array (
|
||||
'TABLE_NAME' => 'values',
|
||||
'COLUMN_NAME' => 'base_id',
|
||||
'CONSTRAINT_NAME' => 'values_ibfk_2',
|
||||
'REFERENCED_TABLE_NAME' => 'currencies',
|
||||
'REFERENCED_COLUMN_NAME' => 'id',
|
||||
'UPDATE_RULE' => 'RESTRICT',
|
||||
'DELETE_RULE' => 'CASCADE',
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
5
db/schema.yml
Normal file
5
db/schema.yml
Normal file
@ -0,0 +1,5 @@
|
||||
tables:
|
||||
- name: currencies
|
||||
columns:
|
||||
- name: id
|
||||
|
@ -42,6 +42,7 @@ services:
|
||||
- 8082:8080
|
||||
environment:
|
||||
ADMINER_DESIGN: 'dracula'
|
||||
ADMINER_PLUGINS: 'dump-json'
|
||||
|
||||
volumes:
|
||||
dbdata:
|
||||
|
@ -2,6 +2,7 @@
|
||||
namespace ProVM\Money;
|
||||
|
||||
use ProVM\Common\Alias\Model;
|
||||
use ProVM\Common\Factory\Model as ModelFactory;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
@ -16,5 +17,47 @@ class Currency extends Model {
|
||||
if ($this->values === null) {
|
||||
$this->values = $this->parentOf(Value::class, [Model::CHILD_KEY => 'currency_id']);
|
||||
}
|
||||
return $this->values;
|
||||
}
|
||||
|
||||
protected static $fields = ['code', 'name'];
|
||||
public static function add(ModelFactory $factory, $info) {
|
||||
$input = array_intersect_key((array) $info, array_combine(self::$fields, self::$fields));
|
||||
$currency = $factory->find(Currency::class)->where([['code', $input['code']]])->one();
|
||||
$created = false;
|
||||
$result = (object) compact('input', 'currency', 'created');
|
||||
if (!$currency) {
|
||||
$currency = $factory->create(Currency::class, $input);
|
||||
$created = $currency->save();
|
||||
$result->created = $created;
|
||||
}
|
||||
$result->currency = $currency->asArray();
|
||||
return $result;
|
||||
}
|
||||
protected function checkCode(string $code): bool {
|
||||
return ($this->find(Currency::class)->where([['code', $code]])->one()) ? true : false;
|
||||
}
|
||||
public function edit($info) {
|
||||
$data = array_intersect_key((array) $info, array_combine(self::$fields, self::$fields));
|
||||
$edited = false;
|
||||
foreach ($data as $field => $value) {
|
||||
if ($this->{$field} != $value) {
|
||||
if ($field == 'code' and $this->checkCode($value)) {
|
||||
throw \Exception('Currency code ' . $value . ' is already in the database.');
|
||||
}
|
||||
$this->{$field} = $value;
|
||||
$edited = true;
|
||||
}
|
||||
}
|
||||
if ($edited) {
|
||||
$edited = $this->save();
|
||||
}
|
||||
return $edited;
|
||||
}
|
||||
public function addValue($info) {
|
||||
$arr = (array) $info;
|
||||
$arr['currency_id'] = (int) $this->id;
|
||||
$result = Value::add($this->factory, $arr);
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ namespace ProVM\Money;
|
||||
|
||||
use ProVM\Common\Alias\Model;
|
||||
use ProVM\Common\Define\Model\DateTime;
|
||||
use ProVM\Common\Factory\Model as ModelFactory;
|
||||
|
||||
/**
|
||||
* @property Currency $currency_id
|
||||
@ -10,10 +11,11 @@ use ProVM\Common\Define\Model\DateTime;
|
||||
* @property double $value
|
||||
* @property Currency $base_id
|
||||
*/
|
||||
class Value {
|
||||
class Value extends Model {
|
||||
use DateTime;
|
||||
|
||||
public static $_table = 'values';
|
||||
public static $_id_column = ['currency_id', 'base_id', 'date_time'];
|
||||
|
||||
protected $currency;
|
||||
public function currency(): ?Currency {
|
||||
@ -29,4 +31,43 @@ class Value {
|
||||
}
|
||||
return $this->base;
|
||||
}
|
||||
|
||||
protected static $fields = ['currency_id', 'date_time', 'value', 'base_id'];
|
||||
public static function add(ModelFactory $factory, $info) {
|
||||
$input = array_intersect_key((array) $info, array_combine(self::$fields, self::$fields));
|
||||
$value = $factory->find(Value::class)->where([['currency_id', $input['currency_id']], ['date_time', $input['date_time']], ['base_id', $input['base_id']]])->one();
|
||||
$created = false;
|
||||
$result = (object) compact('input', 'value', 'created');
|
||||
if (!$value) {
|
||||
$value = $factory->create(Value::class, $input);
|
||||
$created = $value->save();
|
||||
$result->created = $created;
|
||||
}
|
||||
$result->value = $value->asArray();
|
||||
return $result;
|
||||
}
|
||||
public function edit($info) {
|
||||
$data = array_intersect_key((array) $info, array_combine(self::$fields, self::$fields));
|
||||
$edited = false;
|
||||
foreach ($data as $field => $value) {
|
||||
if ($this->{$field} != $value) {
|
||||
if ($field == 'currency_id' or $field == 'base_id') {
|
||||
continue;
|
||||
}
|
||||
$this->{$field} = $value;
|
||||
$edited = true;
|
||||
}
|
||||
}
|
||||
if ($edited) {
|
||||
$edited = $this->save();
|
||||
}
|
||||
return $edited;
|
||||
}
|
||||
public function asArray(): array {
|
||||
$output = parent::asArray();
|
||||
$output['currency'] = $this->currency()->asArray();
|
||||
$output['base'] = $this->base()->asArray();
|
||||
$output['date_time'] = $this->dateTime()->format('Y-m-d H:i:s');
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user