class KeyBase

Base class for table keys (primary, unique, index).

Hierarchy

Expanded class hierarchy of KeyBase

File

core/lib/Drupal/Core/Database/SchemaDefinition/KeyBase.php, line 12

Namespace

Drupal\Core\Database\SchemaDefinition
View source
abstract class KeyBase implements SchemaDefinitionInterface {
  
  /**
   * The key columns.
   *
   * @var list<KeyColumn>
   *   The list of KeyColumn objects.
   */
  public readonly array $columns;
  
  /**
   * Constructor.
   *
   * @param list<KeyColumn|string|array{0:string, 1:int}> $columns
   *   A mix of key column specifiers, being KeyColumn objects, strings naming
   *   columns, or arrays of two elements, column name and length, specifying a
   *   prefix of the named column.
   * @param bool $limitedLengthAllowed
   *   Defines if limited length columns are allowed.
   */
  public function __construct(array $columns, bool $limitedLengthAllowed) {
    $this->columns = $this->buildKeyColumns($columns, $limitedLengthAllowed);
  }
  
  /**
   * Builds an array of KeyColumn objects from a mixed list of columns.
   *
   * @param list<KeyColumn|string|array{0:string, 1:int}> $rawColumns
   *   A mix of key column specifiers, being KeyColumn objects, strings naming
   *   columns, or arrays of two elements, column name and length, specifying a
   *   prefix of the named column.
   * @param bool $limitedLengthAllowed
   *   Defines if limited length columns are allowed.
   *
   * @return list<KeyColumn>
   *   The normalized list of KeyColumn objects.
   */
  protected function buildKeyColumns(array $rawColumns, bool $limitedLengthAllowed) : array {
    $columns = [];
    foreach ($rawColumns as $rawColumn) {
      if ($rawColumn instanceof KeyColumn) {
        if (!$limitedLengthAllowed && $rawColumn->length !== NULL) {
          throw new SchemaDefinitionException('Limited length columns not allowed in this context');
        }
        $columns[] = $rawColumn;
      }
      elseif (is_array($rawColumn)) {
        if (!$limitedLengthAllowed) {
          throw new SchemaDefinitionException('Limited length columns not allowed in this context');
        }
        $columns[] = new KeyColumn($rawColumn[0], $rawColumn[1]);
      }
      elseif (is_string($rawColumn)) {
        $columns[] = new KeyColumn($rawColumn);
      }
      else {
        throw new SchemaDefinitionException("Key columns should be defined as an array of KeyColumn objects, or strings indicating the column name, or arrays with column name and limited length");
      }
    }
    return $columns;
  }
  
  /**
   * {@inheritdoc}
   */
  public function toArray() : array {
    $spec = [];
    foreach ($this->columns as $keyColumn) {
      $spec[] = $keyColumn->length !== NULL ? [
        $keyColumn->name,
        $keyColumn->length,
      ] : $keyColumn->name;
    }
    return $spec;
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
KeyBase::$columns public property The key columns.
KeyBase::buildKeyColumns protected function Builds an array of KeyColumn objects from a mixed list of columns.
KeyBase::toArray public function Converts the object to legacy array-based specifications. Overrides SchemaDefinitionInterface::toArray 1
KeyBase::__construct public function Constructor. 4

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.