Subversion Repositories PHPX

Rev

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

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