Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * @package PayPal
4
 *
5
 * $Id: Profile.php 696 2011-09-08 09:08:23Z tiefland $
6
 */
7
 
8
/**
9
 * Base package class.
10
 */
11
require_once 'PayPal.php';
12
 
13
/**
14
 * Base class for PayPal Profiles, managaes interaction with handlers, etc.
15
 *
16
 * @package PayPal
17
 * @abstract
18
 */
19
class Profile
20
{
21
    /**
22
     * Which environment should API calls be made against?
23
     *
24
     * @see $_validEnvironments
25
     *
26
     * @access protected
27
     */
28
    var $_environment;
29
 
30
    /**
31
     * The list of valid environments that API calls can be executed
32
     * against.
33
     *
34
     * @access protected
35
     */
36
    var $_validEnvironments = array();
37
 
38
    /**
39
     * The ProfileHandler instance associated with this Profile.
40
     *
41
     * @access protected
42
     */
43
    var $_handler;
44
 
45
    /**
46
     * The ProfileHandler ID.
47
     *
48
     * @access protected
49
     */
50
    var $_handler_id;
51
 
52
    /**
53
     * Base constructor which creates a default handler if none was
54
     * provided.
55
     *
56
     * @param string The name of the profile
57
     * @param object An optional handler to store the profile in
58
     */
59
    function Profile($id, &$handler)
60
    {
61
        $this->_handler =& $handler;
62
        $this->_handler_id = $id;
63
    }
64
 
65
    /**
66
     * Loads the profile data from the defined handler.
67
     *
68
     * @return mixed true on success or a PayPal_Error object on failure
69
     * @final
70
     * @access private
71
     */
72
    function _load()
73
    {
74
        $loaded_data = $this->_handler->loadProfile($this->_handler_id);
75
        $expected_keys = $this->_getSerializeList();
76
 
77
        if (!is_a($this->_handler, 'ProfileHandler_Array')) {
78
            $expected_keys[] = 'classname';
79
        }
80
 
81
        if (PayPal::isError($loaded_data)) {
82
            foreach ($expected_keys as $key) {
83
                $ikey = "_$key";
84
                $this->$ikey = null;
85
            }
86
 
87
            return PayPal::raiseError("Could not load data for non-existant profile '{$this->_handler_id}'");
88
        }
89
 
90
        $expected_keys = array_flip($expected_keys);
91
        foreach ($loaded_data as $key => $value) {
92
            $ikey = "_$key";
93
            $this->$ikey = $value;
94
            unset($expected_keys[$key]);
95
        }
96
 
97
        if (!empty($expected_keys)) {
98
            $key_list = implode(', ', array_flip($expected_keys));
99
            return PayPal::raiseError("The following values were expected but not found in the profile data: $key_list");
100
        }
101
 
102
        return $this->loadEnvironments();
103
    }
104
 
105
    function getID()
106
    {
107
        return $this->_handler_id;
108
    }
109
 
110
    function getInstance($id, &$handler)
111
    {
112
        return PayPal::raiseError("Cannot return an instance of the base Profile class");
113
    }
114
 
115
    /**
116
     * Loads the environment names from the endpoint map.
117
     *
118
     * @return mixed True on success or a PayPal error object on failure
119
     */
120
    function loadEnvironments()
121
    {
122
        $version = PayPal::getWSDLVersion();
123
        $endpoints = PayPal::getEndpoints();
124
        if (PayPal::isError($endpoints)) {
125
            return $endpoints;
126
        }
127
 
128
        foreach ($endpoints as $range) {
129
            if ($version >= $range['min'] &&
130
                $version <= $range['max']) {
131
                foreach (array_keys($range['environments']) as $environment) {
132
                    $this->_validEnvironments[] = strtolower($environment);
133
                }
134
                return true;
135
            }
136
        }
137
 
138
        return PayPal::raiseError("Could not find any endpoint mappings for WSDL version '$version'");
139
    }
140
 
141
    /**
142
     * Saves the profile data to the defined handler.
143
     *
144
     * @return mixed true on success or a PayPal_Error object on failure
145
     * @final
146
     */
147
    function save()
148
    {
149
        $values = $this->_getSerializeList();
150
        foreach ($values as $value) {
151
            $ivalue = "_$value";
152
            if (isset($this->$ivalue)) {
153
                $data[$value] = $this->$ivalue;
154
            } else {
155
                $data[$value] = null;
156
            }
157
        }
158
 
159
        $data['classname'] = get_class($this);
160
 
161
        return $this->_handler->saveProfile($data, $this->_handler_id);
162
    }
163
 
164
    /**
165
     * Returns an array of member variables names which should be
166
     * included when storing the profile.
167
     *
168
     * @return array An array of member variable names which should be included
169
     * @access protected
170
     */
171
    function _getSerializeList()
172
    {
173
        return array();
174
    }
175
 
176
    /**
177
     * Set the environment associated with this profile.
178
     *
179
     * @param string True on success, a Paypal error object on failure
180
     */
181
    function setEnvironment($environment)
182
    {
183
        $environment = strtolower($environment);
184
 
185
        $envs = $this->getValidEnvironments();
186
        if (PayPal::isError($envs)) {
187
            return $envs;
188
        }
189
 
190
        if (in_array($environment, $envs)) {
191
            $this->_environment = $environment;
192
            return true;
193
        }
194
 
195
        return PayPal::raiseError("Invalid Environment Specified");
196
    }
197
 
198
    /**
199
     * Get the environment associated with the profile.
200
     *
201
     * @return string The environment associated with the profile.
202
     */
203
    function getEnvironment()
204
    {
205
        return strtolower($this->_environment);
206
    }
207
 
208
    /**
209
     * Returns an array of valid Environments
210
     *
211
     * @return array An array of valid environment names
212
     */
213
    function getValidEnvironments()
214
    {
215
        if (empty($this->_validEnvironments)) {
216
            $res = $this->loadEnvironments();
217
            if (PayPal::isError($res)) {
218
                return $res;
219
            }
220
        }
221
 
222
        return $this->_validEnvironments;
223
    }
224
 
225
}