Subversion Repositories PHPX

Rev

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