HEX
Server: LiteSpeed
System: Linux premium260.web-hosting.com 4.18.0-553.45.1.lve.el8.x86_64 #1 SMP Wed Mar 26 12:08:09 UTC 2025 x86_64
User: aliazzsr (627)
PHP: 7.4.33
Disabled: NONE
Upload Files
File: /home/aliazzsr/api.crm.vqode.com/components/filtering/OperatorCondition.php
<?php
namespace app\components\filtering;

use app\components\BaseModel;
use app\components\formatters\DateFormatter;
use yii\helpers\ArrayHelper;

class OperatorCondition extends BaseCondition
{
    const OPERATOR = 'operator';
    const OPERATOR_SHORT = 'o';

    const VALUE = 'value';
    const VALUE_SHORT = 'v';

    protected static $operators = [
        'BETWEEN',
        '<',
        '>',
        '=',
        '>=',
        '<=',
        '<>',
        'LIKE',
        'NOT LIKE',
        'IN',
        'NOT IN',
    ];

    protected static function getOperator($value)
    {
        $opLong = ArrayHelper::getValue($value, self::OPERATOR);
        $opShort = ArrayHelper::getValue($value, self::OPERATOR_SHORT);

        return strtoupper($opLong ?: $opShort);
    }

    protected static function getValue($value)
    {
        $valueLong = ArrayHelper::getValue($value, self::VALUE);
        $valueShort = ArrayHelper::getValue($value, self::VALUE_SHORT);

        return $valueLong ?: $valueShort;
    }

    /**
     * @inheritdoc
     */
    public static function get($attribute, $value, $modelClass)
    {
        $condition = [];

        $operator = static::getOperator($value);
        $value = static::getValue($value);

        if (static::isValidOperator($operator)) {
            /** @var BaseModel $modelClass */
            $attributes = $modelClass::getTableSchema()->columns;
            $dbAttr = ArrayHelper::getValue($attributes, $attribute);

            switch (isset($dbAttr->type) ? $dbAttr->type : null) {
                case 'timestamp':
                    if ('BETWEEN' === $operator) {
                        list($d0, $d1) = (array)$value;
                        $condition = [$operator, static::getDateAttribute($dbAttr->name), DateFormatter::toMysql($d0), DateFormatter::toMysql($d1)];
                    } else {
                        $condition = [$operator, static::getDateAttribute($dbAttr->name), DateFormatter::toMysql($value)];
                    }
                    break;
                default:
                    $condition = [$operator, $attribute, $value];
                    if ('BETWEEN' === $operator) {
                        list($d0, $d1) = (array)$value;
                        $condition = [$operator, $attribute, $d0, $d1];
                    } elseif (is_array($value) && 'IN' !== $operator) {
                        $conditions = [];
                        foreach ($value as $v) {
                            $conditions[] = [$operator, $attribute, $v];
                        }

                        $condition = array_merge(['OR'], $conditions);
                    }
            }
        }

        return $condition;
    }

    /**
     * Checks if operator is valid
     * @param string $operator
     * @return bool if operator is valid
     */
    protected static function isValidOperator($operator)
    {
        return in_array(strtoupper($operator), self::$operators);
    }

    /**
     * @inheritdoc
     */
    public static function is($attribute, $value)
    {
        $containsOperator = null !== static::getOperator($value);
        $containsValue = null !== static::getValue($value);

        return $containsOperator && $containsValue;
    }
}