ReservedKeywordsValidator.php 2.6 KB
Newer Older
1 2
<?php

Michael Moravec's avatar
Michael Moravec committed
3 4
declare(strict_types=1);

5 6 7 8
namespace Doctrine\DBAL\Platforms\Keywords;

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

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

22
    /** @var string[] */
23
    private $violations = [];
24

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

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

41
    /**
42
     * @return string[]
43
     */
44
    private function isReservedWord(string $word) : array
45
    {
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
     * @param string[] $violatedPlatforms
Benjamin Morel's avatar
Benjamin Morel committed
64
     */
65
    private function addViolation(string $asset, array $violatedPlatforms) : void
66
    {
67
        if (! $violatedPlatforms) {
68 69
            return;
        }
70

71 72
        $this->violations[] = $asset . ' keyword violations: ' . implode(', ', $violatedPlatforms);
    }
73

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

Benjamin Morel's avatar
Benjamin Morel committed
85 86 87
    /**
     * {@inheritdoc}
     */
88
    public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint) : void
89 90 91
    {
    }

Benjamin Morel's avatar
Benjamin Morel committed
92 93 94
    /**
     * {@inheritdoc}
     */
95
    public function acceptIndex(Table $table, Index $index) : void
96 97 98
    {
    }

Benjamin Morel's avatar
Benjamin Morel committed
99 100 101
    /**
     * {@inheritdoc}
     */
102
    public function acceptSchema(Schema $schema) : void
103 104 105
    {
    }

Benjamin Morel's avatar
Benjamin Morel committed
106 107 108
    /**
     * {@inheritdoc}
     */
109
    public function acceptSequence(Sequence $sequence) : void
110 111 112
    {
    }

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