123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\Setup\Declaration\Schema\Dto;
- use Magento\Framework\Setup\Declaration\Schema\Dto\Constraints\Internal;
- use Magento\Framework\Setup\Declaration\Schema\Dto\Constraints\Reference;
- /**
- * Table structural element
- * Aggregate inside itself: columns, constraints and indexes
- * Resource is also specified on this strucural element
- */
- class Table extends GenericElement implements
- ElementInterface,
- ElementDiffAwareInterface
- {
- /**
- * In case if we will need to change this object: add, modify or drop, we will need
- * to define it by its type
- */
- const TYPE = 'table';
- /**
- * @var Constraint[]
- */
- private $constraints = [];
- /**
- * @var Column[]
- */
- private $columns = [];
- /**
- * @var string
- */
- protected $type = 'table';
- /**
- * @var Index[]
- */
- private $indexes = [];
- /**
- * @var string
- */
- private $resource;
- /**
- * @var string
- */
- private $engine;
- /**
- * @var string
- */
- private $nameWithoutPrefix;
- /**
- * @var null|string
- */
- private $comment;
- /**
- * @var string
- */
- private $onCreate;
- /**
- * @var string
- */
- private $charset;
- /**
- * @var string
- */
- private $collation;
- /**
- * @param string $name
- * @param string $type
- * @param string $nameWithoutPrefix
- * @param string $resource
- * @param string $engine
- * @param string $charset
- * @param string $collation
- * @param string $onCreate
- * @param string|null $comment
- * @param array $columns
- * @param array $indexes
- * @param array $constraints
- * @SuppressWarnings(PHPMD.ExcessiveParameterList)
- */
- public function __construct(
- string $name,
- string $type,
- string $nameWithoutPrefix,
- string $resource,
- string $engine,
- string $charset,
- string $collation,
- string $onCreate,
- string $comment = null,
- array $columns = [],
- array $indexes = [],
- array $constraints = []
- ) {
- parent::__construct($name, $type);
- $this->columns = $columns;
- $this->indexes = $indexes;
- $this->constraints = $constraints;
- $this->resource = $resource;
- $this->engine = $engine;
- $this->nameWithoutPrefix = $nameWithoutPrefix;
- $this->comment = $comment;
- $this->onCreate = $onCreate;
- $this->charset = $charset;
- $this->collation = $collation;
- }
- /**
- * Return different table constraints.
- *
- * It can be constraint like unique key or reference to another table, etc
- *
- * @return Constraint[]
- */
- public function getConstraints()
- {
- return $this->constraints;
- }
- /**
- * Get constraint by name.
- *
- * @param string $name
- * @return Constraint | bool
- */
- public function getConstraintByName($name)
- {
- return $this->constraints[$name] ?? false;
- }
- /**
- * This method lookup only for foreign keys constraints
- *
- * @return Reference[]
- */
- public function getReferenceConstraints()
- {
- $constraints = [];
- foreach ($this->getConstraints() as $constraint) {
- if ($constraint instanceof Reference) {
- $constraints[$constraint->getName()] = $constraint;
- }
- }
- return $constraints;
- }
- /**
- * Returns primary constraint
- *
- * As primary constraint always have one name
- * and can be only one for table
- * it name is allocated into it constraint
- *
- * @return bool|Internal
- */
- public function getPrimaryConstraint()
- {
- return $this->constraints[Internal::PRIMARY_NAME] ?? false;
- }
- /**
- * Retrieve internal constraints
- *
- * @return array
- */
- public function getInternalConstraints() : array
- {
- $constraints = [];
- foreach ($this->getConstraints() as $constraint) {
- if ($constraint instanceof Internal) {
- $constraints[] = $constraint;
- }
- }
- return $constraints;
- }
- /**
- * Get index by name
- *
- * @param string $name
- * @return Index | bool
- */
- public function getIndexByName($name)
- {
- return $this->indexes[$name] ?? false;
- }
- /**
- * Return all columns.
- *
- * Note, table always must have columns
- *
- * @return Column[]
- */
- public function getColumns()
- {
- return $this->columns;
- }
- /**
- * Return all indexes, that are applied to table
- *
- * @return Index[]
- */
- public function getIndexes()
- {
- return $this->indexes;
- }
- /**
- * Retrieve shard name, on which table will exists
- *
- * @return string
- */
- public function getResource()
- {
- return $this->resource;
- }
- /**
- * Add constraints
- *
- * This is workaround, as any DTO object couldnt be changed after instantiation.
- * However there is case, when we have 2 tables with constraints in different tables,
- * that depends to each other table. So we need to setup DTO first and only then pass
- * problematic constraints to it, in order to avoid circular dependency.
- *
- * @param Constraint[] $constraints
- */
- public function addConstraints(array $constraints)
- {
- $this->constraints = array_replace($this->constraints, $constraints);
- }
- /**
- * Add columns
- *
- * @param Column[] $columns
- */
- public function addColumns(array $columns)
- {
- $this->columns = array_replace($this->columns, $columns);
- }
- /**
- * Retrieve information about trigger
- *
- * @return string
- */
- public function getOnCreate()
- {
- return $this->onCreate;
- }
- /**
- * If column exists - retrieve column
- *
- * @param string $nameOrId
- * @return Column | bool
- */
- public function getColumnByName($nameOrId)
- {
- if (isset($this->columns[$nameOrId])) {
- return $this->columns[$nameOrId];
- }
- return false;
- }
- /**
- * Retrieve elements by specific type
- *
- * Allowed types: columns, constraints, indexes...
- *
- * @param string $type
- * @return ElementInterface[]
- */
- public function getElementsByType($type)
- {
- if (!isset($this->{$type})) {
- throw new \InvalidArgumentException(sprintf("Type %s is not defined", $type));
- }
- return $this->{$type};
- }
- /**
- * Add indexes
- *
- * This is workaround, as any DTO object couldnt be changed after instantiation.
- * However there is case, when we depends on column definition we need modify our indexes
- *
- * @param array $indexes
- */
- public function addIndexes(array $indexes)
- {
- $this->indexes = array_replace($this->indexes, $indexes);
- }
- /**
- * @inheritdoc
- */
- public function getElementType()
- {
- return self::TYPE;
- }
- /**
- * Get engine name
- *
- * @return string
- */
- public function getEngine(): string
- {
- return $this->engine;
- }
- /**
- * @inheritdoc
- */
- public function getDiffSensitiveParams()
- {
- return [
- 'resource' => $this->getResource(),
- 'engine' => $this->getEngine(),
- 'comment' => $this->getComment(),
- 'charset' => $this->getCharset(),
- 'collation' => $this->getCollation()
- ];
- }
- /**
- * Return charset of table
- *
- * @return string
- */
- public function getCharset() : string
- {
- return $this->charset;
- }
- /**
- * Return charset of table
- *
- * @return string
- */
- public function getCollation() : string
- {
- return $this->collation;
- }
- /**
- * Get name without prefix
- *
- * @return string
- */
- public function getNameWithoutPrefix(): string
- {
- return $this->nameWithoutPrefix;
- }
- /**
- * Get comment
- *
- * @return null|string
- */
- public function getComment()
- {
- return $this->comment;
- }
- }
|