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 BasicCondition to Condition #663

Merged
merged 1 commit into from
Jul 24, 2020
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
26 changes: 13 additions & 13 deletions docs/conditions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ Model Scope
-----------

Using the Model::addCondition method is the basic way to limit the model scope of records. Under the hood
Agile Data utilizes a special set of classes (BasicCondition and CompoundCondition) to apply the conditions as filters on records retrieved.
Agile Data utilizes a special set of classes (Condition and CompoundCondition) to apply the conditions as filters on records retrieved.
These classes can be used directly and independently from Model class.

.. php:method:: scope()
Expand All @@ -270,9 +270,9 @@ This method provides access to the model scope enabling conditions to be added::

.. php:namespace:: atk4\data\Model\Scope

.. php:class:: BasicCondition
.. php:class:: Condition

BasicCondition represents a simple condition in a form [field, operation, value], similar to the functionality of the
Condition represents a simple condition in a form [field, operation, value], similar to the functionality of the
Model::addCondition method

.. php:method:: __construct($key, $operator = null, $value = null);
Expand All @@ -290,11 +290,11 @@ If $value is omitted as argument then $operator is considered as $value and '='
Negates the condition, e.g::

// results in 'name is not John'
$condition = (new BasicCondition('name', 'John'))->negate();
$condition = (new Condition('name', 'John'))->negate();

.. php:method:: on(Model $model);

Sets the model of BasicCondition to a clone of $model to avoid changes to the original object.::
Sets the model of Condition to a clone of $model to avoid changes to the original object.::

// uses the $contact model to conver the condition to human readable words
$condition->toWords($contact);
Expand All @@ -304,32 +304,32 @@ Sets the model of BasicCondition to a clone of $model to avoid changes to the or
Converts the condition object to human readable words. Model must be set first::

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

.. php:class:: CompoundCondition

CompoundCondition object has a single defined junction (AND or OR) and can contain multiple nested BasicCondition and/or CompoundCondition objects referred to as nested conditions.
CompoundCondition object has a single defined junction (AND or OR) and can contain multiple nested Condition and/or CompoundCondition objects referred to as nested conditions.
This makes creating Model scopes with deep nested conditions possible,
e.g ((Name like 'ABC%' and Country = 'US') or (Name like 'CDE%' and (Country = 'DE' or Surname = 'XYZ')))

CompoundCondition can be created using new CompoundCondition() statement from an array or joining BasicCondition objects::
CompoundCondition can be created using new CompoundCondition() statement from an array or joining Condition objects::

// $condition1 will be used as child-component
$condition1 = new BasicCondition('name', 'like', 'ABC%');
$condition1 = new Condition('name', 'like', 'ABC%');

// $condition2 will be used as child-component
$condition2 = new BasicCondition('country', 'US');
$condition2 = new Condition('country', 'US');

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

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

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

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

// $compoundCondition3 is created using AND as junction and $condition5 and $compoundCondition2 as nested conditions
$compoundCondition3 = CompoundCondition::createAnd($condition5, $compoundCondition2);
Expand Down
2 changes: 1 addition & 1 deletion src/Action/Iterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ protected function match(array $row, Model\Scope\AbstractCondition $condition)
$match = false;

