Completed
Push — master ( b54f87...bde51f )
by Beniamin
04:50
created

ReferenceParser::parseSQL()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 20
c 0
b 0
f 0
ccs 14
cts 14
cp 1
rs 8.8571
cc 5
eloc 10
nc 9
nop 0
crap 5
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\Parser;
13
14
use Phuria\SQLBuilder\QueryBuilder\Component\QueryComponentInterface;
15
use Phuria\SQLBuilder\ReferenceManager;
16
use Phuria\SQLBuilder\Table\AbstractTable;
17
18
/**
19
 * @author Beniamin Jonatan Šimko <[email protected]>
20
 */
21
class ReferenceParser
22
{
23
    /**
24
     * @var string $rawSQL
25
     */
26
    private $rawSQL;
27
28
    /**
29
     * @var ReferenceManager $manager
30
     */
31
    private $manager;
32
33
    /**
34
     * @param                  $rawSQL
35
     * @param ReferenceManager $manager
36
     */
37 27
    public function __construct($rawSQL, ReferenceManager $manager)
38
    {
39 27
        $this->rawSQL = $rawSQL;
40 27
        $this->manager = $manager;
41 27
    }
42
43
    /**
44
     * @return string
45
     */
46 27
    public function parseSQL()
47
    {
48 27
        $references = $this->manager->all();
49
50 27
        foreach ($references as &$value) {
51 19
            if (is_string($value)) {
52 1
                $value = "\"" . $value ."\"";
53 1
            }
54
55 19
            if ($value instanceof AbstractTable) {
56 18
                $value = $value->getAliasOrName();
57 18
            }
58
59 19
            if ($value instanceof QueryComponentInterface) {
60 1
                $value = $value->buildSQL();
61 1
            }
62 27
        }
63
64 27
        return str_replace(array_keys($references), array_values($references), $this->rawSQL);
65
    }
66
}