1<?php
2/*
3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14 *
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license. For more information, see
17 * <http://www.doctrine-project.org>.
18 */
19
20namespace Doctrine\DBAL\Platforms\Keywords;
21
22use Doctrine\DBAL\Schema\Visitor\Visitor;
23use Doctrine\DBAL\Schema\Table;
24use Doctrine\DBAL\Schema\Column;
25use Doctrine\DBAL\Schema\ForeignKeyConstraint;
26use Doctrine\DBAL\Schema\Schema;
27use Doctrine\DBAL\Schema\Sequence;
28use Doctrine\DBAL\Schema\Index;
29
30class ReservedKeywordsValidator implements Visitor
31{
32    /**
33     * @var KeywordList[]
34     */
35    private $keywordLists = array();
36
37    /**
38     * @var array
39     */
40    private $violations = array();
41
42    /**
43     * @param \Doctrine\DBAL\Platforms\Keywords\KeywordList[] $keywordLists
44     */
45    public function __construct(array $keywordLists)
46    {
47        $this->keywordLists = $keywordLists;
48    }
49
50    /**
51     * @return array
52     */
53    public function getViolations()
54    {
55        return $this->violations;
56    }
57
58    /**
59     * @param string $word
60     *
61     * @return array
62     */
63    private function isReservedWord($word)
64    {
65        if ($word[0] == "`") {
66            $word = str_replace('`', '', $word);
67        }
68
69        $keywordLists = array();
70        foreach ($this->keywordLists as $keywordList) {
71            if ($keywordList->isKeyword($word)) {
72                $keywordLists[] = $keywordList->getName();
73            }
74        }
75
76        return $keywordLists;
77    }
78
79    /**
80     * @param string $asset
81     * @param array  $violatedPlatforms
82     *
83     * @return void
84     */
85    private function addViolation($asset, $violatedPlatforms)
86    {
87        if ( ! $violatedPlatforms) {
88            return;
89        }
90
91        $this->violations[] = $asset . ' keyword violations: ' . implode(', ', $violatedPlatforms);
92    }
93
94    /**
95     * {@inheritdoc}
96     */
97    public function acceptColumn(Table $table, Column $column)
98    {
99        $this->addViolation(
100            'Table ' . $table->getName() . ' column ' . $column->getName(),
101            $this->isReservedWord($column->getName())
102        );
103    }
104
105    /**
106     * {@inheritdoc}
107     */
108    public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint)
109    {
110    }
111
112    /**
113     * {@inheritdoc}
114     */
115    public function acceptIndex(Table $table, Index $index)
116    {
117    }
118
119    /**
120     * {@inheritdoc}
121     */
122    public function acceptSchema(Schema $schema)
123    {
124    }
125
126    /**
127     * {@inheritdoc}
128     */
129    public function acceptSequence(Sequence $sequence)
130    {
131    }
132
133    /**
134     * {@inheritdoc}
135     */
136    public function acceptTable(Table $table)
137    {
138        $this->addViolation(
139            'Table ' . $table->getName(),
140            $this->isReservedWord($table->getName())
141        );
142    }
143}
144