Completed
Push — master ( 5b6dd2...e674ef )
by Beniamin
02:46
created

ReferenceParser   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 54
ccs 18
cts 18
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A parseSQL() 0 10 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\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 29
    public function __construct($rawSQL, ReferenceManager $manager)
38
    {
39 29
        $this->rawSQL = $rawSQL;
40 29
        $this->manager = $manager;
41 29
    }
42
43
    /**
44
     * @return string
45
     */
46 29
    public function parseSQL()
47
    {
48 29
        $references = $this->manager->all();
49
50 29
        foreach ($references as &$value) {
51 21
            $value = $this->convertReferenceToValue($value);
52 29
        }
53
54 29
        return str_replace(array_keys($references), array_values($references), $this->rawSQL);
55
    }
56
57
    /**
58
     * @param $reference
59
     *
60
     * @return string
61
     */
62 21
    private function convertReferenceToValue($reference)
63
    {
64 21
        if (is_string($reference)) {
65 1
            return "\"" . $reference ."\"";
66 21
        } elseif ($reference instanceof AbstractTable) {
67 20
            return $reference->getAliasOrName();
68 2
        } elseif ($reference instanceof QueryComponentInterface) {
69 1
            return $reference->buildSQL();
70
        }
71
72 1
        return $reference;
73
    }
74
}