1 <?php
  2 
  3 /**
  4  * Class to represent sql foreign keys.
  5  *
  6  * @author gharlan
  7  *
  8  * @package redaxo\core\sql
  9  */
 10 class rex_sql_foreign_key
 11 {
 12     const RESTRICT = 'RESTRICT';
 13     const CASCADE = 'CASCADE';
 14     const SET_NULL = 'SET NULL';
 15 
 16     /** @var string */
 17     private $name;
 18 
 19     /** @var string */
 20     private $table;
 21 
 22     /** @var string[] */
 23     private $columns;
 24 
 25     /** @var string */
 26     private $onUpdate;
 27 
 28     /** @var string */
 29     private $onDelete;
 30 
 31     private $modified = false;
 32 
 33     /**
 34      * @param string   $name
 35      * @param string   $table
 36      * @param string[] $columns  Mapping of locale column to column in foreign table
 37      * @param string   $onUpdate One of `rex_sql_foreign_key::RESTRICT`, `rex_sql_foreign_key::CASCADE`, `rex_sql_foreign_key::SET_NULL`
 38      * @param string   $onDelete One of `rex_sql_foreign_key::RESTRICT`, `rex_sql_foreign_key::CASCADE`, `rex_sql_foreign_key::SET_NULL`
 39      */
 40     public function __construct($name, $table, array $columns, $onUpdate = self::RESTRICT, $onDelete = self::RESTRICT)
 41     {
 42         $this->name = $name;
 43         $this->table = $table;
 44         $this->columns = $columns;
 45         $this->onUpdate = $onUpdate;
 46         $this->onDelete = $onDelete;
 47     }
 48 
 49     /**
 50      * @param bool $modified
 51      *
 52      * @return $this
 53      */
 54     public function setModified($modified)
 55     {
 56         $this->modified = $modified;
 57 
 58         return $this;
 59     }
 60 
 61     /**
 62      * @return bool
 63      */
 64     public function isModified()
 65     {
 66         return $this->modified;
 67     }
 68 
 69     /**
 70      * @param string $name
 71      *
 72      * @return $this
 73      */
 74     public function setName($name)
 75     {
 76         $this->name = $name;
 77 
 78         return $this->setModified(true);
 79     }
 80 
 81     /**
 82      * @return string
 83      */
 84     public function getName()
 85     {
 86         return $this->name;
 87     }
 88 
 89     /**
 90      * @param string $table
 91      *
 92      * @return $this
 93      */
 94     public function setTable($table)
 95     {
 96         $this->table = $table;
 97 
 98         return $this->setModified(true);
 99     }
100 
101     /**
102      * @return string
103      */
104     public function getTable()
105     {
106         return $this->table;
107     }
108 
109     /**
110      * @param string[] $columns Mapping of locale column to column in foreign table
111      *
112      * @return $this
113      */
114     public function setColumns(array $columns)
115     {
116         $this->columns = $columns;
117 
118         return $this->setModified(true);
119     }
120 
121     /**
122      * @return string[]
123      */
124     public function getColumns()
125     {
126         return $this->columns;
127     }
128 
129     /**
130      * @param string $onUpdate
131      *
132      * @return $this
133      */
134     public function setOnUpdate($onUpdate)
135     {
136         $this->onUpdate = $onUpdate;
137 
138         return $this->setModified(true);
139     }
140 
141     /**
142      * @return string
143      */
144     public function getOnUpdate()
145     {
146         return $this->onUpdate;
147     }
148 
149     /**
150      * @param string $onDelete
151      *
152      * @return $this
153      */
154     public function setOnDelete($onDelete)
155     {
156         $this->onDelete = $onDelete;
157 
158         return $this->setModified(true);
159     }
160 
161     /**
162      * @return string
163      */
164     public function getOnDelete()
165     {
166         return $this->onDelete;
167     }
168 
169     /**
170      * @return bool
171      */
172     public function equals(self $index)
173     {
174         return
175             $this->name === $index->name &&
176             $this->table === $index->table &&
177             $this->columns == $index->columns &&
178             $this->onUpdate === $index->onUpdate &&
179             $this->onDelete === $index->onDelete;
180     }
181 }
182