1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104:
<?php
namespace CzProject\SqlSchema;
class Index
{
const TYPE_INDEX = 'INDEX';
const TYPE_PRIMARY = 'PRIMARY';
const TYPE_UNIQUE = 'UNIQUE';
const TYPE_FULLTEXT = 'FULLTEXT';
/** @var string */
private $name;
/** @var string */
private $type;
/** @var IndexColumn[] */
private $columns = array();
/**
* @param string
* @param string[]|string
* @param string
*/
public function __construct($name, $columns = array(), $type = self::TYPE_INDEX)
{
$this->name = $name;
$this->setType($type);
if (!is_array($columns)) {
$columns = array($columns);
}
foreach ($columns as $column) {
$this->addColumn($column);
}
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param string
* @return self
*/
public function setType($type)
{
$type = (string) $type;
$exists = $type === self::TYPE_INDEX
|| $type === self::TYPE_PRIMARY
|| $type === self::TYPE_UNIQUE
|| $type === self::TYPE_FULLTEXT;
if (!$exists) {
throw new OutOfRangeException("Index type '$type' not found.");
}
$this->type = $type;
return $this;
}
/**
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* @param IndexColumn|string
* @return IndexColumn
*/
public function addColumn($column)
{
if (!($column instanceof IndexColumn)) {
$column = new IndexColumn($column);
}
return $this->columns[] = $column;
}
/**
* @return IndexColumn[]
*/
public function getColumns()
{
return $this->columns;
}
}