Subversion Repositories PHPX

Rev

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