Subversion Repositories PHPX

Rev

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