Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*******************************************************************************
3
 *  Copyright 2011 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4
 *  Licensed under the Apache License, Version 2.0 (the "License");
5
 *
6
 *  You may not use this file except in compliance with the License.
7
 *  You may obtain a copy of the License at: http://aws.amazon.com/apache2.0
8
 *  This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
9
 *  CONDITIONS OF ANY KIND, either express or implied. See the License for the
10
 *  specific language governing permissions and limitations under the License.
11
 * *****************************************************************************
12
 */
13
 
14
 
15
/**
16
 * CheckoutByAmazon_Service_Model - base class for all model classes
17
 */
18
abstract class CheckoutByAmazon_Service_Model
19
{
20
 
21
    /** @var array */
22
    protected  $_fields = array ();
23
 
24
    /**
25
     * Construct new model class
26
     *
27
     * @param mixed $data - DOMElement or Associative Array to construct from.
28
     */
29
    public function __construct($data = null)
30
    {
31
        if (!is_null($data)) {
32
            if ($this->_isAssociativeArray($data)) {
33
                $this->_fromAssociativeArray($data);
34
            } elseif ($this->_isDOMElement($data)) {
35
                $this->_fromDOMElement($data);
36
            } else {
37
                throw new Exception ("Unable to construct from provided data.
38
                                Please be sure to pass associative array or DOMElement");
39
            }
40
 
41
        }
42
    }
43
 
44
    /**
45
     * Support for virtual properties getters.
46
     *
47
     * Virtual property call example:
48
     *
49
     *   $action->Property
50
     *
51
     * Direct getter(preferred):
52
     *
53
     *   $action->getProperty()
54
     *
55
     * @param string $propertyName name of the property
56
     */
57
    public function __get($propertyName)
58
    {
59
       $getter = "get$propertyName";
60
       return $this->$getter();
61
    }
62
 
63
    /**
64
     * Support for virtual properties setters.
65
     *
66
     * Virtual property call example:
67
     *
68
     *   $action->Property  = 'ABC'
69
     *
70
     * Direct setter (preferred):
71
     *
72
     *   $action->setProperty('ABC')
73
     *
74
     * @param string $propertyName name of the property
75
     */
76
    public function __set($propertyName, $propertyValue)
77
    {
78
       $setter = "set$propertyName";
79
       $this->$setter($propertyValue);
80
       return $this;
81
    }
82
 
83
 
84
    /**
85
     * XML fragment representation of this object
86
     * Note, name of the root determined by caller
87
     * This fragment returns inner fields representation only
88
     * @return string XML fragment for this object
89
     */
90
    protected function _toXMLFragment()
91
    {
92
        $xml = "";
93
        foreach ($this->_fields as $fieldName => $field) {
94
            $fieldValue = $field['FieldValue'];
95
            if (!is_null($fieldValue)) {
96
                $fieldType = $field['FieldType'];
97
                if (is_array($fieldType)) {
98
                    if ($this->_isComplexType($fieldType[0])) {
99
                        foreach ($fieldValue as $Item) {
100
                            $xml .= "<$fieldName>";
101
                            $xml .= $Item->_toXMLFragment();
102
                            $xml .= "</$fieldName>";
103
                        }
104
                    } else {
105
                        foreach ($fieldValue as $Item) {
106
                            $xml .= "<$fieldName>";
107
                            $xml .= $this->_escapeXML($Item);
108
                            $xml .= "</$fieldName>";
109
                        }
110
                    }
111
                } else {
112
                    if ($this->_isComplexType($fieldType)) {
113
                        $xml .= "<$fieldName>";
114
                        $xml .= $fieldValue->_toXMLFragment();
115
                        $xml .= "</$fieldName>";
116
                    } else {
117
                        $xml .= "<$fieldName>";
118
                        $xml .= $this->_escapeXML($fieldValue);
119
                        $xml .= "</$fieldName>";
120
                    }
121
                }
122
            }
123
        }
124
        return $xml;
125
    }
126
 
127
 
128
    /**
129
     * Escape special XML characters
130
     * @return string with escaped XML characters
131
     */
132
    private function _escapeXML($str)
133
    {
134
        $from = array( "&", "<", ">", "'", "\"");
135
        $to = array( "&amp;", "&lt;", "&gt;", "&#039;", "&quot;");
136
        return str_replace($from, $to, $str);
137
    }
138
 
139
 
140
 
141
    /**
142
     * Construct from DOMElement
143
     *
144
     * This function iterates over object fields and queries XML
145
     * for corresponding tag value. If query succeeds, value extracted
146
     * from xml, and field value properly constructed based on field type.
147
     *
148
     * Field types defined as arrays always constructed as arrays,
149
     * even if XML contains a single element - to make sure that
150
     * data structure is predictable, and no is_array checks are
151
     * required.
152
     *
153
     * @param DOMElement $dom XML element to construct from
154
     */
155
    private function _fromDOMElement(DOMElement $dom)
156
    {
157
        $xpath = new DOMXPath($dom->ownerDocument);
158
        $xpath->registerNamespace('a','http://payments.amazon.com/checkout/v2/2010-08-31/');
159
 
160
        foreach ($this->_fields as $fieldName => $field) {
161
            $fieldType = $field['FieldType'];
162
            if (is_array($fieldType)) {
163
                if ($this->_isComplexType($fieldType[0])) {
164
                    $elements = $xpath->query("./a:$fieldName", $dom);
165
                    if ($elements->length >= 1) {
166
                        require_once (str_replace('_', DIRECTORY_SEPARATOR, $fieldType[0]) . ".php");
167
                        foreach ($elements as $element) {
168
                            $this->_fields[$fieldName]['FieldValue'][] = new $fieldType[0]($element);
169
                        }
170
                    }
171
                } else {
172
                    $elements = $xpath->query("./a:$fieldName", $dom);
173
                    if ($elements->length >= 1) {
174
                        foreach ($elements as $element) {
175
                            $Text = $xpath->query('./text()', $element);
176
                            $this->_fields[$fieldName]['FieldValue'][] = $Text->item(0)->data;
177
                        }
178
                    }
179
                }
180
            } else {
181
                if ($this->_isComplexType($fieldType)) {
182
                    $elements = $xpath->query("./a:$fieldName", $dom);
183
                    if ($elements->length == 1) {
184
                        require_once (str_replace('_', DIRECTORY_SEPARATOR, $fieldType) . ".php");
185
                        $this->_fields[$fieldName]['FieldValue'] = new $fieldType($elements->item(0));
186
                    }
187
                } else {
188
                    $element = $xpath->query("./a:$fieldName/text()", $dom);
189
                    if ($element->length == 1) {
190
                        $this->_fields[$fieldName]['FieldValue'] = $element->item(0)->data;
191
                    }
192
                }
193
            }
194
        }
195
    }
196
 
197
 
198
    /**
199
     * Construct from Associative Array
200
     *
201
     *
202
     * @param array $array associative array to construct from
203
     */
204
    private function _fromAssociativeArray(array $array)
205
    {
206
        foreach ($this->_fields as $fieldName => $field) {
207
            $fieldType = $field['FieldType'];
208
            if (is_array($fieldType)) {
209
                if ($this->_isComplexType($fieldType[0])) {
210
                    if (array_key_exists($fieldName, $array)) {
211
                        $elements = $array[$fieldName];
212
                        if (!$this->_isNumericArray($elements)) {
213
                            $elements =  array($elements);
214
                        }
215
                        if (count ($elements) >= 1) {
216
                            require_once (str_replace('_', DIRECTORY_SEPARATOR, $fieldType[0]) . ".php");
217
                            foreach ($elements as $element) {
218
                                $this->_fields[$fieldName]['FieldValue'][] = new $fieldType[0]($element);
219
                            }
220
                        }
221
                    }
222
                } else {
223
                    if (array_key_exists($fieldName, $array)) {
224
                        $elements = $array[$fieldName];
225
                        if (!$this->_isNumericArray($elements)) {
226
                            $elements =  array($elements);
227
                            }
228
                        if (count ($elements) >= 1) {
229
                            foreach ($elements as $element) {
230
                                $this->_fields[$fieldName]['FieldValue'][] = $element;
231
                            }
232
                        }
233
                    }
234
                }
235
            } else {
236
                if ($this->_isComplexType($fieldType)) {
237
                    if (array_key_exists($fieldName, $array)) {
238
                        require_once (str_replace('_', DIRECTORY_SEPARATOR, $fieldType) . ".php");
239
                        $this->_fields[$fieldName]['FieldValue'] = new $fieldType($array[$fieldName]);
240
                    }
241
                } else {
242
                    if (array_key_exists($fieldName, $array)) {
243
                        $this->_fields[$fieldName]['FieldValue'] = $array[$fieldName];
244
                    }
245
                }
246
            }
247
        }
248
    }
249
 
250
 
251
 
252
    /**
253
     * Determines if field is complex type
254
     *
255
     * @param string $fieldType field type name
256
     */
257
    private function _isComplexType ($fieldType)
258
    {
259
        return preg_match('/^CheckoutByAmazon_Service_Model_/', $fieldType);
260
    }
261
 
262
   /**
263
    * Checks  whether passed variable is an associative array
264
    *
265
    * @param mixed $var
266
    * @return TRUE if passed variable is an associative array
267
    */
268
    private function _isAssociativeArray($var) {
269
        return is_array($var) && array_keys($var) !== range(0, sizeof($var) - 1);
270
    }
271
 
272
   /**
273
    * Checks  whether passed variable is DOMElement
274
    *
275
    * @param mixed $var
276
    * @return TRUE if passed variable is DOMElement
277
    */
278
    private function _isDOMElement($var) {
279
        return $var instanceof DOMElement;
280
    }
281
 
282
   /**
283
    * Checks  whether passed variable is numeric array
284
    *
285
    * @param mixed $var
286
    * @return TRUE if passed variable is an numeric array
287
    */
288
    protected function _isNumericArray($var) {
289
        return is_array($var) && array_keys($var) === range(0, sizeof($var) - 1);
290
    }
291
}
292
 
293
?>