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