Subversion Repositories PHPX

Rev

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

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