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

Rename "money" type to "atk4_money" type #895

Merged
merged 8 commits into from
Oct 1, 2021
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
4 changes: 2 additions & 2 deletions .github/workflows/test-unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ jobs:
- name: "Run tests: SQLite Hintable (only for Phpunit)"
if: matrix.type == 'Phpunit'
run: |
sed -i 's~"psr-4": {~"psr-4": { "Mvorisek\\\\Atk4\\\\Hintable\\\\Tests\\\\": "vendor/mahalux/atk4-hintable/tests/",~' composer.json && composer dump
vendor/bin/phpunit --configuration vendor/mahalux/atk4-hintable/phpunit.xml.dist --bootstrap vendor/autoload.php --no-coverage -v
sed -i 's~"psr-4": {~"psr-4": { "Mvorisek\\\\Atk4\\\\Hintable\\\\Tests\\\\": "vendor/mvorisek/atk4-hintable/tests/",~' composer.json && composer dump
vendor/bin/phpunit --configuration vendor/mvorisek/atk4-hintable/phpunit.xml.dist --bootstrap vendor/autoload.php --no-coverage -v

- name: Check Coding Style (only for CodingStyle)
if: matrix.type == 'CodingStyle'
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class JobReport extends Job {

// Build relation between job and invoice line
$this->hasMany('InvoiceLines', ['model' => $invoice_lines])
->addField('invoiced', ['aggregate' => 'sum', 'field' => 'total', 'type' => 'money']);
->addField('invoiced', ['aggregate' => 'sum', 'field' => 'total', 'type' => 'atk4_money']);

// Next we need to see how much is reported through timesheets
$timesheet = new Timesheet($this->persistence);
Expand All @@ -153,7 +153,7 @@ class JobReport extends Job {

// Build relation between Job and Timesheets
$this->hasMany('Timesheets', ['model' => $timesheet])
->addField('reported', ['aggregate' => 'sum', 'field' => 'cost', 'type' => 'money']);
->addField('reported', ['aggregate' => 'sum', 'field' => 'cost', 'type' => 'atk4_money']);

// Finally lets calculate profit
$this->addExpression('profit', '[invoiced]-[reported]');
Expand Down Expand Up @@ -565,14 +565,15 @@ Define your first model class:

``` php
namespace my;

class User extends \Atk4\Data\Model
{
public $table = 'user';
function init(): void
{
parent::init();

$this->addFields(['email','name','password']);
$this->addFields(['email', 'name', 'password']);
// use your table fields here
}
}
Expand All @@ -599,7 +600,7 @@ Now you can explore. Try typing:
> $m = new \my\User($db);
> $m->loadBy('email', '[email protected]')
> $m->get()
> $m->export(['email','name'])
> $m->export(['email', 'name'])
> $m->action('count')
> $m->action('count')->getOne()
```
Expand Down
20 changes: 17 additions & 3 deletions bootstrap-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types as DbalTypes;

// TODO types to migration to DBAL, might be removed later

final class Types
{
public const MONEY = 'money';
public const MONEY = 'atk4_money';
}

class MoneyType extends DbalTypes\Type
Expand All @@ -25,6 +23,22 @@ public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $pla
{
return DbalTypes\Type::getType(DbalTypes\Types::FLOAT)->getSQLDeclaration($fieldDeclaration, $platform);
}

public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string
{
if ($value === null || trim((string) $value) === '') {
return null;
}

return (string) round((float) $value, 4);
}

public function convertToPHPValue($value, AbstractPlatform $platform): float
{
$v = $this->convertToDatabaseValue($value, $platform);

return $v === null ? null : (float) $v;
}
}

DbalTypes\Type::addType(Types::MONEY, MoneyType::class);
14 changes: 4 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,15 @@
"ext-pdo": "*",
"atk4/core": "dev-develop",
"doctrine/dbal": "^2.13.3 || ^3.0",
"mahalux/atk4-hintable": "~1.3.2"
"mvorisek/atk4-hintable": "~1.5.0"
},
"require-release": {
"php": ">=7.4.0",
"ext-intl": "*",
"ext-pdo": "*",
"atk4/core": "~3.1.0",
"doctrine/dbal": "^2.13.3 || ^3.0",
"mahalux/atk4-hintable": "~1.3.2"
},
"conflict": {
"atk4/dsql": "*",
"atk4/schema": "*"
"mvorisek/atk4-hintable": "~1.5.0"
},
"require-dev": {
"ergebnis/composer-normalize": "^2.13",
Expand All @@ -69,17 +65,15 @@
},
"autoload": {
"psr-4": {
"Atk4\\Data\\": "src/",
"Atk4\\Schema\\": "src-schema/"
"Atk4\\Data\\": "src/"
},
"files": [
"bootstrap-types.php"
]
},
"autoload-dev": {
"psr-4": {
"Atk4\\Data\\Tests\\": "tests/",
"Atk4\\Schema\\Tests\\": "tests-schema/"
"Atk4\\Data\\Tests\\": "tests/"
}
},
"minimum-stability": "dev",
Expand Down
4 changes: 2 additions & 2 deletions docs/design.rst
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ Code::
class Model_Client extends Model_User {
public function sendPasswordReminder() {

mail($this->get('email'), 'Your password is: '.$this->get('password'));
mail($this->get('email'), 'Your password is: ' . $this->get('password'));
}
}

