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
20
21namespace Doctrine\DBAL\Platforms\Keywords;
22
23use Doctrine\DBAL\Schema\Visitor\Visitor;
24use Doctrine\DBAL\Schema\Table;
25use Doctrine\DBAL\Schema\Column;
26use Doctrine\DBAL\Schema\ForeignKeyConstraint;
27use Doctrine\DBAL\Schema\Schema;
28use Doctrine\DBAL\Schema\Sequence;
29use Doctrine\DBAL\Schema\Index;
30
31class ReservedKeywordsValidator implements Visitor
32{
33    /**
34     * @var KeywordList[]
35     */
36    private $keywordLists = array();
37
38    /**
39     * @var array
40     */
41    private $violations = array();
42
43    public function __construct(array $keywordLists)
44    {
45        $this->keywordLists = $keywordLists;
46    }
47
48    public function getViolations()
49    {
50        return $this->violations;
51    }
52
53    /**
54     * @param string $word
55     * @return array
56     */
57    private function isReservedWord($word)
58    {
59        if ($word[0] == "`") {
60            $word = str_replace('`', '', $word);
61        }
62
63        $keywordLists = array();
64        foreach ($this->keywordLists as $keywordList) {
65            if ($keywordList->isKeyword($word)) {
66                $keywordLists[] = $keywordList->getName();
67            }
68        }
69        return $keywordLists;
70    }
71
72    private function addViolation($asset, $violatedPlatforms)
73    {
74        if ( ! $violatedPlatforms) {
75            return;
76        }
77
78        $this->violations[] = $asset . ' keyword violations: ' . implode(', ', $violatedPlatforms);
79    }
80
81    public function acceptColumn(Table $table, Column $column)
82    {
83        $this->addViolation(
84            'Table ' . $table->getName() . ' column ' . $column->getName(),
85            $this->isReservedWord($column->getName())
86        );
87    }
88
89    public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint)
90    {
91
92    }
93
94    public function acceptIndex(Table $table, Index $index)
95    {
96
97    }
98
99    public function acceptSchema(Schema $schema)
100    {
101
102    }
103
104    public function acceptSequence(Sequence $sequence)
105    {
106
107    }
108
109    public function acceptTable(Table $table)
110    {
111        $this->addViolation(
112            'Table ' . $table->getName(),
113            $this->isReservedWord($table->getName())
114        );
115    }
116}
117