// simple condition
if ($condition instanceof Model\Scope\BasicCondition) {
if ($condition instanceof Model\Scope\Condition) {
$args = $condition->toQueryArguments();

$field = $args[0];
Expand Down
4 changes: 2 additions & 2 deletions src/Model/Scope/CompoundCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ public function __construct(array $nestedConditions = [], string $junction = sel
$this->junction = $junction;

foreach ($nestedConditions as $nestedCondition) {
$nestedCondition = is_string($nestedCondition) ? new BasicCondition($nestedCondition) : $nestedCondition;
$nestedCondition = is_string($nestedCondition) ? new Condition($nestedCondition) : $nestedCondition;

if (is_array($nestedCondition)) {
// array of OR nested conditions
if (count($nestedCondition) === 1 && isset($nestedCondition[0]) && is_array($nestedCondition[0])) {
$nestedCondition = new self($nestedCondition[0], self::OR);
} else {
$nestedCondition = new BasicCondition(...$nestedCondition);
$nestedCondition = new Condition(...$nestedCondition);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use atk4\dsql\Expression;
use atk4\dsql\Expressionable;

class BasicCondition extends AbstractCondition
class Condition extends AbstractCondition
{
use ReadableCaptionTrait;

Expand Down
2 changes: 1 addition & 1 deletion src/Persistence/Sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ public function initQueryConditions(Model $model, Query $query, Model\Scope\Abst
$condition = $condition->simplify();

// simple condition
if ($condition instanceof Model\Scope\BasicCondition) {
if ($condition instanceof Model\Scope\Condition) {
$query = $query->where(...$condition->toQueryArguments());
}

Expand Down
58 changes: 29 additions & 29 deletions tests/ScopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace atk4\data\tests;

use atk4\data\Model;
use atk4\data\Model\Scope\BasicCondition;
use atk4\data\Model\Scope\CompoundCondition;
use atk4\data\Model\Scope\Condition;
use atk4\dsql\Expression;

class SCountry extends Model
Expand Down Expand Up @@ -123,7 +123,7 @@ public function testCondition()
{
$user = clone $this->user;

$condition = new BasicCondition('name', 'John');
$condition = new Condition('name', 'John');

$user->scope()->add($condition);

Expand All @@ -136,37 +136,37 @@ public function testContitionToWords()
{
$user = clone $this->user;

$condition = new BasicCondition(new Expression('false'));
$condition = new Condition(new Expression('false'));

$this->assertEquals('expression \'false\'', $condition->toWords($user));

$condition = new BasicCondition('country_id/code', 'US');
$condition = new Condition('country_id/code', 'US');

$this->assertEquals('User that has reference Country Id where Code is equal to \'US\'', $condition->toWords($user));

$condition = new BasicCondition('country_id', 2);
$condition = new Condition('country_id', 2);

$this->assertEquals('Country Id is equal to \'Latvia\'', $condition->toWords($user));

if ($this->driverType == 'sqlite') {
$condition = new BasicCondition('name', $user->expr('[surname]'));
$condition = new Condition('name', $user->expr('[surname]'));

$this->assertEquals('Name is equal to expression \'"user"."surname"\'', $condition->toWords($user));
}

$condition = new BasicCondition('country_id', null);
$condition = new Condition('country_id', null);

$this->assertEquals('Country Id is equal to empty', $condition->toWords($user));

$condition = new BasicCondition('name', '>', 'Test');
$condition = new Condition('name', '>', 'Test');

$this->assertEquals('Name is greater than \'Test\'', $condition->toWords($user));

$condition = (new BasicCondition('country_id', 2))->negate();
$condition = (new Condition('country_id', 2))->negate();

$this->assertEquals('Country Id is not equal to \'Latvia\'', $condition->toWords($user));

$condition = new BasicCondition($user->getField('surname'), $user->getField('name'));
$condition = new Condition($user->getField('surname'), $user->getField('name'));

$this->assertEquals('Surname is equal to User Name', $condition->toWords($user));

Expand Down Expand Up @@ -275,11 +275,11 @@ public function testScope()
{
$user = clone $this->user;

$condition1 = new BasicCondition('name', 'John');
$condition2 = new BasicCondition('country_code', 'CA');
$condition1 = new Condition('name', 'John');
$condition2 = new Condition('country_code', 'CA');

$condition3 = new BasicCondition('surname', 'Doe');
$condition4 = new BasicCondition('country_code', 'LV');
$condition3 = new Condition('surname', 'Doe');
$condition4 = new Condition('country_code', 'LV');

$compoundCondition1 = CompoundCondition::createAnd($condition1, $condition2);
$compoundCondition2 = CompoundCondition::createAnd($condition3, $condition4);
Expand Down Expand Up @@ -315,11 +315,11 @@ public function testScopeToWords()
{
$user = clone $this->user;

$condition1 = new BasicCondition('name', 'Alain');
$condition2 = new BasicCondition('country_code', 'CA');
$condition1 = new Condition('name', 'Alain');
$condition2 = new Condition('country_code', 'CA');

$compoundCondition1 = CompoundCondition::createAnd($condition1, $condition2);
$condition3 = (new BasicCondition('surname', 'Prost'))->negate();
$condition3 = (new Condition('surname', 'Prost'))->negate();

$compoundCondition = CompoundCondition::createAnd($compoundCondition1, $condition3);

Expand All @@ -330,8 +330,8 @@ public function testNegate()
{
$user = clone $this->user;

$condition1 = new BasicCondition('name', '!=', 'Alain');
$condition2 = new BasicCondition('country_code', '!=', 'FR');
$condition1 = new Condition('name', '!=', 'Alain');
$condition2 = new Condition('country_code', '!=', 'FR');

$condition = CompoundCondition::createOr($condition1, $condition2)->negate();

Expand All @@ -346,12 +346,12 @@ public function testAnd()
{
$user = clone $this->user;

$condition1 = new BasicCondition('name', 'Alain');
$condition2 = new BasicCondition('country_code', 'FR');
$condition1 = new Condition('name', 'Alain');
$condition2 = new Condition('country_code', 'FR');

$compoundCondition = CompoundCondition::createAnd($condition1, $condition2);

$compoundCondition = CompoundCondition::createOr($compoundCondition, new BasicCondition('name', 'John'));
$compoundCondition = CompoundCondition::createOr($compoundCondition, new Condition('name', 'John'));

$this->assertEquals('(Name is equal to \'Alain\' and Code is equal to \'FR\') or Name is equal to \'John\'', $compoundCondition->toWords($user));
}
Expand All @@ -360,12 +360,12 @@ public function testOr()
{
$user = clone $this->user;

$condition1 = new BasicCondition('name', 'Alain');
$condition2 = new BasicCondition('country_code', 'FR');
$condition1 = new Condition('name', 'Alain');
$condition2 = new Condition('country_code', 'FR');

$compoundCondition = CompoundCondition::createOr($condition1, $condition2);

$compoundCondition = CompoundCondition::createAnd($compoundCondition, new BasicCondition('name', 'John'));
$compoundCondition = CompoundCondition::createAnd($compoundCondition, new Condition('name', 'John'));

$this->assertEquals('(Name is equal to \'Alain\' or Code is equal to \'FR\') and Name is equal to \'John\'', $compoundCondition->toWords($user));
}
Expand All @@ -374,8 +374,8 @@ public function testMerge()
{
$user = clone $this->user;

$condition1 = new BasicCondition('name', 'Alain');
$condition2 = new BasicCondition('country_code', 'FR');
$condition1 = new Condition('name', 'Alain');
$condition2 = new Condition('country_code', 'FR');

$compoundCondition = CompoundCondition::createAnd($condition1, $condition2);

Expand All @@ -386,8 +386,8 @@ public function testDestroyEmpty()
{
$user = clone $this->user;

$condition1 = new BasicCondition('name', 'Alain');
$condition2 = new BasicCondition('country_code', 'FR');
$condition1 = new Condition('name', 'Alain');
$condition2 = new Condition('country_code', 'FR');

$compoundCondition = CompoundCondition::createAnd($condition1, $condition2);

Expand Down