ExtraClauseTrait::before()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 9.4285
nc 1
cc 1
eloc 5
nop 3
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\Clause;
16
17
use Phossa2\Query\Interfaces\Clause\ExtraClauseInterface;
18
19
/**
20
 * ExtraClauseTrait
21
 *
22
 * @package Phossa2\Query
23
 * @author  Hong Zhang <[email protected]>
24
 * @see     ExtraClauseInterface
25
 * @version 2.0.0
26
 * @since   2.0.0 added
27
 */
28
trait ExtraClauseTrait
29
{
30
    use AbstractTrait;
31
32
    /**
33
     * {@inheritDoc}
34
     */
35
    public function before(
36
        /*# string */ $position,
37
        /*# string */ $rawString,
38
        array $params = []
39
    ) {
40
        return $this->beforeAfter('BEFORE', $position, $rawString, $params);
41
    }
42
43
    /**
44
     * {@inheritDoc}
45
     */
46
    public function after(
47
        /*# string */ $position,
48
        /*# string */ $rawString,
49
        array $params = []
50
    ) {
51
        return $this->beforeAfter('AFTER', $position, $rawString, $params);
52
    }
53
54
    /**
55
     * {@inheritDoc}
56
     */
57
    public function hint(/*# string */ $hintString, array $params = [])
58
    {
59
        return $this->beforeAfter('AFTER', 'TYPE', $hintString, $params);
60
    }
61
62
    /**
63
     * {@inheritDoc}
64
     */
65
    public function option(/*# string */ $optionString, array $params = [])
66
    {
67
        return $this->beforeAfter('AFTER', 'STMT', $optionString, $params);
68
    }
69
70
    /**
71
     * @param  string $type
72
     * @param  string $position
73
     * @param  string $rawString
74
     * @param  array $params
75
     * @return $this
76
     * @access protected
77
     */
78
    protected function beforeAfter(
79
        /*# string */ $type,
80
        /*# string */ $position,
81
        /*# string */ $rawString,
82
        array $params = []
83
    ) {
84
        $clause = &$this->getClause($type);
85
        $pos = strtoupper($position);
86
        $clause[$pos][] = $this->positionedParam($rawString, $params);
87
        return $this;
88
    }
89
90
    /**
91
     * Build AFTER/BEFORE
92
     *
93
     * @param  string $beforeOrAfter BEFORE|AFTER
94
     * @param  string $position
95
     * @param  array $settings
96
     * @return string
97
     * @access protected
98
     */
99
    protected function buildBeforeAfter(
100
        /*# string */ $beforeOrAfter,
101
        /*# string */ $position,
102
        array $settings
103
    )/*# : string */ {
104
        $clause = &$this->getClause($beforeOrAfter);
105
        if (isset($clause[$position])) {
106
            $line = $clause[$position];
107
            $sepr = $settings['seperator'];
108
            return $sepr . join($sepr, $line);
109
        } else {
110
            return '';
111
        }
112
    }
113
}
114