rougin /
describe
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Rougin\Describe; |
||
| 4 | |||
| 5 | use Doctrine\Common\Inflector\Inflector; |
||
| 6 | |||
| 7 | /** |
||
| 8 | * Column Class |
||
| 9 | * |
||
| 10 | * Stores a column information from the results given. |
||
| 11 | * |
||
| 12 | * @package Describe |
||
| 13 | * @author Rougin Gutib <[email protected]> |
||
| 14 | */ |
||
| 15 | class Column |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var boolean |
||
| 19 | */ |
||
| 20 | protected $increment = false; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $type = ''; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $default = ''; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $field = ''; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var boolean |
||
| 39 | */ |
||
| 40 | protected $foreign = false; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var integer |
||
| 44 | */ |
||
| 45 | protected $length = 0; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var boolean |
||
| 49 | */ |
||
| 50 | protected $null = false; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var boolean |
||
| 54 | */ |
||
| 55 | protected $primary = false; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | protected $reference = array('field' => '', 'table' => ''); |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var boolean |
||
| 64 | */ |
||
| 65 | protected $unique = false; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var boolean |
||
| 69 | */ |
||
| 70 | protected $unsigned = false; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Gets the data type. |
||
| 74 | * |
||
| 75 | * @return string |
||
| 76 | */ |
||
| 77 | 3 | public function getDataType() |
|
| 78 | { |
||
| 79 | 3 | return $this->type; |
|
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Gets the default value. |
||
| 84 | * |
||
| 85 | * @return string |
||
| 86 | */ |
||
| 87 | 3 | public function getDefaultValue() |
|
| 88 | { |
||
| 89 | 3 | return $this->default; |
|
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Gets the column description. |
||
| 94 | * |
||
| 95 | * @return string |
||
| 96 | */ |
||
| 97 | 51 | public function getField() |
|
| 98 | { |
||
| 99 | 51 | return $this->field; |
|
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Gets the foreign field. |
||
| 104 | * |
||
| 105 | * @return string |
||
| 106 | */ |
||
| 107 | 3 | public function getReferencedField() |
|
| 108 | { |
||
| 109 | 3 | return $this->reference['field']; |
|
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Gets the foreign table. |
||
| 114 | * |
||
| 115 | * @return string |
||
| 116 | */ |
||
| 117 | 3 | public function getReferencedTable() |
|
| 118 | { |
||
| 119 | 3 | return $this->reference['table']; |
|
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Gets the field's length. |
||
| 124 | * |
||
| 125 | * @return integer |
||
| 126 | */ |
||
| 127 | 3 | public function getLength() |
|
| 128 | { |
||
| 129 | 3 | return $this->length; |
|
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Check if the field is an auto incrementing field. |
||
| 134 | * |
||
| 135 | * @return boolean |
||
| 136 | */ |
||
| 137 | 3 | public function isAutoIncrement() |
|
| 138 | { |
||
| 139 | 3 | return $this->increment; |
|
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Check if the field is a foreign key. |
||
| 144 | * |
||
| 145 | * @return boolean |
||
| 146 | */ |
||
| 147 | 3 | public function isForeignKey() |
|
| 148 | { |
||
| 149 | 3 | return $this->foreign; |
|
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Check if the field accept NULL values. |
||
| 154 | * |
||
| 155 | * @return boolean |
||
| 156 | */ |
||
| 157 | 3 | public function isNull() |
|
| 158 | { |
||
| 159 | 3 | return $this->null; |
|
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Check if the field is a primary key. |
||
| 164 | * |
||
| 165 | * @return boolean |
||
| 166 | */ |
||
| 167 | 30 | public function isPrimaryKey() |
|
| 168 | { |
||
| 169 | 30 | return $this->primary; |
|
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Check if field is unique. |
||
| 174 | * |
||
| 175 | * @return boolean |
||
| 176 | */ |
||
| 177 | 3 | public function isUnique() |
|
| 178 | { |
||
| 179 | 3 | return $this->unique; |
|
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Check if field is unsigned. |
||
| 184 | * |
||
| 185 | * @return boolean |
||
| 186 | */ |
||
| 187 | 3 | public function isUnsigned() |
|
| 188 | { |
||
| 189 | 3 | return $this->unsigned; |
|
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Sets the auto increment. |
||
| 194 | * |
||
| 195 | * @param boolean $increment |
||
| 196 | */ |
||
| 197 | 114 | public function setAutoIncrement($increment) |
|
| 198 | { |
||
| 199 | 114 | $this->increment = $increment; |
|
| 200 | |||
| 201 | 114 | return $this; |
|
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Sets the data type. |
||
| 206 | * |
||
| 207 | * @param string $type |
||
| 208 | * @return self |
||
| 209 | */ |
||
| 210 | 114 | public function setDataType($type) |
|
| 211 | { |
||
| 212 | 114 | $types = array('integer', 'string', 'string'); |
|
| 213 | |||
| 214 | 114 | $shorthand = array('int', 'varchar', 'text'); |
|
| 215 | |||
| 216 | 114 | $index = array_search($type, $shorthand); |
|
| 217 | |||
| 218 | 114 | $this->type = $index === false ? $type : $types[$index]; |
|
| 219 | |||
| 220 | 114 | return $this; |
|
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Sets the default value. |
||
| 225 | * |
||
| 226 | * @param string $default |
||
| 227 | */ |
||
| 228 | 114 | public function setDefaultValue($default) |
|
| 229 | { |
||
| 230 | 114 | $this->default = $default; |
|
| 231 | |||
| 232 | 114 | return $this; |
|
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Sets the column's description. |
||
| 237 | * |
||
| 238 | * @param string $field |
||
| 239 | */ |
||
| 240 | 114 | public function setField($field) |
|
| 241 | { |
||
| 242 | 114 | $this->field = $field; |
|
| 243 | |||
| 244 | 114 | return $this; |
|
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Sets the field as a foreign key. |
||
| 249 | * |
||
| 250 | * @param boolean $foreign |
||
| 251 | */ |
||
| 252 | 114 | public function setForeign($foreign) |
|
| 253 | { |
||
| 254 | 114 | $this->foreign = $foreign; |
|
| 255 | |||
| 256 | 114 | return $this; |
|
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Sets the foreign field. |
||
| 261 | * |
||
| 262 | * @param string $field |
||
| 263 | */ |
||
| 264 | 114 | public function setReferencedField($field) |
|
| 265 | { |
||
| 266 | 114 | $this->reference['field'] = $field; |
|
| 267 | |||
| 268 | 114 | return $this; |
|
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Sets the foreign table. |
||
| 273 | * |
||
| 274 | * @param string $table |
||
| 275 | */ |
||
| 276 | 114 | public function setReferencedTable($table) |
|
| 277 | { |
||
| 278 | 114 | $this->reference['table'] = $table; |
|
| 279 | |||
| 280 | 114 | return $this; |
|
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Sets the field's length. |
||
| 285 | * |
||
| 286 | * @param integer $length |
||
| 287 | */ |
||
| 288 | 78 | public function setLength($length) |
|
| 289 | { |
||
| 290 | 78 | $this->length = $length; |
|
| 291 | |||
| 292 | 78 | return $this; |
|
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Sets if field accepts NULL values. |
||
| 297 | * |
||
| 298 | * @param boolean $null |
||
| 299 | */ |
||
| 300 | 114 | public function setNull($null = true) |
|
| 301 | { |
||
| 302 | 114 | $this->null = $null; |
|
| 303 | |||
| 304 | 114 | return $this; |
|
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Sets if field is a primary key. |
||
| 309 | * |
||
| 310 | * @param boolean $primary |
||
| 311 | */ |
||
| 312 | 114 | public function setPrimary($primary = true) |
|
| 313 | { |
||
| 314 | 114 | $this->primary = $primary; |
|
| 315 | |||
| 316 | 114 | return $this; |
|
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Sets if field is a unique key. |
||
| 321 | * |
||
| 322 | * @param boolean $unique |
||
| 323 | */ |
||
| 324 | 78 | public function setUnique($unique = true) |
|
| 325 | { |
||
| 326 | 78 | $this->unique = $unique; |
|
| 327 | |||
| 328 | 78 | return $this; |
|
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Sets if field is an unsigned key. |
||
| 333 | * |
||
| 334 | * @param boolean $unsigned |
||
| 335 | */ |
||
| 336 | 3 | public function setUnsigned($unsigned = true) |
|
| 337 | { |
||
| 338 | 3 | $this->unsigned = $unsigned; |
|
| 339 | |||
| 340 | 3 | return $this; |
|
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Calls methods from this class in underscore case. |
||
| 345 | * NOTE: To be removed in v2.0.0. All new methods are now in one word. |
||
| 346 | * |
||
| 347 | * @param string $method |
||
| 348 | * @param mixed $parameters |
||
| 349 | * @return mixed |
||
| 350 | */ |
||
| 351 | 3 | public function __call($method, $parameters) |
|
| 352 | { |
||
| 353 | 3 | $method = Inflector::camelize($method); |
|
|
0 ignored issues
–
show
Deprecated Code
introduced
by
Loading history...
|
|||
| 354 | |||
| 355 | 3 | if (method_exists($this, $method) === true) { |
|
| 356 | 3 | $instance = array($this, $method); |
|
| 357 | |||
| 358 | 3 | $result = call_user_func_array($instance, $parameters); |
|
| 359 | 1 | } |
|
| 360 | |||
| 361 | 3 | return isset($result) ? $result : $this; |
|
| 362 | } |
||
| 363 | } |
||
| 364 |