Subversion Repositories PHPX

Rev

Rev 52 | Details | Compare with Previous | Last modification | View Log | RSS feed

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