Subversion Repositories PHPX

Rev

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

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