1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20: 21: 22:
23: class ConventionalReflection extends Object implements IReflection
24: {
25:
26: protected $primary;
27:
28:
29: protected $foreign;
30:
31:
32: protected $table;
33:
34:
35:
36: 37: 38: 39: 40: 41:
42: public function __construct($primary = 'id', $foreign = '%s_id', $table = '%s')
43: {
44: $this->primary = $primary;
45: $this->foreign = $foreign;
46: $this->table = $table;
47: }
48:
49:
50:
51: public function getPrimary($table)
52: {
53: return sprintf($this->primary, $this->getColumnFromTable($table));
54: }
55:
56:
57:
58: public function getHasManyReference($table, $key)
59: {
60: $table = $this->getColumnFromTable($table);
61: return array(
62: sprintf($this->table, $key, $table),
63: sprintf($this->foreign, $table, $key),
64: );
65: }
66:
67:
68:
69: public function getBelongsToReference($table, $key)
70: {
71: $table = $this->getColumnFromTable($table);
72: return array(
73: sprintf($this->table, $key, $table),
74: sprintf($this->foreign, $key, $table),
75: );
76: }
77:
78:
79:
80: public function setConnection(Connection $connection)
81: {}
82:
83:
84:
85: protected function getColumnFromTable($name)
86: {
87: if ($this->table !== '%s' && preg_match('(^' . str_replace('%s', '(.*)', preg_quote($this->table)) . '\z)', $name, $match)) {
88: return $match[1];
89: }
90:
91: return $name;
92: }
93:
94: }
95: