Преобразование условия поиска из массива или строки в url формат
@param string|array $cond Исходное условие поиска
@return string Преобразованное в URL условие
434921
static function condToUrl($cond)
    {
        $cond = self::normalizeCond($cond, [], true);
        if (is_array($cond['from'])){
            $info = parse_url(reset($cond['from']));
            $base_url = '';
            if (isset($info['scheme'])) $base_url.= $info['scheme'].'://';
            if (isset($info['host'])) $base_url.= $info['host'];
            if ($base_url_length = mb_strlen($base_url)){
                foreach ($cond['from'] as $i => $from){
                    if (mb_substr($from,0,$base_url_length) == $base_url) $cond['from'][$i] = mb_substr($from, $base_url_length);
                }
            }
        }
        //if (sizeof($cond['select']) == 1) $cond['select'] = $cond['select'][0];
        if ($cond['select'] == 'self'){
            unset($cond['select'], $cond['depth']);
        }
        unset($cond['correct']);
        foreach ($cond as $key => $c){
            if (empty($c)) unset($cond[$key]);
        }
        $url = F::toJSON($cond, false);
        $url = mb_substr($url, 1, mb_strlen($url)-2, 'UTF-8');
        $url = strtr($url, [
                         '[' => '(',
                         ']' => ')',
                         ',""]' => ',)',
                         '"="' => '"eq"',
                         '"!="' => '"ne"',
                         '">"' => '"gt"',
                         '">="' => '"gte"',
                         '"<"' => '"lt"',
                         '"<="' => '"lte"'
                    ]);
        $url = preg_replace_callback('/"([^"]*)"/ui', function($m){
                        $replacements = array("\\", "/", "\"", "\n", "\r", "\t", "\x08", "\x0c");
                        $escapers = array("\\\\", "\\/", "\\\"", "\\n", "\\r", "\\t", "\\f", "\\b");
                        return urlencode(str_replace($escapers, $replacements, $m[1]));
                    }, $url);
        $url = preg_replace('/,([a-z_]+):/ui','&$1=',$url);
        $url = preg_replace('/\(([a-z_]+),/ui','$1(',$url);
        $url = preg_replace('/\),/ui',')$1',$url);
        $url = mb_substr($url, 5, mb_strlen($url)-5);
        if (isset($base_url)){
            $url = $base_url.'?from='.$url;
        }else{
            $info = explode('&', $url, 2);
            if (!empty($info)){
                $url = urldecode($info[0]).'?'.$info[1];
            }
        }
        return $url;
    }
964496
public function tryLoadBy($field, $cond = UNDEFINED, $value = UNDEFINED)
    {
        $q = $this->dsql;
        $this->dsql = $this->dsql();
        $this->addCondition($field, $cond, $value);
        $this->tryLoadAny();
        $this->dsql = $q;

        return $this;
    }
184343
static function unionCond($cond1, $cond2, $or = false)
    {
        foreach ($cond2 as $key => $param){
            if (!isset($cond1[$key])){
                $cond1[$key] = $param;
            }else
            if ($key == 'order'){
                $cond1[$key] = array_merge($cond1[$key], $param);
            }else
            if ($key == 'where'){
                $cond1[$key] = self::unionCondWhere($cond1[$key], $param, $or);
            }
        }
        return $cond1;
    }
133206
protected function parseCondition(string $condition): string
    {
        $conditions = $this->getConditions();

        foreach ($conditions as $signature => $conds) {
            $conds = is_array($conds) ? $conds : [$conds];

            if (in_array($condition, $conds, true)) {
                return $signature;
            }
        }

        return '';
    }
232346
public function getContrast()
    {
        if (!preg_match('/^-*[0-9]+$/', $this->con)) {
            return;
        }

        if ($this->con < -100 or $this->con > 100) {
            return;
        }

        return (int) $this->con;
    }
820021
private function buildHostname(array $config): string
    {
        if (isset($config['cluster'])) {
            foreach ($config['cluster']['nodes'] as $node) {
                $nodes[] = sprintf(
                    '%s:%s',
                    $node['host'] ?? '127.0.0.1',
                    $node['port'] ?? 27017
                );
            }

            return implode(',', $nodes ?? ['127.0.0.1:27017']);
        }

        return sprintf(
            '%s:%s',
            $config['host'] ?? '127.0.0.1',
            $config['port'] ?? 27017
        );
    }
