Passed
Branch master (5646b8)
by compolom
04:06 queued 01:36
created

Helper::concat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Compolomus\LSQLQueryBuilder\System\Traits;
4
5
use Compolomus\LSQLQueryBuilder\BuilderException;
6
7
trait Helper
8
{
9
    public function concatWhere(array $conditions, string $separator = 'and'): string
10
    {
11
        if (!\in_array($separator, ['and', 'or'], true)) {
12
            throw new BuilderException('Передан неверный тип |WHERE concate|');
13
        }
14
        return implode(' ' . strtoupper($separator) . ' ', $conditions);
15
    }
16
17 1
    public function concat(array $fields): string
18
    {
19 1
        return implode(',', $fields);
20
    }
21
22
    public function concatOrder(array $order, string $type = 'asc'): string
23
    {
24 View Code Duplication
        if (!\in_array($type, ['asc', 'desc'], true)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
25
            throw new BuilderException('Передан неверный тип |ORDER concate|');
26
        }
27
        $result = '';
28
        if (\count($order) > 0) {
29
            $result = implode(',', $this->escapeField($order)) . ' ' . strtoupper($type);
30
        }
31
        return $result;
32
    }
33
34
    /**
35
     * @param $field
36
     * @return array|string
37
     */
38 5
    public function escapeField($field)
39
    {
40 5
        return \is_array($field) ? array_map([$this, 'escapeField'], $field) : str_replace('`*`', '*',
41 5
            '`' . str_replace('.', '`.`', $field) . '`');
42
    }
43
44
    public function map(string $field, $value): string
45
    {
46
        return $field . ' = ' . $value;
47
    }
48
49
    public function uid(string $prefix): string
50
    {
51
        $prefix = strtoupper($prefix);
52
        $str = uniqid($prefix . '-', true);
53
        return str_replace('.', '', substr($str, 2, 2) . substr($str, 12, -4)) . $prefix;
54
    }
55
}
56