Base.php 1.29 KB
Newer Older
Mark committed
1
<?php
2 3 4 5 6
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */
Mark committed
7 8 9 10 11

namespace yii\debug\models\search;

use yii\base\Model;
use yii\debug\components\search\Filter;
12 13 14 15 16 17 18 19
use yii\debug\components\search\matchers;

/**
 * Base search model
 *
 * @author Mark Jebri <mark.github@yandex.ru>
 * @since 2.0
 */
Mark committed
20 21
class Base extends Model
{
22 23 24
    /**
     * Adds filtering condition for a given attribute
     *
25 26 27
     * @param Filter $filter filter instance
     * @param string $attribute attribute to filter
     * @param boolean $partial if partial match should be used
28 29 30 31
     */
    public function addCondition(Filter $filter, $attribute, $partial = false)
    {
        $value = $this->$attribute;
Mark committed
32

33 34 35
        if (mb_strpos($value, '>') !== false) {
            $value = intval(str_replace('>', '', $value));
            $filter->addMatcher($attribute, new matchers\GreaterThan(['value' => $value]));
Mark committed
36

37 38 39 40 41 42 43
        } elseif (mb_strpos($value, '<') !== false) {
            $value = intval(str_replace('<', '', $value));
            $filter->addMatcher($attribute, new matchers\LowerThan(['value' => $value]));
        } else {
            $filter->addMatcher($attribute, new matchers\SameAs(['value' => $value, 'partial' => $partial]));
        }
    }
Mark committed
44
}