UniqueConstraint.php 3.14 KB
Newer Older
1 2
<?php

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

5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
namespace Doctrine\DBAL\Schema;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use function array_keys;
use function array_map;
use function strtolower;

/**
 * Class for a unique constraint.
 */
class UniqueConstraint extends AbstractAsset implements Constraint
{
    /**
     * Asset identifier instances of the column names the unique constraint is associated with.
     *
20
     * @var array<string, Identifier>
21 22 23
     */
    protected $columns = [];

24 25 26
    /**
     * Platform specific flags
     *
27
     * @var array<string, true>
28
     */
Guilherme Blanco's avatar
Guilherme Blanco committed
29
    protected $flags = [];
30

31 32 33
    /**
     * Platform specific options
     *
34
     * @var array<string, mixed>
35 36 37 38
     */
    private $options = [];

    /**
39 40 41
     * @param array<string>        $columns
     * @param array<string>        $flags
     * @param array<string, mixed> $options
42
     */
43
    public function __construct(string $indexName, array $columns, array $flags = [], array $options = [])
44 45 46 47 48 49 50 51
    {
        $this->_setName($indexName);

        $this->options = $options;

        foreach ($columns as $column) {
            $this->_addColumn($column);
        }
52 53 54 55

        foreach ($flags as $flag) {
            $this->addFlag($flag);
        }
56 57 58 59 60
    }

    /**
     * {@inheritdoc}
     */
61
    public function getColumns() : array
62
    {
Guilherme Blanco's avatar
Guilherme Blanco committed
63
        return array_keys($this->columns);
64 65 66 67 68
    }

    /**
     * {@inheritdoc}
     */
69
    public function getQuotedColumns(AbstractPlatform $platform) : array
70 71 72
    {
        $columns = [];

Guilherme Blanco's avatar
Guilherme Blanco committed
73
        foreach ($this->columns as $column) {
74 75 76 77 78 79 80
            $columns[] = $column->getQuotedName($platform);
        }

        return $columns;
    }

    /**
81
     * @return array<int, string>
82
     */
83
    public function getUnquotedColumns() : array
84 85 86 87
    {
        return array_map([$this, 'trimQuotes'], $this->getColumns());
    }

88 89 90
    /**
     * Returns platform specific flags for unique constraint.
     *
91
     * @return array<int, string>
92
     */
93
    public function getFlags() : array
94 95 96 97 98 99 100
    {
        return array_keys($this->flags);
    }

    /**
     * Adds flag for a unique constraint that translates to platform specific handling.
     *
Guilherme Blanco's avatar
Guilherme Blanco committed
101
     * @example $uniqueConstraint->addFlag('CLUSTERED')
102
     */
103
    public function addFlag(string $flag) : self
104 105 106 107 108 109 110 111 112
    {
        $this->flags[strtolower($flag)] = true;

        return $this;
    }

    /**
     * Does this unique constraint have a specific flag?
     */
113
    public function hasFlag(string $flag) : bool
114 115 116 117 118 119 120
    {
        return isset($this->flags[strtolower($flag)]);
    }

    /**
     * Removes a flag.
     */
121
    public function removeFlag(string $flag) : void
122 123 124 125
    {
        unset($this->flags[strtolower($flag)]);
    }

126
    public function hasOption(string $name) : bool
127 128 129 130 131 132 133
    {
        return isset($this->options[strtolower($name)]);
    }

    /**
     * @return mixed
     */
134
    public function getOption(string $name)
135 136 137 138 139
    {
        return $this->options[strtolower($name)];
    }

    /**
140
     * @return array<string, mixed>
141
     */
142
    public function getOptions() : array
143 144 145
    {
        return $this->options;
    }
Guilherme Blanco's avatar
Guilherme Blanco committed
146

147
    protected function _addColumn(string $column) : void
Guilherme Blanco's avatar
Guilherme Blanco committed
148 149 150
    {
        $this->columns[$column] = new Identifier($column);
    }
151
}