|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Doctrine\DBAL\Schema; |
| 6 | + |
| 7 | +use Doctrine\DBAL\Schema\Exception\InvalidViewDefinition; |
| 8 | +use Doctrine\DBAL\Schema\Name\OptionallyQualifiedName; |
| 9 | + |
| 10 | +final class ViewEditor |
| 11 | +{ |
| 12 | + private ?OptionallyQualifiedName $name = null; |
| 13 | + |
| 14 | + private ?string $sql = null; |
| 15 | + |
| 16 | + /** @internal Use {@link View::editor()} or {@link View::edit()} to create an instance */ |
| 17 | + public function __construct() |
| 18 | + { |
| 19 | + } |
| 20 | + |
| 21 | + public function setName(OptionallyQualifiedName $name): self |
| 22 | + { |
| 23 | + $this->name = $name; |
| 24 | + |
| 25 | + return $this; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * @param non-empty-string $unqualifiedName |
| 30 | + * @param ?non-empty-string $qualifier |
| 31 | + */ |
| 32 | + public function setUnquotedName(string $unqualifiedName, ?string $qualifier = null): self |
| 33 | + { |
| 34 | + $this->name = OptionallyQualifiedName::unquoted($unqualifiedName, $qualifier); |
| 35 | + |
| 36 | + return $this; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @param non-empty-string $unqualifiedName |
| 41 | + * @param ?non-empty-string $qualifier |
| 42 | + */ |
| 43 | + public function setQuotedName(string $unqualifiedName, ?string $qualifier = null): self |
| 44 | + { |
| 45 | + $this->name = OptionallyQualifiedName::quoted($unqualifiedName, $qualifier); |
| 46 | + |
| 47 | + return $this; |
| 48 | + } |
| 49 | + |
| 50 | + public function setSQL(string $sql): self |
| 51 | + { |
| 52 | + $this->sql = $sql; |
| 53 | + |
| 54 | + return $this; |
| 55 | + } |
| 56 | + |
| 57 | + public function create(): View |
| 58 | + { |
| 59 | + if ($this->name === null) { |
| 60 | + throw InvalidViewDefinition::nameNotSet(); |
| 61 | + } |
| 62 | + |
| 63 | + if ($this->sql === null) { |
| 64 | + throw InvalidViewDefinition::sqlNotSet($this->name); |
| 65 | + } |
| 66 | + |
| 67 | + return new View( |
| 68 | + $this->name->toString(), |
| 69 | + $this->sql, |
| 70 | + ); |
| 71 | + } |
| 72 | +} |
0 commit comments