Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?PHP
2
/**
3
 * Base class for all models
4
 *
5
 * $Id: Model.php,v 1.5 2005/02/03 17:38:54 schst Exp $
6
 *
7
 * The base class provides __set() and __get()
8
 * as well as some other helper methods.
9
 *
10
 * @package Services_Ebay
11
 * @author  Stephan Schmidt <schst@php.net>
12
 *
13
 * @todo    different caches for different detail levels
14
 * @todo    add the possibility to disable the cache for single models
15
 */
16
class Services_Ebay_Model implements ArrayAccess
17
{
18
   /**
19
    * model type
20
    *
21
    * @var  string
22
    */
23
    protected $type = null;
24
 
25
   /**
26
    * properties of the model
27
    *
28
    * @var  array
29
    */
30
    protected $properties = array();
31
 
32
   /**
33
    * properties that are stored in eBay's database
34
    *
35
    * These are stored to check, which fields have been modified
36
    * in the item
37
    *
38
    * @var  array
39
    */
40
    protected $eBayProperties = array();
41
 
42
   /**
43
    * optional session, used to send API calls
44
    *
45
    * @var  object Services_Ebay_Session
46
    */
47
    protected $session;
48
 
49
   /**
50
    * property that stores the unique identifier (=pk) of the model
51
    *
52
    * @var string
53
    */
54
    protected $primaryKey = null;
55
 
56
   /**
57
    * store the static cache for all models of this type
58
    *
59
    * @var  object Services_Ebay_Cache
60
    */
61
    protected static $cache = null;
62
 
63
   /**
64
    * indicates, whether the model has been cached
65
    *
66
    */
67
    protected $cached = false;
68
 
69
    /**
70
    * create new model
71
    *
72
    * @param    array   properties
73
    */
74
    public function __construct($props, $session = null, $DetailLevel = 0)
75
    {
76
        $this->cached = false;
77
        if (is_array($props)) {
78
            $this->properties = $props;
79
        } elseif ($this->primaryKey !== null) {
80
            $this->properties[$this->primaryKey] = $props;
81
 
82
            // try loading the data from the cache
83
            if (is_object(self::$cache)) {
84
            	$cacheProps = self::$cache->load($this->type, $this->getPrimaryKey(), $DetailLevel);
85
            	if (is_array($cacheProps)) {
86
            		$this->properties = $cacheProps;
87
            		$this->cached = true;
88
            	}
89
            }
90
        }
91
 
92
        // store the session
93
        if( $session instanceof Services_Ebay_Session) {
94
            $this->session = $session;
95
        }
96
        $this->eBayProperties = $this->properties;
97
 
98
        if (!$this->isCached() && is_object(self::$cache)) {
99
        	self::$cache->store($this->type, $this->getPrimaryKey(), $DetailLevel, $this->properties);
100
        }
101
    }
102
 
103
   /**
104
    * check, whether the model has been cached
105
    *
106
    * @return   boolean
107
    */
108
    public function isCached()
109
    {
110
    	return $this->cached;
111
    }
112
 
113
   /**
114
    * set the session
115
    *
116
    * @param    object Services_Ebay_Session
117
    */
118
    public function setSession(Services_Ebay_Session $session)
119
    {
120
        $this->session = $session;
121
    }
122
 
123
   /**
124
    * set the cache
125
    *
126
    * @param    object Services_Ebay_Cache
127
    */
128
    static public function setCache(Services_Ebay_Cache $cache)
129
    {
130
        self::$cache = $cache;
131
    }
132
 
133
   /**
134
    * get a property
135
    *
136
    * @param    string   property name
137
    * @return   mixed    property value
138
    */
139
    public function __get($prop)
140
    {
141
        if (isset($this->properties[$prop])) {
142
            return $this->properties[$prop];
143
        }
144
    }
145
 
146
   /**
147
    * set a property
148
    *
149
    * @param    string   property name
150
    * @param    mixed    property value
151
    */
152
    public function __set($prop, $value)
153
    {
154
        $this->properties[$prop] = $value;
155
    }
156
 
157
   /**
158
    * return all properties of the user
159
    *
160
    * @return   array
161
    */
162
    public function toArray()
163
    {
164
        return $this->properties;
165
    }
166
 
167
   /**
168
    * get the properties that have been modified,
169
    * since the item has been fetched the last
170
    * time.
171
    *
172
    * This does not involve an API-call
173
    *
174
    * @return   array
175
    */
176
    public function GetModifiedProperties()
177
    {
178
        $modified = array();
179
        foreach ($this->properties as $key => $value) {
180
            if (!isset($this->eBayProperties[$key])) {
181
                $modified[$key] = $value;
182
                continue;
183
            }
184
            if ($this->eBayProperties[$key] === $value) {
185
                continue;
186
            }
187
            $modified[$key] = $value;
188
        }
189
        return $modified;
190
    }
191
 
192
   /**
193
	* check, whether a property exists
194
	*
195
	* This is needed to implement the ArrayAccess interface
196
	*
197
	* @param	string	property
198
	*/
199
	public function offsetExists($offset)
200
	{
201
	    if (isset($this->properties[$offset])) {
202
	    	return true;
203
	    }
204
	    return false;
205
	}
206
 
207
   /**
208
	* get a property
209
	*
210
	* This is needed to implement the ArrayAccess interface
211
	*
212
	* @param	string	property
213
	*/
214
	public function offsetGet($offset)
215
	{
216
		return $this->properties[$offset];
217
	}
218
 
219
   /**
220
	* set a property
221
	*
222
	* This is needed to implement the ArrayAccess interface
223
	*
224
	* @param	string	property
225
	* @param	mixed	value
226
	*/
227
	public function offsetSet($offset, $value)
228
	{
229
		$this->properties[$offset] = $value;
230
	}
231
 
232
   /**
233
	* unset a property
234
	*
235
	* This is needed to implement the ArrayAccess interface
236
	*
237
	* @param	string	property
238
	*/
239
	public function offsetUnset($offset)
240
	{
241
		unset($this->properties[$offset]);
242
	}
243
 
244
   /**
245
    * get the primary key of the model
246
    *
247
    * @return   string
248
    */
249
	public function getPrimaryKey()
250
	{
251
		if ($this->primaryKey === null) {
252
			return false;
253
		}
254
		if (!isset($this->properties[$this->primaryKey])) {
255
			return false;
256
		}
257
		return $this->properties[$this->primaryKey];
258
	}
259
}
260
?>