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