Subversion Repositories PHPX

Rev

Rev 27 | Rev 51 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 27 Rev 35
Line 137... Line 137...
137
 
137
 
138
  /**
138
  /**
139
   * Maps the values of an associative array to a model object
139
   * Maps the values of an associative array to a model object
140
   *
140
   *
141
   * @param array $data
141
   * @param array $data
-
 
142
   *   Data to be mapped to properties of the object
142
   * @param array $mapping = null
143
   * @param array $mapping = null
143
   *   <p>If <var>$mapping</var> is not provided, or <code>null</code> (default),
144
   *   <p>If <var>$mapping</var> is not provided, or <code>null</code> (default),
144
   *   the values of <var>$data</var> are mapped to properties of
145
   *   the values of <var>$data</var> are mapped to properties of
145
   *   the model object as specified by the keys of <var>$data</var>.</p>
146
   *   the model object as specified by the keys of <var>$data</var>.</p>
146
   *   <p>If <var>$mapping</var> is provided and an array, the keys of
147
   *   <p>If <var>$mapping</var> is provided and an array, the keys of
Line 148... Line 149...
148
   *   the corresponding values of <var>$mapping</var>.  If a value of
149
   *   the corresponding values of <var>$mapping</var>.  If a value of
149
   *   <var>$mapping</var> is <code>null</code>, the corresponding value
150
   *   <var>$mapping</var> is <code>null</code>, the corresponding value
150
   *   in <var>$data</var> is not mapped; if a key is missing in
151
   *   in <var>$data</var> is not mapped; if a key is missing in
151
   *   <var>$mapping</var>, the value is mapped as if <var>$mapping</var>
152
   *   <var>$mapping</var>, the value is mapped as if <var>$mapping</var>
152
   *   was <code>null</code>.</p>
153
   *   was <code>null</code>.</p>
153
   * @param bool $exclusive
154
   * @param bool $exclusive = false
154
   *   If <code>true</code>, <em>only</em> the keys of $data that are present
155
   *   <p>If <code>true</code>, <em>only</em> the keys of $data that are present
155
   *   in $mapping are mapped.
156
   *   in $mapping are mapped.</p>
156
   * @throws InvalidArgumentException if <var>$mapping</var> is neither
-
 
157
   *   <code>null</code> nor an array.
-
 
158
   */
157
   */
159
  public function map($data, $mapping = null, $exclusive = false)
158
  public function map(array $data, array $mapping = null, $exclusive = false)
160
  {
159
  {
161
    if (is_null($mapping))
160
    if (is_null($mapping))
162
    {
161
    {
163
      foreach ($data as $key => $value)
162
      foreach ($data as $key => $value)
164
      {
163
      {
165
        $this->$key = $value;
164
        $this->$key = $value;
166
      }
165
      }
167
    }
166
    }
168
    else if (is_array($mapping))
167
    else
169
    {
168
    {
170
      foreach ($data as $key => $value)
169
      foreach ($data as $key => $value)
171
      {
170
      {
172
        if (array_key_exists($key, $mapping))
171
        if (array_key_exists($key, $mapping))
173
        {
172
        {
Line 180... Line 179...
180
        {
179
        {
181
          $this->$key = $value;
180
          $this->$key = $value;
182
        }
181
        }
183
      }
182
      }
184
    }
183
    }
185
    else
-
 
186
    {
-
 
187
      throw new InvalidArgumentException(
-
 
188
        'Expected null or array for $mapping, saw <pre>'
-
 
189
        . print_r($mapping, true) . '</pre>');
-
 
190
    }
-
 
191
  }
184
  }
192
}
185
}
193
186