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: public function __construct($primary = 'id', $foreign = '%s_id', $table = '%s')
42: {
43: $this->primary = $primary;
44: $this->foreign = $foreign;
45: $this->table = $table;
46: }
47:
48:
49: public function getPrimary($table)
50: {
51: return sprintf($this->primary, $this->getColumnFromTable($table));
52: }
53:
54:
55: public function getHasManyReference($table, $key)
56: {
57: $table = $this->getColumnFromTable($table);
58: return array(
59: sprintf($this->table, $key, $table),
60: sprintf($this->foreign, $table, $key),
61: );
62: }
63:
64:
65: public function getBelongsToReference($table, $key)
66: {
67: $table = $this->getColumnFromTable($table);
68: return array(
69: sprintf($this->table, $key, $table),
70: sprintf($this->foreign, $key, $table),
71: );
72: }
73:
74:
75: public function setConnection(Connection $connection)
76: {}
77:
78:
79: protected function getColumnFromTable($name)
80: {
81: if ($this->table !== '%s' && preg_match('(^' . str_replace('%s', '(.*)', preg_quote($this->table)) . '\z)', $name, $match)) {
82: return $match[1];
83: }
84:
85: return $name;
86: }
87:
88: }
89: