Rev 54 | 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 | |||
| 51 | PointedEar | 3 | namespace PointedEars\PHPX; |
| 4 | |||
| 27 | PointedEar | 5 | /** |
| 6 | * Interface to be implemented if the model should be localizable |
||
| 7 | */ |
||
| 8 | interface ILocalizable |
||
| 9 | { |
||
| 10 | /** |
||
| 52 | PointedEar | 11 | * Localizes this model. The actual implementation is left to |
| 12 | * the model class implementing this interface. |
||
| 27 | PointedEar | 13 | */ |
| 52 | PointedEar | 14 | function localize (); |
| 27 | PointedEar | 15 | } |
| 16 | |||
| 17 | /** |
||
| 18 | * Abstract model class |
||
| 19 | * |
||
| 59 | PointedEar | 20 | * Provides a constructor to initialize properties using setters and getters. |
| 27 | PointedEar | 21 | * |
| 22 | * @author Thomas Lahn |
||
| 23 | */ |
||
| 59 | PointedEar | 24 | abstract class AbstractModel extends Base |
| 27 | PointedEar | 25 | { |
| 26 | /** |
||
| 27 | * Creates a new model object |
||
| 28 | * |
||
| 29 | * @param array $data Initialization data (optional) |
||
| 30 | * @param array $mapping Mapping for initialization data (optional) |
||
| 31 | */ |
||
| 52 | PointedEar | 32 | protected function __construct (array $data = null, array $mapping = null) |
| 27 | PointedEar | 33 | { |
| 34 | if (!is_null($data)) |
||
| 35 | { |
||
| 36 | $this->map($data, $mapping); |
||
| 37 | } |
||
| 38 | } |
||
| 49 | PointedEar | 39 | |
| 27 | PointedEar | 40 | /** |
| 41 | * Returns <code>true</code> if a variable name is a property variable name |
||
| 42 | * (starts with <tt>$_</tt>), <code>false</code> otherwise. |
||
| 43 | * |
||
| 44 | * @param string $varName |
||
| 45 | * @return boolean |
||
| 46 | * @see getPropertyVars() |
||
| 47 | */ |
||
| 52 | PointedEar | 48 | private static function _isPropertyVar ($varName) |
| 27 | PointedEar | 49 | { |
| 50 | return preg_match('/^_\\w/', $varName) > 0; |
||
| 51 | } |
||
| 49 | PointedEar | 52 | |
| 27 | PointedEar | 53 | /** |
| 54 | * Returns <code>true</code> if a variable name is a property variable name |
||
| 55 | * (starts with <tt>$_</tt>), <code>false</code> otherwise. |
||
| 56 | * |
||
| 57 | * @param string $varName |
||
| 58 | * @return string |
||
| 59 | * @see getPropertyVars() |
||
| 60 | */ |
||
| 52 | PointedEar | 61 | private static function _toPropertyVar ($varName) |
| 27 | PointedEar | 62 | { |
| 63 | return preg_replace('/^_(\\w)/', '\\1', $varName); |
||
| 64 | } |
||
| 49 | PointedEar | 65 | |
| 27 | PointedEar | 66 | /** |
| 67 | * Returns the public names of the property variables of a {@link Model} |
||
| 68 | * as an array of strings |
||
| 69 | * |
||
| 70 | * @return array |
||
| 71 | */ |
||
| 52 | PointedEar | 72 | public function getPropertyVars () |
| 27 | PointedEar | 73 | { |
| 74 | return array_map( |
||
| 75 | array('self', '_toPropertyVar'), |
||
| 76 | array_filter( |
||
| 77 | array_keys(get_object_vars($this)), |
||
| 78 | array('self', '_isPropertyVar') |
||
| 79 | ) |
||
| 80 | ); |
||
| 81 | } |
||
| 49 | PointedEar | 82 | |
| 27 | PointedEar | 83 | /** |
| 84 | * Maps the values of an associative array to a model object |
||
| 85 | * |
||
| 86 | * @param array $data |
||
| 35 | PointedEar | 87 | * Data to be mapped to properties of the object |
| 27 | PointedEar | 88 | * @param array $mapping = null |
| 89 | * <p>If <var>$mapping</var> is not provided, or <code>null</code> (default), |
||
| 90 | * the values of <var>$data</var> are mapped to properties of |
||
| 91 | * the model object as specified by the keys of <var>$data</var>.</p> |
||
| 92 | * <p>If <var>$mapping</var> is provided and an array, the keys of |
||
| 93 | * <var>$data</var> are mapped to properties as specified by |
||
| 94 | * the corresponding values of <var>$mapping</var>. If a value of |
||
| 95 | * <var>$mapping</var> is <code>null</code>, the corresponding value |
||
| 96 | * in <var>$data</var> is not mapped; if a key is missing in |
||
| 97 | * <var>$mapping</var>, the value is mapped as if <var>$mapping</var> |
||
| 98 | * was <code>null</code>.</p> |
||
| 35 | PointedEar | 99 | * @param bool $exclusive = false |
| 100 | * <p>If <code>true</code>, <em>only</em> the keys of $data that are present |
||
| 101 | * in $mapping are mapped.</p> |
||
| 49 | PointedEar | 102 | * @return AbstractModel |
| 103 | * The modified object |
||
| 27 | PointedEar | 104 | */ |
| 52 | PointedEar | 105 | public function map (array $data, array $mapping = null, $exclusive = false) |
| 27 | PointedEar | 106 | { |
| 107 | if (is_null($mapping)) |
||
| 108 | { |
||
| 109 | foreach ($data as $key => $value) |
||
| 110 | { |
||
| 111 | $this->$key = $value; |
||
| 112 | } |
||
| 113 | } |
||
| 35 | PointedEar | 114 | else |
| 27 | PointedEar | 115 | { |
| 116 | foreach ($data as $key => $value) |
||
| 117 | { |
||
| 118 | if (array_key_exists($key, $mapping)) |
||
| 119 | { |
||
| 120 | if ($exclusive || !is_null($mapping[$key])) |
||
| 121 | { |
||
| 122 | $this->{$mapping[$key]} = $value; |
||
| 123 | } |
||
| 124 | } |
||
| 125 | else |
||
| 126 | { |
||
| 127 | $this->$key = $value; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | } |
||
| 49 | PointedEar | 131 | |
| 132 | return $this; |
||
| 27 | PointedEar | 133 | } |
| 134 | } |