1366961
public function searchIds(array $cond, array $options = array()): SearchResult
    {
        $params = $options + array('cond' => $cond);

        $params['ids_only'] = true;

        $result = $this->getEntityDao()->search($params);

        if (!$result instanceof SearchResult) {
            throw new FindException('Data Access Object did not return a search result');
        }

        return $result;
    }
932521
public function count($table, $cond=null, $fields='*', $distinct=false)
    {
    	$ds = $this->ds();
    	if ($distinct) $distinct = 'DISTINCT ';
    	
    	$cond = SqlHelper::parse_cond($ds,$cond);
    	if ($cond) $cond = "WHERE {$cond}";
		
    	if (is_null($fields) || trim($fields) == '*') {
            $fields = '*';
        } 
        else {
            $fields = SqlHelper::qfields($fields,$table);
        }
        
        $table = SqlHelper::qtable($table);
        
        $result = (int) Sql::read($ds, Sql::MODE_READ_GETONE, [
				"SELECT COUNT({$distinct}{$fields}) FROM {$table} {$cond}"
			]);
		return $result;
    }
390772
public function updateOne($cond, $data, $cb = null, $params = null)
    {
        $this->pool->updateOne($this->name, $cond, $data, $cb, $params);
    }
1358321
protected function parseDescriptorField($config)
    {
        if (is_array($config)) {
            $descriptor = [];
            foreach ($config as $field) {
                if (!empty($this->{$field})) {
                    $descriptor[] = $this->{$field};
                }
            }

            return implode(' ', $descriptor);
        } else {
            return $this->{$config};
        }
    }
831310
public function where($cond)
    {
        $this->wheres[] = $this->whereProcess(self::OPERATION_AND, func_get_args());
        return $this;
    }
334441
public static function concat()
    {
        $args = func_get_arg();
        $string = '';

        foreach ($args as $arg) {
            $string .= $arg;
        }

        return $string;
    }
1343635
public function orderBy($cond) {
		$cols=\Base::instance()->split($cond);
		$this->uasort(function($val1,$val2) use($cols) {
			foreach ($cols as $col) {
				$parts=explode(' ',$col,2);
				$order=empty($parts[1])?'ASC':$parts[1];
				$col=$parts[0];
				list($v1,$v2)=array($val1[$col],$val2[$col]);
				if ($out=strnatcmp($v1,$v2)*
					((strtoupper($order)=='ASC')*2-1))
					return $out;
			}
			return 0;
		});
	}
570317
public function select_row($table, $cond=null, $fields='*', $sort=null)
    {
    	return Sql::assistant( $this->ds )->select_row($table, $cond, $fields,  $sort);
    }
228864
public function sql_prependTableToFields($cond, $table) {
		$out = preg_replace_callback('/'.
			'(\w+\((?:[^)(]+|\((?:[^)(]+|(?R))*\))*\))|'.
			'(?:(\s)|^|(?<=[(]))'.
			'([a-zA-Z_](?:[\w\-_]+))'.
			'(?=[\s<>=!)]|$)/i',
			function($match) use($table) {
				if (!isset($match[3]))
					return $match[1];
				if (preg_match('/\b(AND|OR|IN|LIKE|NOT)\b/i',$match[3]))
					return $match[0];
				return $match[2].$table.'.'.$match[3];
			}, $cond);
		return $out ?: $cond;
	}
928665
public function inlineCond($cond, $style, array $attr = null)
    {
        $style = $this->inline($style, $attr);
        $cond  = $this->escaper->html($cond);
        return "<!--[if {$cond}]>{$style}<![endif]-->";
    }
1127474
public function transform(array $config): string
    {
        $cfg = '';
        foreach ($config as $nodeName => $nodeData) {
            $nodeName = \is_numeric($nodeName) ? 'num__'.$nodeName : $nodeName;

            if (\is_array($nodeData)) {
                $cfg .= \sprintf('%s[%s]%s', \chr($this->newline), $nodeName, \chr($this->newline));
                $cfg .= $this->addLine($nodeName, $nodeData, 0);
            } elseif (\is_bool($nodeData)) {
                $nodeData = $nodeData === true ? 'true' : 'false';
                $cfg .= \sprintf('%s = %s%s', $nodeName, $nodeData, \chr($this->newline));
            } else {
                $nodeData = $this->normalize((string)$nodeData);
                $cfg .= \sprintf('%s = %s%s', $nodeName, $nodeData, \chr($this->newline));
            }
        }
        return $cfg;
    }