studxxx/yii2-condition-builder-client

Yii2 Client condition builder

0.0.6 2018-05-25 11:39 UTC

This package is not auto-updated.

Last update: 2025-04-19 22:08:51 UTC


README

version Latest Version License: MIT Total Downloads

It's a specific library which, based on constructor conditions and data to compare, performs data filtering according to given conditions

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist studxxx/yii2-condition-builder-client "*"

or add

"studxxx/yii2-condition-builder-client": "*"

to the require section of your composer.json file.

Setup

Into config file

<?php
return [
    // ...
    'container' => [
        'definitions' => [
            'studxxx\conditionclient\ConditionBuilderInterface' =>
                'studxxx\conditionclient\ConditionBuilder',
        ]
    ]
];

Condiction format

<php
return [
    "conditions" => [
        [
            "operator" => "AND",
            "conditions" => [
                [
                    "operator" => "NOT",
                    "conditions" => [
                        [
                            "attribute" => "items.price_variant",
                            "comparison" => "in",
                            "value" => [
                                "exclusive",
                                "buyout"
                            ],
                        ]
                    ]
                ],
                [
                    "attribute" => "items.namespace",
                    "comparison" => "=",
                    "value" => "products",
                ],
                [
                    "attribute" => "project",
                    "comparison" => "=",
                    "value" => "TM",
                }
            ]
        ]
    ],
];
<?php

namespace api\modules\api\v2\services;

use studxxx\conditionclient\ConditionBuilderInterface;

class DiscountService implements DiscountServiceInterface
{
    /** @var ConditionBuilderInterface */
    private $conditionBuilder;

    public function __construct(ConditionBuilderInterface $conditionBuilder)
    {
        $this->conditionBuilder = $conditionBuilder;
    }

    public function applicability(ConditionQuery $query, string $code, array $params = []): array
    {
        /** @var array $condition */
        $condition = $this->getCondition($query, $code);

        $this->conditionBuilder->setData($params);
        return $this->conditionBuilder->buildConditions($condition)->getData();
    }
}