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

ReferenceCompiler::compile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
crap 2
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
}