Expand Down Expand Up @@ -206,7 +206,7 @@ Code to declare fields::
parent::init();

$this->addField('description');
$this->addField('amount')->type('money');
$this->addField('amount')->type('atk4_money');
$this->addField('is_paid')->type('boolean');
}
}
Expand Down
20 changes: 4 additions & 16 deletions docs/model.rst
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,9 @@ Creates multiple field objects in one method call. See multiple syntax examples:
$m->addFields([
'last_name',
'login' => ['default' => 'unknown'],
'salary' => ['type' => 'money', CustomField::class, 'default' => 100],
['tax', CustomField::class, 'type' => 'money', 'default' => 20],
'vat' => new CustomField(['type' => 'money', 'default' => 15]),
'salary' => ['type' => 'atk4_money', CustomField::class, 'default' => 100],
['tax', CustomField::class, 'type' => 'atk4_money', 'default' => 20],
'vat' => new CustomField(['type' => 'atk4_money', 'default' => 15]),
]);


Expand Down Expand Up @@ -320,18 +320,6 @@ This can also be useful for calculating relative times::
}


Strict Fields
^^^^^^^^^^^^^

.. php:property:: strict_fields

By default model will only allow you to operate with values for the fields
that have been defined through addField(). If you attempt to get, set or
otherwise access the value of any other field that has not been properly
defined, you'll get an exception. Read more about :php:class:`Field`

If you set `strict_fields` to false, then the check will not be performed.

Actions
-------
Another common thing to define inside :php:meth:`Model::init()` would be
Expand All @@ -357,7 +345,7 @@ a user invokable actions::

$this->save(['password' => .. ]);

return 'generated and sent password to '.$m->get('name');
return 'generated and sent password to ' . $m->get('name');
}
}

Expand Down
4 changes: 2 additions & 2 deletions docs/persistence.rst
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ It's not only the 'type' property, but 'enum' can also imply restrictions::

There are also non-trivial types in Agile Data::

$m->addField('salary', ['type' => 'money']);
$m->addField('salary', ['type' => 'atk4_money']);
$m->set('salary', "20"); // converts to 20.00

$m->addField('date', ['type' => 'date']);
Expand Down Expand Up @@ -273,7 +273,7 @@ Your init() method for a Field_Currency might look like this::

$this->getOwner()->addField(
$f.'_amount',
['type' => 'money', 'system' => true]
['type' => 'atk4_money', 'system' => true]
);

$this->getOwner()->hasOne(
Expand Down
2 changes: 1 addition & 1 deletion docs/persistence/csv.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ which fields you would like to see in the CSV::

foreach (new Model_User($db) as $m) {
$m->withPersistence($csv)
->onlyFields(['id','name','password'])
->onlyFields(['id', 'name', 'password'])
->save();
}

Expand Down
2 changes: 1 addition & 1 deletion docs/references.rst
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ for a sum.

You can also specify a type yourself::

->addField('paid_amount', ['aggregate' => 'sum', 'field' => 'amount', 'type' => 'money']);
->addField('paid_amount', ['aggregate' => 'sum', 'field' => 'amount', 'type' => 'atk4_money']);

Aggregate fields are always declared read-only, and if you try to
change them (`$m->set('paid_amount', 123);`), you will receive exception.
Expand Down
8 changes: 4 additions & 4 deletions docs/sql.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ SQL Reference
Second argument could be array containing additional settings for the field::

$model->hasOne('account_id', ['model' => [Account::class]])
->addField('account_balance', ['balance', 'type' => 'money']);
->addField('account_balance', ['balance', 'type' => 'atk4_money']);

Returns new field object.

Expand All @@ -79,15 +79,15 @@ SQL Reference
->addFields([
'opening_balance',
'balance'
], ['type' => 'money']);
], ['type' => 'atk4_money']);

You can also specify aliases::

$model->hasOne('account_id', ['model' => [Account::class]])
->addFields([
'opening_balance',
'account_balance' => 'balance'
], ['type' => 'money']);
], ['type' => 'atk4_money']);

If you need to pass more details to individual field, you can also use sub-array::

Expand All @@ -96,7 +96,7 @@ SQL Reference
[
['opening_balance', 'caption' => 'The Opening Balance'],
'account_balance' => 'balance'
], ['type' => 'money']);
], ['type' => 'atk4_money']);

