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