Subversion Repositories PHPX

Compare Revisions

Last modification

Ignore whitespace Rev 48 → Rev 49

/trunk/Db/Mapper.php
1,5 → 1,6
<?php
 
require_once __DIR__ . '/../AbstractModel.php';
require_once __DIR__ . '/Table.php';
 
/**
7,7 → 8,7
*
* @author Thomas Lahn
*/
abstract class Mapper
abstract class Mapper extends AbstractModel
{
/**
* Class name of the associated table model
15,9 → 16,9
* @var string
*/
protected $_table = 'Table';
 
protected $_dbTable;
 
/**
* Sets the {@link Table} for this mapper
* @param string|Table $dbTable
30,11 → 31,11
{
$table = new $table();
}
 
if (!($table instanceof Table)) {
throw new Exception('Invalid table data gateway provided');
}
 
$this->_dbTable = $table;
}
 
56,14 → 57,24
{
$table = $this->_table;
}
 
$this->setDbTable($table);
}
 
return $this->_dbTable;
}
 
/**
* Returns the <code>Table</code> for this object.
*
* @return Table
*/
public function getTable ()
{
return $this->getDbTable();
}
 
/**
* Sorts an array of objects by the property of the nested object.
* To be used with the u*sort() functions.
*
82,7 → 93,7
{
return 0;
}
 
return ($a->$property < $b->$property) ? -1 : 1;
}
}
/trunk/AbstractModel.php
28,7 → 28,7
* @param array $data Initialization data (optional)
* @param array $mapping Mapping for initialization data (optional)
*/
public function __construct(array $data = null, array $mapping = null)
protected function __construct(array $data = null, array $mapping = null)
{
if (!is_null($data))
{
35,7 → 35,7
$this->map($data, $mapping);
}
}
 
/**
* Getter for properties
*
51,22 → 51,22
$class = get_class($this);
return $class::${$name};
}
 
$method = 'get' . ucfirst($name);
 
if (method_exists($this, $method))
{
return $this->$method();
}
 
if (property_exists($this, "_$name"))
{
return $this->{"_$name"};
}
 
return $this->$name;
}
 
/**
* Setter for properties
*
77,18 → 77,18
public function __set($name, $value)
{
$method = 'set' . ucfirst($name);
 
if (method_exists($this, $method))
{
return $this->$method($value);
}
 
if (property_exists($this, "_$name"))
{
$this->{"_$name"} = $value;
return $this->{"_$name"};
}
 
/* NOTE: Attempts to set other properties are _silently_ _ignored_ */
}
 
104,7 → 104,7
{
return preg_match('/^_\\w/', $varName) > 0;
}
 
/**
* Returns <code>true</code> if a variable name is a property variable name
* (starts with <tt>$_</tt>), <code>false</code> otherwise.
117,7 → 117,7
{
return preg_replace('/^_(\\w)/', '\\1', $varName);
}
 
/**
* Returns the public names of the property variables of a {@link Model}
* as an array of strings
134,7 → 134,7
)
);
}
 
/**
* Maps the values of an associative array to a model object
*
154,6 → 154,8
* @param bool $exclusive = false
* <p>If <code>true</code>, <em>only</em> the keys of $data that are present
* in $mapping are mapped.</p>
* @return AbstractModel
* The modified object
*/
public function map(array $data, array $mapping = null, $exclusive = false)
{
181,5 → 183,7
}
}
}
 
return $this;
}
}