Skip to content

Commit

Permalink
cs fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Aug 18, 2022
1 parent 5c2a309 commit 4b48362
Show file tree
Hide file tree
Showing 15 changed files with 343 additions and 256 deletions.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,10 @@ Agile Data was designed in a way where all of your code can rely ONLY on model o
This next example builds a complex "Job Profitability Report" by only relying on Model logic:

``` php
class JobReport extends Job {
function init(): void {
class JobReport extends Job
{
protected function init(): void
{
parent::init();

// Invoice contains Lines that may relevant to this job
Expand Down Expand Up @@ -359,10 +361,12 @@ If you have enjoyed those examples and would like to try them yourself, continue
Agile Data uses vendor-independent and lightweight `Model` class to describe your business entities:

``` php
class Client extends \Atk4\Data\Model {
class Client extends \Atk4\Data\Model
{
public $table = 'client';

function init(): void {
protected function init(): void
{
parent::init();

$this->addField('name');
Expand Down Expand Up @@ -580,7 +584,8 @@ namespace my;
class User extends \Atk4\Data\Model
{
public $table = 'user';
function init(): void

protected function init(): void
{
parent::init();

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
},
"require-release": {
"php": ">=7.4 <8.3",
"atk4/core": "^4.0",
"atk4/core": "~4.0.0",
"doctrine/dbal": "~3.3.0",
"mvorisek/atk4-hintable": "~1.9.0"
},
Expand Down
60 changes: 38 additions & 22 deletions docs/advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ multiple transaction types. Some of those types would even require additional
fields. The pattern suggest you should add a new table "transaction_transfer" and
store extra fields there. In your code::

class Transaction_Transfer extends Transaction {
function init(): void {
class Transaction_Transfer extends Transaction
{
protected function init(): void {
parent::init();
$j = $this->join('transaction_transfer.transaction_id');
$j->addField('destination_account');
Expand Down Expand Up @@ -94,12 +95,13 @@ of the record. Finally to help with performance, you can implement a switch::

...

function init(): void {
..
protected function init(): void
{
...

if ($this->typeSubstitution) {
$this->onHook(Model::HOOK_AFTER_LOAD,
..........
...
)
}
}
Expand Down Expand Up @@ -131,7 +133,8 @@ I will be looking to create the following fields:

To implement the above, I'll create a new class::

class ControllerAudit {
class ControllerAudit
{
use \Atk4\Core\InitializerTrait {
init as private _init;
}
Expand All @@ -145,7 +148,8 @@ $owner, and $app values (due to AppScopeTrait) as well as execute init() method,
which I want to define like this::


protected function init(): void {
protected function init(): void
{
$this->_init();

if (isset($this->getOwner()->no_audit)) {
Expand Down Expand Up @@ -210,13 +214,15 @@ soft-delete controller for Agile Data (for educational purposes).

Start by creating a class::

class ControllerSoftDelete {
class ControllerSoftDelete
{
use \Atk4\Core\InitializerTrait {
init as private _init;
}
use \Atk4\Core\TrackableTrait;

protected function init(): void {
protected function init(): void
{
$this->_init();

if (property_exists($this->getOwner(), 'no_soft_delete')) {
Expand All @@ -234,7 +240,8 @@ Start by creating a class::
}
}

public function softDelete(Model $entity) {
public function softDelete(Model $entity)
{
$entity->assertIsLoaded();

$id = $entity->getId();
Expand All @@ -249,7 +256,8 @@ Start by creating a class::
return $entity;
}

public function restore(Model $entity) {
public function restore(Model $entity)
{
$entity->assertIsLoaded();

$id = $entity->getId();
Expand Down Expand Up @@ -310,14 +318,17 @@ In case you want $m->delete() to perform soft-delete for you - this can also be
achieved through a pretty simple controller. In fact I'm reusing the one from
before and just slightly modifying it::

class ControllerSoftDelete2 extends ControllerSoftDelete {
protected function init(): void {
class ControllerSoftDelete2 extends ControllerSoftDelete
{
protected function init(): void
{
parent::init();

$this->getOwner()->onHook(Model::HOOK_BEFORE_DELETE, \Closure::fromCallable([$this, 'softDelete']), null, 100);
}

public function softDelete(Model $m) {
public function softDelete(Model $m)
{
parent::softDelete();

$m->hook(Model::HOOK_AFTER_DELETE);
Expand Down Expand Up @@ -351,15 +362,17 @@ to another user?
With Agile Data you can create controller that will ensure that certain fields
inside your model are unique::

class ControllerUniqueFields {
class ControllerUniqueFields
{
use \Atk4\Core\InitializerTrait {
init as private _init;
}
use \Atk4\Core\TrackableTrait;

protected $fields = null;

function init(): void {
protected function init(): void
{
$this->_init();

// by default make 'name' unique
Expand All @@ -370,7 +383,7 @@ inside your model are unique::
$this->getOwner()->onHook(Model::HOOK_BEFORE_SAVE, \Closure::fromCallable([$this, 'beforeSave']));
}

function beforeSave(Model $m)
protected function beforeSave(Model $m)
{
foreach ($this->fields as $field) {
if ($m->getDirtyRef()[$field]) {
Expand Down Expand Up @@ -434,10 +447,11 @@ Here is what I need to do:

Create new Model::

class Model_InvoicePayment extends \Atk4\Data\Model {
class Model_InvoicePayment extends \Atk4\Data\Model
{
public $table = 'invoice_payment';

function init(): void
protected function init(): void
{
parent::init();
$this->hasOne('invoice_id', 'Model_Invoice');
Expand Down Expand Up @@ -496,7 +510,7 @@ payment towards a most suitable invoice::


// Add to Model_Payment
function autoAllocate()
public function autoAllocate()
{
$client = $this->ref['client_id'];
$invoices = $client->ref('Invoice');
Expand Down Expand Up @@ -546,8 +560,10 @@ adding invoice, I want to make it possible to specify 'Category' through the
name, not only category_id. First, let me illustrate how can I do that with
category_id::

class Model_Invoice extends \Atk4\Data\Model {
function init(): void {
class Model_Invoice extends \Atk4\Data\Model
{
protected function init(): void
{
parent::init();

...
Expand Down
66 changes: 33 additions & 33 deletions docs/conditions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ to set those conditions inside the init() method of your model::

class Model_Girl extends Model_User
{
function init(): void
protected function init(): void
{
parent::init();

Expand Down Expand Up @@ -254,7 +254,7 @@ These classes can be used directly and independently from Model class.
This method provides access to the model scope enabling conditions to be added::

$contact->scope()->addCondition($condition); // adding condition to a model
$contact->scope()->addCondition($condition); // adding condition to a model

.. php:class:: Scope
Expand All @@ -264,53 +264,53 @@ e.g ((Name like 'ABC%' and Country = 'US') or (Name like 'CDE%' and (Country = '

Scope can be created using new Scope() statement from an array or joining Condition objects or condition arguments arrays::

// $condition1 will be used as nested condition
$condition1 = new Condition('name', 'like', 'ABC%');
// $condition1 will be used as nested condition
$condition1 = new Condition('name', 'like', 'ABC%');

// $condition2 will converted to Condtion object and used as nested condition
$condition2 = ['country', 'US'];
// $condition2 will converted to Condtion object and used as nested condition
$condition2 = ['country', 'US'];

// $scope1 is created using AND as junction and $condition1 and $condition2 as nested conditions
$scope1 = Scope::createAnd($condition1, $condition2);
// $scope1 is created using AND as junction and $condition1 and $condition2 as nested conditions
$scope1 = Scope::createAnd($condition1, $condition2);

$condition3 = new Condition('country', 'DE');
$condition4 = ['surname', 'XYZ'];
$condition3 = new Condition('country', 'DE');
$condition4 = ['surname', 'XYZ'];

// $scope2 is created using OR as junction and $condition3 and $condition4 as nested conditions
$scope2 = Scope::createOr($condition3, $condition4);
// $scope2 is created using OR as junction and $condition3 and $condition4 as nested conditions
$scope2 = Scope::createOr($condition3, $condition4);

$condition5 = new Condition('name', 'like', 'CDE%');
$condition5 = new Condition('name', 'like', 'CDE%');

// $scope3 is created using AND as junction and $condition5 and $scope2 as nested conditions
$scope3 = Scope::createAnd($condition5, $scope2);
// $scope3 is created using AND as junction and $condition5 and $scope2 as nested conditions
$scope3 = Scope::createAnd($condition5, $scope2);

// $scope is created using OR as junction and $scope1 and $scope3 as nested conditions
$scope = Scope::createOr($scope1, $scope3);
// $scope is created using OR as junction and $scope1 and $scope3 as nested conditions
$scope = Scope::createOr($scope1, $scope3);


Scope is an independent object not related to any model. Applying scope to model is using the Model::scope()->add($condition) method::

$contact->scope()->add($condition); // adding condition to a model
$contact->scope()->add($conditionXYZ); // adding more conditions
$contact->scope()->add($condition); // adding condition to a model
$contact->scope()->add($conditionXYZ); // adding more conditions

.. php:method:: __construct($nestedConditions = [], $junction = Scope::AND);
Creates a Scope object from an array::

// below will create 2 conditions and nest them in a compound conditions with AND junction
$scope1 = new Scope([
['name', 'like', 'ABC%'],
['country', 'US'],
]);
// below will create 2 conditions and nest them in a compound conditions with AND junction
$scope1 = new Scope([
['name', 'like', 'ABC%'],
['country', 'US'],
]);

.. php:method:: negate();
Negate method has behind the full map of conditions so any condition object can be negated, e.g negating '>=' results in '<', etc.
For compound conditionss this method is using De Morgan's laws, e.g::

// using $scope1 defined above
// results in "(Name not like 'ABC%') or (Country does not equal 'US')"
$scope1->negate();
// using $scope1 defined above
// results in "(Name not like 'ABC%') or (Country does not equal 'US')"
$scope1->negate();

.. php:method:: createAnd(...$conditions);
Expand Down Expand Up @@ -357,15 +357,15 @@ If $value is omitted as argument then $operator is considered as $value and '='
Negates the condition, e.g::

// results in "name != 'John'"
$condition = (new Condition('name', 'John'))->negate();
// results in "name != 'John'"
$condition = (new Condition('name', 'John'))->negate();

.. php:method:: toWords(Model $model = null);
Converts the condition object to human readable words. Condition must be assigned to a model or model argument provided::

// results in 'Contact where Name is John'
(new Condition('name', 'John'))->toWords($contactModel);
// results in 'Contact where Name is John'
(new Condition('name', 'John'))->toWords($contactModel);

Conditions on Referenced Models
-------------------------------
Expand All @@ -375,15 +375,15 @@ Agile Data allows for adding conditions on related models for retrieval of type
Setting conditions on references can be done utilizing the Model::refLink method but there is a shorthand format
directly integrated with addCondition method using "/" to chain the reference names::

$contact->addCondition('company/country', 'US');
$contact->addCondition('company/country', 'US');

This will limit the $contact model to those whose company is in US.
'company' is the name of the reference in $contact model and 'country' is a field in the referenced model.

If a condition must be set directly on the existence or number of referenced records the special symbol "#" can be
utilized to indicate the condition is on the number of records::

$contact->addCondition('company/tickets/#', '>', 3);
$contact->addCondition('company/tickets/#', '>', 3);

This will limit the $contact model to those whose company have more than 3 tickets.
'company' and 'tickets' are the name of the chained references ('company' is a reference in the $contact model and
Expand Down
Loading

0 comments on commit 4b48362

Please sign in to comment.