Completed
Push — master ( bf07e5...f61ec4 )
by Beniamin
02:20
created

ReferenceCompiler   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 0
cbo 2
dl 0
loc 35
ccs 13
cts 13
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A compile() 0 8 2
A convertReferenceToValue() 0 12 4
1
<?php
2
3
/**
4
 * This file is part of Phuria SQL Builder package.
5
 *
6
 * Copyright (c) 2016 Beniamin Jonatan Šimko
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Phuria\SQLBuilder\QueryCompiler;
13
14
use Phuria\SQLBuilder\QueryBuilder\BuilderInterface;
15
use Phuria\SQLBuilder\Table\AbstractTable;
16
17
/**
18
 * @author Beniamin Jonatan Šimko <[email protected]>
19
 */
20
class ReferenceCompiler
21
{
22
    /**
23
     * @param string $rawSQL
24
     * @param array  $references
25
     *
26
     * @return string
27
     */
28 34
    public function compile($rawSQL, array $references)
29
    {
30 34
        foreach ($references as &$value) {
31 24
            $value = $this->convertReferenceToValue($value);
32 34
        }
33
34 34
        return str_replace(array_keys($references), array_values($references), $rawSQL);
35
    }
36
37
    /**
38
     * @param $reference
39
     *
40
     * @return string
41
     */
42 24
    private function convertReferenceToValue($reference)
43
    {
44 24
        if (is_string($reference)) {
45 2
            return "\"" . $reference ."\"";
46 23
        } elseif ($reference instanceof AbstractTable) {
47 22
            return $reference->getAliasOrName();
48 2
        } elseif ($reference instanceof BuilderInterface) {
49 1
            return $reference->buildSQL();
50
        }
51
52 1
        return $reference;
53
    }
54
}