Rev 27 | Rev 49 | 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 | |||
| 3 | /** | 
        ||
| 4 | * Interface to be implemented if the model should be localizable | 
        ||
| 5 | */ | 
        ||
| 6 | interface ILocalizable | 
        ||
| 7 | { | 
        ||
| 8 |   /** | 
        ||
| 9 |    * Localizes this model.  The actual implementation is left to the model class | 
        ||
| 10 |    * implementing this interface. | 
        ||
| 11 |    */ | 
        ||
| 12 | function localize();  | 
        ||
| 13 | } | 
        ||
| 14 | |||
| 15 | /** | 
        ||
| 16 |  * Abstract model class | 
        ||
| 17 |  * | 
        ||
| 18 |  * Provides basic setters and getters for protected/private properties | 
        ||
| 19 |  * and a constructor to initialize properties using setters and getters. | 
        ||
| 20 |  * | 
        ||
| 21 |  * @author Thomas Lahn | 
        ||
| 22 |  */ | 
        ||
| 23 | abstract class AbstractModel  | 
        ||
| 24 | { | 
        ||
| 25 |   /** | 
        ||
| 26 |    * Creates a new model object | 
        ||
| 27 |    * | 
        ||
| 28 |    * @param array $data     Initialization data (optional) | 
        ||
| 29 |    * @param array $mapping  Mapping for initialization data (optional) | 
        ||
| 30 |    */ | 
        ||
| 31 | public function __construct(array $data = null, array $mapping = null)  | 
        ||
| 32 |   { | 
        ||
| 33 | if (!is_null($data))  | 
        ||
| 34 |     { | 
        ||
| 35 | $this->map($data, $mapping);  | 
        ||
| 36 |     } | 
        ||
| 37 |   } | 
        ||
| 38 | |||
| 39 |   /** | 
        ||
| 40 |    * Getter for properties | 
        ||
| 41 |    * | 
        ||
| 42 |    * @param string $name | 
        ||
| 43 |    * @throws ModelPropertyException | 
        ||
| 44 |    * @return mixed | 
        ||
| 45 |    */ | 
        ||
| 46 | public function __get($name)  | 
        ||
| 47 |   { | 
        ||
| 48 |     /* Support for Object-Relational Mappers */ | 
        ||
| 49 | if (strpos($name, 'persistent') === 0)  | 
        ||
| 50 |     { | 
        ||
| 51 | $class = get_class($this);  | 
        ||
| 52 | return $class::${$name};  | 
        ||
| 53 |     } | 
        ||
| 54 | |||
| 55 | $method = 'get' . ucfirst($name);  | 
        ||
| 56 | |||
| 57 | if (method_exists($this, $method))  | 
        ||
| 58 |     { | 
        ||
| 59 | return $this->$method();  | 
        ||
| 60 |     } | 
        ||
| 61 | |||
| 62 | if (property_exists($this, "_$name"))  | 
        ||
| 63 |     { | 
        ||
| 64 | return $this->{"_$name"};  | 
        ||
| 65 |     } | 
        ||
| 66 | |||
| 67 | return $this->$name;  | 
        ||
| 68 |   } | 
        ||
| 69 | |||
| 70 |   /** | 
        ||
| 71 |    * Setter for properties | 
        ||
| 72 |    * | 
        ||
| 73 |    * @param string $name | 
        ||
| 74 |    * @param mixed $value  The new property value before assignment | 
        ||
| 75 |    * @throws ModelPropertyException | 
        ||
| 76 |    */ | 
        ||
| 77 | public function __set($name, $value)  | 
        ||
| 78 |   { | 
        ||
| 79 | $method = 'set' . ucfirst($name);  | 
        ||
| 80 | |||
| 81 | if (method_exists($this, $method))  | 
        ||
| 82 |     { | 
        ||
| 83 | return $this->$method($value);  | 
        ||
| 84 |     } | 
        ||
| 85 | |||
| 86 | if (property_exists($this, "_$name"))  | 
        ||
| 87 |     { | 
        ||
| 88 | $this->{"_$name"} = $value;  | 
        ||
| 89 | return $this->{"_$name"};  | 
        ||
| 90 |     } | 
        ||
| 91 | |||
| 92 |     /* NOTE: Attempts to set other properties are _silently_ _ignored_ */ | 
        ||
| 93 |   } | 
        ||
| 94 | |||
| 95 |   /** | 
        ||
| 96 |    * Returns <code>true</code> if a variable name is a property variable name | 
        ||
| 97 |    * (starts with <tt>$_</tt>), <code>false</code> otherwise. | 
        ||
| 98 |    * | 
        ||
| 99 |    * @param string $varName | 
        ||
| 100 |    * @return boolean | 
        ||
| 101 |    * @see getPropertyVars() | 
        ||
| 102 |    */ | 
        ||
| 103 | private static function _isPropertyVar($varName)  | 
        ||
| 104 |   { | 
        ||
| 105 | return preg_match('/^_\\w/', $varName) > 0;  | 
        ||
| 106 |   } | 
        ||
| 107 | |||
| 108 |   /** | 
        ||
| 109 |    * Returns <code>true</code> if a variable name is a property variable name | 
        ||
| 110 |    * (starts with <tt>$_</tt>), <code>false</code> otherwise. | 
        ||
| 111 |    * | 
        ||
| 112 |    * @param string $varName | 
        ||
| 113 |    * @return string | 
        ||
| 114 |    * @see getPropertyVars() | 
        ||
| 115 |    */ | 
        ||
| 116 | private static function _toPropertyVar($varName)  | 
        ||
| 117 |   { | 
        ||
| 118 | return preg_replace('/^_(\\w)/', '\\1', $varName);  | 
        ||
| 119 |   } | 
        ||
| 120 | |||
| 121 |   /** | 
        ||
| 122 |    * Returns the public names of the property variables of a {@link Model} | 
        ||
| 123 |    * as an array of strings | 
        ||
| 124 |    * | 
        ||
| 125 |    * @return array | 
        ||
| 126 |    */ | 
        ||
| 127 | public function getPropertyVars()  | 
        ||
| 128 |   { | 
        ||
| 129 | return array_map(  | 
        ||
| 130 | array('self', '_toPropertyVar'),  | 
        ||
| 131 | array_filter(  | 
        ||
| 132 | array_keys(get_object_vars($this)),  | 
        ||
| 133 | array('self', '_isPropertyVar')  | 
        ||
| 134 |       ) | 
        ||
| 135 | );  | 
        ||
| 136 |   } | 
        ||
| 137 | |||
| 138 |   /** | 
        ||
| 139 |    * Maps the values of an associative array to a model object | 
        ||
| 140 |    * | 
        ||
| 141 |    * @param array $data | 
        ||
| 35 | PointedEar | 142 |    *   Data to be mapped to properties of the object | 
        
| 27 | PointedEar | 143 |    * @param array $mapping = null | 
        
| 144 |    *   <p>If <var>$mapping</var> is not provided, or <code>null</code> (default), | 
        ||
| 145 |    *   the values of <var>$data</var> are mapped to properties of | 
        ||
| 146 |    *   the model object as specified by the keys of <var>$data</var>.</p> | 
        ||
| 147 |    *   <p>If <var>$mapping</var> is provided and an array, the keys of | 
        ||
| 148 |    *   <var>$data</var> are mapped to properties as specified by | 
        ||
| 149 |    *   the corresponding values of <var>$mapping</var>.  If a value of | 
        ||
| 150 |    *   <var>$mapping</var> is <code>null</code>, the corresponding value | 
        ||
| 151 |    *   in <var>$data</var> is not mapped; if a key is missing in | 
        ||
| 152 |    *   <var>$mapping</var>, the value is mapped as if <var>$mapping</var> | 
        ||
| 153 |    *   was <code>null</code>.</p> | 
        ||
| 35 | PointedEar | 154 |    * @param bool $exclusive = false | 
        
| 155 |    *   <p>If <code>true</code>, <em>only</em> the keys of $data that are present | 
        ||
| 156 |    *   in $mapping are mapped.</p> | 
        ||
| 27 | PointedEar | 157 |    */ | 
        
| 35 | PointedEar | 158 | public function map(array $data, array $mapping = null, $exclusive = false)  | 
        
| 27 | PointedEar | 159 |   { | 
        
| 160 | if (is_null($mapping))  | 
        ||
| 161 |     { | 
        ||
| 162 | foreach ($data as $key => $value)  | 
        ||
| 163 |       { | 
        ||
| 164 | $this->$key = $value;  | 
        ||
| 165 |       } | 
        ||
| 166 |     } | 
        ||
| 35 | PointedEar | 167 |     else | 
        
| 27 | PointedEar | 168 |     { | 
        
| 169 | foreach ($data as $key => $value)  | 
        ||
| 170 |       { | 
        ||
| 171 | if (array_key_exists($key, $mapping))  | 
        ||
| 172 |         { | 
        ||
| 173 | if ($exclusive || !is_null($mapping[$key]))  | 
        ||
| 174 |           { | 
        ||
| 175 | $this->{$mapping[$key]} = $value;  | 
        ||
| 176 |           } | 
        ||
| 177 |         } | 
        ||
| 178 |         else | 
        ||
| 179 |         { | 
        ||
| 180 | $this->$key = $value;  | 
        ||
| 181 |         } | 
        ||
| 182 |       } | 
        ||
| 183 |     } | 
        ||
| 184 |   } | 
        ||
| 185 | } |