ReservedKeywordsValidator.php 2.58 KB
Newer Older
1 2 3 4 5 6
<?php

namespace Doctrine\DBAL\Platforms\Keywords;

use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
7
use Doctrine\DBAL\Schema\Index;
8 9
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Sequence;
10 11
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\Visitor\Visitor;
12 13
use function implode;
use function str_replace;
14 15 16

class ReservedKeywordsValidator implements Visitor
{
17
    /** @var KeywordList[] */
18
    private $keywordLists = [];
19

20
    /** @var string[] */
21
    private $violations = [];
22

Benjamin Morel's avatar
Benjamin Morel committed
23
    /**
24
     * @param KeywordList[] $keywordLists
Benjamin Morel's avatar
Benjamin Morel committed
25
     */
26 27 28 29
    public function __construct(array $keywordLists)
    {
        $this->keywordLists = $keywordLists;
    }
30

Benjamin Morel's avatar
Benjamin Morel committed
31
    /**
32
     * @return string[]
Benjamin Morel's avatar
Benjamin Morel committed
33
     */
34 35 36 37
    public function getViolations()
    {
        return $this->violations;
    }
38

39 40
    /**
     * @param string $word
Benjamin Morel's avatar
Benjamin Morel committed
41
     *
42
     * @return string[]
43 44 45
     */
    private function isReservedWord($word)
    {
46
        if ($word[0] === '`') {
47 48
            $word = str_replace('`', '', $word);
        }
49

50
        $keywordLists = [];
51
        foreach ($this->keywordLists as $keywordList) {
52 53
            if (! $keywordList->isKeyword($word)) {
                continue;
54
            }
55 56

            $keywordLists[] = $keywordList->getName();
57
        }
58

59 60
        return $keywordLists;
    }
61

Benjamin Morel's avatar
Benjamin Morel committed
62
    /**
63 64
     * @param string   $asset
     * @param string[] $violatedPlatforms
Benjamin Morel's avatar
Benjamin Morel committed
65 66 67
     *
     * @return void
     */
68 69
    private function addViolation($asset, $violatedPlatforms)
    {
70
        if (! $violatedPlatforms) {
71 72
            return;
        }
73

74 75
        $this->violations[] = $asset . ' keyword violations: ' . implode(', ', $violatedPlatforms);
    }
76

Benjamin Morel's avatar
Benjamin Morel committed
77 78 79
    /**
     * {@inheritdoc}
     */
80 81 82 83 84 85 86 87
    public function acceptColumn(Table $table, Column $column)
    {
        $this->addViolation(
            'Table ' . $table->getName() . ' column ' . $column->getName(),
            $this->isReservedWord($column->getName())
        );
    }

Benjamin Morel's avatar
Benjamin Morel committed
88 89 90
    /**
     * {@inheritdoc}
     */
91 92 93 94
    public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint)
    {
    }

Benjamin Morel's avatar
Benjamin Morel committed
95 96 97
    /**
     * {@inheritdoc}
     */
98 99 100 101
    public function acceptIndex(Table $table, Index $index)
    {
    }

Benjamin Morel's avatar
Benjamin Morel committed
102 103 104
    /**
     * {@inheritdoc}
     */
105 106 107 108
    public function acceptSchema(Schema $schema)
    {
    }

Benjamin Morel's avatar
Benjamin Morel committed
109 110 111
    /**
     * {@inheritdoc}
     */
112 113 114 115
    public function acceptSequence(Sequence $sequence)
    {
    }

Benjamin Morel's avatar
Benjamin Morel committed
116 117 118
    /**
     * {@inheritdoc}
     */
119 120 121 122 123 124 125
    public function acceptTable(Table $table)
    {
        $this->addViolation(
            'Table ' . $table->getName(),
            $this->isReservedWord($table->getName())
        );
    }
126
}