Base.php 1.18 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 22
class Base extends Model
{
	/**
23 24 25 26 27
	 * Adds filtering condition for a given attribute
	 *
	 * @param Filter $filter filter instance
	 * @param string $attribute attribute to filter
	 * @param boolean $partial if partial match should be used
Mark committed
28
	 */
29
	public function addCondition(Filter $filter, $attribute, $partial = false)
Mark committed
30 31 32 33 34
	{
		$value = $this->$attribute;

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

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