Completed
Push — master ( 439406...4b2726 )
by Hong
03:09
created

StatementAbstract   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 108
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getDialect() 0 8 2
A getStatement() 0 13 1
A __toString() 0 4 1
A combineSettings() 0 11 1
A build() 0 10 2
A getType() 0 4 1
getConfigs() 0 1 ?
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Query
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Query\Traits;
16
17
use Phossa2\Shared\Base\ObjectAbstract;
18
use Phossa2\Query\Interfaces\BuilderInterface;
19
use Phossa2\Query\Interfaces\StatementInterface;
20
21
/**
22
 * StatementAbstract
23
 *
24
 * @package Phossa2\Query
25
 * @author  Hong Zhang <[email protected]>
26
 * @see     ObjectAbstract
27
 * @see     StatementInterface
28
 * @version 2.0.0
29
 * @since   2.0.0 added
30
 */
31
abstract class StatementAbstract extends ObjectAbstract implements StatementInterface
32
{
33
    use DialectAwareTrait, SettingsTrait, BuilderAwareTrait;
34
35
    /**
36
     * Constructor
37
     *
38
     * @param  BuilderInterface $builder
39
     * @access public
40
     */
41
    public function __construct(BuilderInterface $builder)
42
    {
43
        $this->setBuilder($builder);
44
    }
45
46
    /**
47
     * {@inheritDoc}
48
     */
49
    public function getDialect()/*# : DialectInterface */
50
    {
51
        if ($this->hasDialect()) {
52
            return $this->getDialect();
53
        } else {
54
            return $this->getBuilder()->getDialect();
55
        }
56
    }
57
58
    /**
59
     * {@inheritDoc}
60
     */
61
    public function getStatement(array $settings = [])/*# : string */
62
    {
63
        // combine settings
64
        $settings = $this->combineSettings(
65
            $this->getDialect()->dialectSettings(), // dialect specific
66
            $settings // user provided settings
67
        );
68
69
        // build sql
70
        $sql = $this->build($settings);
71
72
        return $sql;
73
    }
74
75
    /**
76
     * {@inheritDoc}
77
     */
78
    public function __toString()/*# : string */
79
    {
80
        return $this->getStatement();
81
    }
82
83
    /**
84
     * Combine builder/statement and provided settings
85
     *
86
     * @param  array $settings1 dialect specific
87
     * @param  array $settings2 user provided
88
     * @access protected
89
     */
90
    protected function combineSettings(
91
        array $settings1,
92
        array $settings2
93
    )/*# : array */ {
94
        return array_merge(
95
            $this->getBuilder()->getSettings(), // from builder
96
            $this->getSettings(), // current statement
97
            $settings1, // dialect specific
98
            $settings2  // user settings
99
        );
100
    }
101
102
    /**
103
     * Build SQL statement clauses
104
     *
105
     * @param  array $settings
106
     * @param  string
107
     * @access protected
108
     */
109
    protected function build(array $settings)/*# : string */
110
    {
111
        $result = $this->getType();
112
        $settings['join'] = $settings['seperator'] . $settings['indent'];
113
        foreach ($this->getConfigs() as $clause) {
114
            $method = 'build' . ucfirst(strtolower($clause));
115
            $result .= $this->{$method}($settings);
116
        }
117
        return $result;
118
    }
119
120
    /**
121
     * Get the statement type, such as 'SELECT'
122
     *
123
     * @return string
124
     * @access protected
125
     */
126
    protected function getType()/*# : string */
127
    {
128
        return '';
129
    }
130
131
    /**
132
     * Get clause configurations
133
     *
134
     * @return array
135
     * @access protected
136
     */
137
    abstract protected function getConfigs()/*# : array */;
138
}
139