1: <?php
2:
3: namespace Alchemy\core\schema;
4: use Alchemy\core\query\ColumnRef;
5: use Alchemy\core\query\TableRef;
6: use Alchemy\util\promise\IPromisable;
7:
8:
9: 10: 11:
12: class Column extends TableElement implements IPromisable {
13:
14: 15: 16: 17: 18: 19: 20: 21:
22: public static function find($column, $self = null) {
23: list($ref, $col) = explode('.', $column) + array('', '');
24:
25: $table = ($ref == 'self' || $ref == '') ? $self : Table::find($ref);
26: if (!$table) {
27: throw new \Exception("Cannot find Table '{$ref}'.");
28: }
29:
30: return $table->getColumn($col);
31: }
32:
33:
34: public static function list_promisable_methods() {
35: return array(
36: 'copy' => "Alchemy\core\schema\Column",
37: 'getTable' => "Alchemy\core\schema\Table");
38: }
39:
40:
41: 42: 43: 44: 45: 46:
47: public function decode($value) {
48: $def = self::get_definition($this->type);
49: return $def['decode']($this, $value);
50: }
51:
52:
53: 54: 55: 56: 57: 58:
59: public function encode($value) {
60: $def = self::get_definition($this->type);
61: return $def['encode']($this, $value);
62: }
63:
64:
65: 66: 67: 68: 69:
70: public function getForeignKey() {
71: if ($this->args['foreign_key']) {
72: return Index::ForeignKey(array(array($this), array($this->args['foreign_key'])), $this->table, $this->name);
73: }
74: }
75:
76:
77: 78: 79: 80: 81:
82: public function getIndex() {
83: if ($this->args['unique']) {
84: return Index::UniqueKey($this, $this->table, $this->name);
85: } elseif ($this->args['index']) {
86: return Index::Index($this, $this->table, $this->name);
87: }
88: }
89:
90:
91: public function getArg($arg) {
92: return array_key_exists($arg, $this->args) ? $this->args[$arg] : null;
93: }
94:
95:
96: 97: 98: 99: 100: 101:
102: public function getRef(TableRef $table = null) {
103: return new ColumnRef($this, $table ?: $this->getTable()->getRef());
104: }
105:
106:
107: 108: 109: 110: 111:
112: public function isNullable() {
113: return $this->args['null'];
114: }
115:
116:
117: 118: 119: 120: 121:
122: public function isPrimaryKeyPart() {
123: return $this->args['primary_key'];
124: }
125: }
126: