Subversion Repositories PHPX

Rev

Rev 52 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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