Returns $this.

Expand Down
2 changes: 1 addition & 1 deletion docs/typecasting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ Supported types
- 'text' - for storing long strings, suchas notes or description. Normalize will trim the value.
- 'boolean' - normalize will cast value to boolean.
- 'integer' - normalize will cast value to integer.
- 'money' - normalize will round value with 4 digits after dot.
- 'atk4_money' - normalize will round value with 4 digits after dot.
- 'float' - normalize will cast value to float.
- 'date' - normalize will convert value to DateTime object.
- 'datetime' - normalize will convert value to DateTime object.
Expand Down
2 changes: 1 addition & 1 deletion docs/types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ as `['Number', 'precision' => 2, 'prefix' => '€']`
Not only this allows us make a flexible and re-usable functionality for fields,
but also allows for an easy way to override::

$model->addField('salary', ['type' => 'money', 'precision' => 4', 'prefix' => false, 'postfix' => 'Rub']);
$model->addField('salary', ['type' => 'atk4_money', 'precision' => 4', 'prefix' => false, 'postfix' => 'Rub']);

Although some configuration of the field may appear irrelevant (prefix/postfix)
to operations with data from inside PHP, those properties can be used by
Expand Down
4 changes: 2 additions & 2 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
includes:
- vendor/mahalux/atk4-hintable/phpstan-ext.neon
- vendor/mvorisek/atk4-hintable/phpstan-ext.neon

parameters:
level: 6
Expand Down Expand Up @@ -37,7 +37,7 @@ parameters:
count: 11

# TODO these rules are generated, this ignores should be fixed in the code
# for src-schema/PhpunitTestCase.php
# for src/Schema/TestCase.php
- '~^Access to an undefined property Atk4\\Data\\Persistence::\$connection\.$~'
- '~^Call to an undefined method Atk4\\Data\\Persistence::dsql\(\)\.$~'
# for src/FieldSqlExpression.php
Expand Down
2 changes: 0 additions & 2 deletions phpunit-mariadb.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<testsuites>
<testsuite name="tests">
<directory>tests</directory>
<directory>tests-schema</directory>
</testsuite>
</testsuites>
<listeners>
Expand All @@ -17,7 +16,6 @@
<coverage>
<include>
<directory suffix=".php">src</directory>
<directory suffix=".php">src-schema</directory>
</include>
<report>
<php outputFile="build/logs/clover-mariadb.cov" />
Expand Down
2 changes: 0 additions & 2 deletions phpunit-mssql.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<testsuites>
<testsuite name="tests">
<directory>tests</directory>
<directory>tests-schema</directory>
</testsuite>
</testsuites>
<listeners>
Expand All @@ -17,7 +16,6 @@
<coverage>
<include>
<directory suffix=".php">src</directory>
<directory suffix=".php">src-schema</directory>
</include>
<report>
<php outputFile="build/logs/clover-mssql.cov" />
Expand Down
2 changes: 0 additions & 2 deletions phpunit-mysql.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<testsuites>
<testsuite name="tests">
<directory>tests</directory>
<directory>tests-schema</directory>
</testsuite>
</testsuites>
<listeners>
Expand All @@ -17,7 +16,6 @@
<coverage>
<include>
<directory suffix=".php">src</directory>
<directory suffix=".php">src-schema</directory>
</include>
<report>
<php outputFile="build/logs/clover-mysql.cov" />
Expand Down
2 changes: 0 additions & 2 deletions phpunit-oracle.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<testsuites>
<testsuite name="tests">
<directory>tests</directory>
<directory>tests-schema</directory>
</testsuite>
</testsuites>
<listeners>
Expand All @@ -17,7 +16,6 @@
<coverage>
<include>
<directory suffix=".php">src</directory>
<directory suffix=".php">src-schema</directory>
</include>
<report>
<php outputFile="build/logs/clover-oracle.cov" />
Expand Down
2 changes: 0 additions & 2 deletions phpunit-pgsql.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<testsuites>
<testsuite name="tests">
<directory>tests</directory>
<directory>tests-schema</directory>
</testsuite>
</testsuites>
<listeners>
Expand All @@ -17,7 +16,6 @@
<coverage>
<include>
<directory suffix=".php">src</directory>
<directory suffix=".php">src-schema</directory>
</include>
<report>
<php outputFile="build/logs/clover-pgsql.cov" />
Expand Down
2 changes: 0 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<testsuites>
<testsuite name="tests">
<directory>tests</directory>
<directory>tests-schema</directory>
</testsuite>
</testsuites>
<listeners>
Expand All @@ -17,7 +16,6 @@
<coverage>
<include>
<directory suffix=".php">src</directory>
<directory suffix=".php">src-schema</directory>
</include>
<report>
<php outputFile="build/logs/clover.cov" />
Expand Down
Loading