Skip to content

Commit

Permalink
QUASAR-366 Added support for ES6
Browse files Browse the repository at this point in the history
  • Loading branch information
pyatachok committed Dec 2, 2019
1 parent 2d730c1 commit d2cb4d0
Show file tree
Hide file tree
Showing 38 changed files with 1,576 additions and 10 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Changelog
All notable changes to this project will be documented in this file.

## [Unreleased]
### Added
- support for mongodb 3.6

## [0.0.1] - 2019-12-01
### Added
- support for elasticsearch6
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
Base Library for condition builders
====================

[![Latest Version](https://img.shields.io/github/tag/mice-tm/yii2-condition-builder.svg?style=flat-square&label=release)](https://github.com/mice-tm/yii2-condition-builder/releases)
[![Latest Version](https://img.shields.io/github/tag/mice-tm/conditions-base.svg?style=flat-square&label=release)](https://github.com/mice-tm/conditions-base/releases)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Total Downloads](https://img.shields.io/packagist/dt/mice-tm/yii2-condition-builder.svg?style=flat-square)](https://packagist.org/packages/mice-tm/yii2-condition-builder)
[![Total Downloads](https://img.shields.io/packagist/dt/mice-tm/conditions-base.svg?style=flat-square)](https://packagist.org/packages/mice-tm/conditions-base)

This library represents flexible mechanism for building logical conditions for furser elasticsearch search query generation by this conditions.

Supports ES6.


Usage
-----


Tests
-----

```bash
./unit.sh
```

[CHANGELOG]: ./CHANGELOG.md
[version-badge]: https://img.shields.io/badge/version-0.0.1-blue.svg

18 changes: 18 additions & 0 deletions codeception.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
paths:
tests: tests
output: tests/_output
data: tests/_data
support: tests/_support
envs: tests/_envs
actor_suffix: Tester
settings:
bootstrap: ../_bootstrap.php
extensions:
enabled:
- Codeception\Extension\RunFailed
coverage:
enabled: true
remote: false
whitelist:
include:
- src/services/*
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Base library for conditons builder",
"keywords": ["conditions", "builder"],
"license": "MIT",
"minimum-stability": "stabel",
"minimum-stability": "stable",
"authors": [
{
"name": "Iryna Mychkova",
Expand All @@ -13,7 +13,8 @@
],
"autoload": {
"psr-4": {
"micetm\\conditionsBase\\": "src"
"micetm\\conditionsBase\\": "src",
"micetm\\conditionsBase\\tests\\": "tests/"
}
},
"repositories": [
Expand All @@ -24,6 +25,8 @@
],
"require": {
"php": ">=7.1.0",
"yiisoft/yii2": "2.0.15.1",
"yii2tech/embedded": "~1.0.3",
"oomphinc/composer-installers-extender": "^1.1.2"
},
"config": {
Expand Down
18 changes: 18 additions & 0 deletions src/exceptions/AttributeNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace micetm\conditionsBase\exceptions;

class AttributeNotFoundException extends \Exception
{
private $attribute;

public function __construct($attribute, $message = "", $code = 0, Throwable $previous = null)
{
$this->attribute = $attribute;
parent::__construct($message, $code, $previous);
}

public function getAttribute()
{
return $this->attribute;
}
}
8 changes: 3 additions & 5 deletions src/models/ComparisonInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

namespace micetm\conditionsBase\models;

use micetm\conditions\models\constructor\conditions\Condition;

interface ComparisonInterface
{
public function buildFilter(Condition $condition): array;
public function buildFilter(ConditionInterface $condition): array;

public static function isMaster(Condition $condition): bool;
}
public static function isMaster(ConditionInterface $condition): bool;
}
80 changes: 80 additions & 0 deletions src/models/conditions/Condition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace micetm\conditionsBase\models\conditions;

use micetm\conditionsBase\models\ConditionInterface;
use yii\base\Model;
use yii2tech\embedded\ContainerInterface;
use yii2tech\embedded\ContainerTrait;

/**
* Class Condition
* @package micetm\conditionsBase\models\conditions
*/
class Condition extends Model implements ContainerInterface, ConditionInterface
{
use ContainerTrait;

/** @var string */
public $operator;

/** @var string */
public $attribute;

/** @var string */
public $comparison;

/** @var mixed */
public $value;

public $conditions = [];

public static $operators = [
self::OPERATOR_AND => self::OPERATOR_AND,
self::OPERATOR_OR => self::OPERATOR_OR,
self::OPERATOR_NOT => self::OPERATOR_NOT,
self::OPERATOR_STATEMENT => self::OPERATOR_STATEMENT
];


public function embedConditionModels()
{
return $this->mapEmbeddedList('conditions', Condition::class, ['unsetSource' => false]);
}

public function rules()
{
return [
[['operator', 'attribute', 'comparison'], 'string'],
['operator', 'in', 'range' => self::$operators],
// [
// ['operator'],
// 'required',
// 'on' => self::SCENARIO_DEFAULT
// ],
['conditionModels', 'yii2tech\embedded\Validator', 'message' => 'Conditions are invalid.'],
];
}

/**
* @inheritdoc
*/
public function scenarios()
{
return [
self::SCENARIO_DEFAULT => [
'operator',
'attribute',
'comparison',
'conditionModels',
'value',
],
] + parent::scenarios();
}

public function isUnary():bool
{
return !empty($this->attribute) && !empty($this->comparison);
}

}
2 changes: 1 addition & 1 deletion src/services/BuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ interface BuilderInterface
* @return array
* @throws WrongComparison
*/
public static function create($conditions):array;
public function create($conditions):array;

}
104 changes: 104 additions & 0 deletions src/services/ComparisonHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace micetm\conditionsBase\services;

use micetm\conditionsBase\models\AttributeInterface;
use micetm\conditionsBase\models\ConditionInterface;


class ComparisonHelper
{
const COMPARISONS_ASSERTS = [
AttributeInterface::EQUAL_TO_COMPARISON => 'eq',
AttributeInterface::GREATER_THAN_COMPARISON => 'gt',
AttributeInterface::GREATER_THAN_OR_EQUAL_TO_COMPARISON => 'gte',
AttributeInterface::LESS_THAN_COMPARISON => 'lt',
AttributeInterface::LESS_THAN_OR_EQUAL_TO_COMPARISON => 'lte',
AttributeInterface::LIKE_COMPARISON => 'like',
AttributeInterface::MORE_THAN_ONE_IN_COMPARISON => 'in',
];

/**
* @param Condition $condition
* @param $value
* @return bool
*/
public static function compare(ConditionInterface $condition, $value)
{
if (empty(self::COMPARISONS_ASSERTS[$condition->comparison])) {
return self::in($condition->value, $value);
}

return self::{self::COMPARISONS_ASSERTS[$condition->comparison]}($condition->value, $value);
}

/**
* @param mixed $a
* @param mixed $b
* @return bool
*/
public static function in($a, $b)
{
return !empty(array_intersect((array)$a, (array)$b));
}

/**
* @param mixed $a
* @param mixed $b
* @return bool
*/
public static function eq($a, $b)
{
return $a == $b;
}

/**
* @param mixed $a
* @param mixed $b
* @return bool
*/
public static function gt($a, $b)
{
return $a > $b;
}

/**
* @param mixed $a
* @param mixed $b
* @return bool
*/
public static function gte($a, $b)
{
return $a >= $b;
}

/**
* @param mixed $a
* @param mixed $b
* @return bool
*/
public static function lt($a, $b)
{
return $a < $b;
}

/**
* @param mixed $a
* @param mixed $b
* @return bool
*/
public static function lte($a, $b)
{
return $a <= $b;
}

/**
* @param mixed $a
* @param mixed $b
* @return bool
*/
public static function like($a, $b)
{
return false !== strpos($b, $a);
}
}
16 changes: 16 additions & 0 deletions src/services/ComparisonManagerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
namespace micetm\conditionsBase\services;

use micetm\conditionsBase\exceptions\WrongComparison;
use micetm\conditionsBase\models\ComparisonInterface;
use micetm\conditionsBase\models\ConditionInterface;

interface ComparisonManagerInterface
{
/**
* @param Condition $condition
* @return ComparisonInterface
* @throws WrongComparison
*/
public function getComparison(ConditionInterface $condition): ComparisonInterface;
}
15 changes: 15 additions & 0 deletions src/services/OracleInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace micetm\conditionsBase\services;

use micetm\conditionsBase\models\conditions\Condition;

interface OracleInterface
{
/**
* @param Condition $condition Consists of attribute name, its value and a comparison type
* @param array|object $target Where to find coresponding attribute value for comparison
* @return bool
*/
public function speak(Condition $condition, $target): bool;
}

0 comments on commit d2cb4d0

Please sign in to comment.