Subversion Repositories PHPX

Rev

Rev 27 | Rev 51 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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