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

ReferenceParser   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 46
c 0
b 0
f 0
wmc 6
lcom 1
cbo 3
ccs 18
cts 18
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B parseSQL() 0 20 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
}