Rev 51 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
27 | PointedEar | 1 | <?php |
2 | |||
51 | PointedEar | 3 | namespace PointedEars\PHPX\Db; |
4 | |||
27 | PointedEar | 5 | /** |
6 | * Generic abstract database mapper class |
||
7 | * |
||
8 | * @author Thomas Lahn |
||
9 | */ |
||
51 | PointedEar | 10 | abstract class Mapper extends \PointedEars\PHPX\AbstractModel |
27 | PointedEar | 11 | { |
12 | /** |
||
13 | * Class name of the associated table model |
||
14 | * |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $_table = 'Table'; |
||
49 | PointedEar | 18 | |
27 | PointedEar | 19 | protected $_dbTable; |
49 | PointedEar | 20 | |
27 | PointedEar | 21 | /** |
22 | * Sets the {@link Table} for this mapper |
||
23 | * @param string|Table $dbTable |
||
24 | * Class name of the new instance, or an existing instance |
||
25 | * @throws Exception if <var>$dbTable</var> is not a <code>Table</code> |
||
26 | */ |
||
27 | public function setDbTable($table) |
||
28 | { |
||
29 | if (is_string($table)) |
||
30 | { |
||
31 | $table = new $table(); |
||
32 | } |
||
49 | PointedEar | 33 | |
27 | PointedEar | 34 | if (!($table instanceof Table)) { |
35 | throw new Exception('Invalid table data gateway provided'); |
||
36 | } |
||
49 | PointedEar | 37 | |
27 | PointedEar | 38 | $this->_dbTable = $table; |
39 | } |
||
40 | |||
41 | /** |
||
42 | * Gets the {@link Table} for this mapper |
||
43 | * |
||
44 | * @param string|Table $table |
||
45 | * Class name of the new instance or an existing instance. |
||
46 | * The default is the value of the <code>$_table</code> property. |
||
47 | * @return Table |
||
48 | * @throws Exception if <var>$dbTable</var> is not a <code>Table</code> |
||
49 | * @see Mapper::setDbTable() |
||
50 | */ |
||
51 | public function getDbTable($table = null) |
||
52 | { |
||
53 | if (is_null($this->_dbTable)) |
||
54 | { |
||
55 | if (is_null($table)) |
||
56 | { |
||
57 | $table = $this->_table; |
||
58 | } |
||
49 | PointedEar | 59 | |
27 | PointedEar | 60 | $this->setDbTable($table); |
61 | } |
||
49 | PointedEar | 62 | |
27 | PointedEar | 63 | return $this->_dbTable; |
64 | } |
||
49 | PointedEar | 65 | |
27 | PointedEar | 66 | /** |
49 | PointedEar | 67 | * Returns the <code>Table</code> for this object. |
68 | * |
||
69 | * @return Table |
||
70 | */ |
||
71 | public function getTable () |
||
72 | { |
||
73 | return $this->getDbTable(); |
||
74 | } |
||
75 | |||
76 | /** |
||
27 | PointedEar | 77 | * Sorts an array of objects by the property of the nested object. |
78 | * To be used with the u*sort() functions. |
||
79 | * |
||
80 | * @param object $a First operand of the comparison |
||
81 | * @param object $b Second operand of the comparison |
||
82 | * @param string $property |
||
83 | * Name of the property of the nested object by which to sort the outer array |
||
84 | * @return int |
||
85 | * 0 if <var>$a</var> and <var>$b</var> are "equal", |
||
86 | * <code>-1</code> if <var>$a</var> is "less" than <var>$b</var>, |
||
87 | * <code>1</code> otherwise. |
||
88 | */ |
||
89 | protected static function sortByProperty($a, $b, $property) |
||
90 | { |
||
91 | if ($a->$property === $b->$property) |
||
92 | { |
||
93 | return 0; |
||
94 | } |
||
49 | PointedEar | 95 | |
27 | PointedEar | 96 | return ($a->$property < $b->$property) ? -1 : 1; |
97 | } |
||
98 | } |