8
* This source file is subject to the new BSD license that is bundled
9
* with this package in the file LICENSE.txt.
10
* It is also available through the world-wide-web at this URL:
11
* http://framework.zend.com/license/new-bsd
12
* If you did not receive a copy of the license and are unable to
13
* obtain it through the world-wide-web, please send an email
14
* to license@zend.com so we can send you a copy immediately.
19
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
20
* @license http://framework.zend.com/license/new-bsd New BSD License
24
* @see Zend_Gdata_App_Util
26
require_once 'Zend/Gdata/App/Util.php';
29
* Abstract class for all XML elements
34
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
35
* @license http://framework.zend.com/license/new-bsd New BSD License
37
abstract class Zend_Gdata_App_Base
41
* @var string The XML element name, including prefix if desired
43
protected $_rootElement = null;
46
* @var string The XML namespace prefix
48
protected $_rootNamespace = 'atom';
51
* @var string The XML namespace URI - takes precedence over lookup up the
52
* corresponding URI for $_rootNamespace
54
protected $_rootNamespaceURI = null;
57
* @var array Leftover elements which were not handled
59
protected $_extensionElements = array();
62
* @var array Leftover attributes which were not handled
64
protected $_extensionAttributes = array();
67
* @var string XML child text node content
69
protected $_text = null;
72
* @var array Memoized results from calls to lookupNamespace() to avoid
73
* expensive calls to getGreatestBoundedValue(). The key is in the
74
* form 'prefix-majorVersion-minorVersion', and the value is the
75
* output from getGreatestBoundedValue().
77
protected static $_namespaceLookupCache = array();
80
* List of namespaces, as a three-dimensional array. The first dimension
81
* represents the namespace prefix, the second dimension represents the
82
* minimum major protocol version, and the third dimension is the minimum
83
* minor protocol version. Null keys are NOT allowed.
85
* When looking up a namespace for a given prefix, the greatest version
86
* number (both major and minor) which is less than the effective version
89
* @see lookupNamespace()
90
* @see registerNamespace()
91
* @see registerAllNamespaces()
94
protected $_namespaces = array(
97
0 => 'http://www.w3.org/2005/Atom'
102
0 => 'http://purl.org/atom/app#'
105
0 => 'http://www.w3.org/2007/app'
110
public function __construct()
115
* Returns the child text node of this element
116
* This represents any raw text contained within the XML element
118
* @return string Child text node
120
public function getText($trim = true)
123
return trim($this->_text);
130
* Sets the child text node of this element
131
* This represents any raw text contained within the XML element
133
* @param string $value Child text node
134
* @return Zend_Gdata_App_Base Returns an object of the same type as 'this' to provide a fluent interface.
136
public function setText($value)
138
$this->_text = $value;
143
* Returns an array of all elements not matched to data model classes
144
* during the parsing of the XML
146
* @return array All elements not matched to data model classes during parsing
148
public function getExtensionElements()
150
return $this->_extensionElements;
154
* Sets an array of all elements not matched to data model classes
155
* during the parsing of the XML. This method can be used to add arbitrary
156
* child XML elements to any data model class.
158
* @param array $value All extension elements
159
* @return Zend_Gdata_App_Base Returns an object of the same type as 'this' to provide a fluent interface.
161
public function setExtensionElements($value)
163
$this->_extensionElements = $value;
168
* Returns an array of all extension attributes not transformed into data
169
* model properties during parsing of the XML. Each element of the array
170
* is a hashed array of the format:
171
* array('namespaceUri' => string, 'name' => string, 'value' => string);
173
* @return array All extension attributes
175
public function getExtensionAttributes()
177
return $this->_extensionAttributes;
181
* Sets an array of all extension attributes not transformed into data
182
* model properties during parsing of the XML. Each element of the array
183
* is a hashed array of the format:
184
* array('namespaceUri' => string, 'name' => string, 'value' => string);
185
* This can be used to add arbitrary attributes to any data model element
187
* @param array $value All extension attributes
188
* @return Zend_Gdata_App_Base Returns an object of the same type as 'this' to provide a fluent interface.
190
public function setExtensionAttributes($value)
192
$this->_extensionAttributes = $value;
197
* Retrieves a DOMElement which corresponds to this element and all
198
* child properties. This is used to build an entry back into a DOM
199
* and eventually XML text for sending to the server upon updates, or
200
* for application storage/persistence.
202
* @param DOMDocument $doc The DOMDocument used to construct DOMElements
203
* @return DOMElement The DOMElement representing this element and all
206
public function getDOM($doc = null, $majorVersion = 1, $minorVersion = null)
209
$doc = new DOMDocument('1.0', 'utf-8');
211
if ($this->_rootNamespaceURI != null) {
212
$element = $doc->createElementNS($this->_rootNamespaceURI, $this->_rootElement);
213
} elseif ($this->_rootNamespace !== null) {
214
if (strpos($this->_rootElement, ':') === false) {
215
$elementName = $this->_rootNamespace . ':' . $this->_rootElement;
217
$elementName = $this->_rootElement;
219
$element = $doc->createElementNS($this->lookupNamespace($this->_rootNamespace), $elementName);
221
$element = $doc->createElement($this->_rootElement);
223
if ($this->_text != null) {
224
$element->appendChild($element->ownerDocument->createTextNode($this->_text));
226
foreach ($this->_extensionElements as $extensionElement) {
227
$element->appendChild($extensionElement->getDOM($element->ownerDocument));
229
foreach ($this->_extensionAttributes as $attribute) {
230
$element->setAttribute($attribute['name'], $attribute['value']);
236
* Given a child DOMNode, tries to determine how to map the data into
237
* object instance members. If no mapping is defined, Extension_Element
238
* objects are created and stored in an array.
240
* @param DOMNode $child The DOMNode needed to be handled
242
protected function takeChildFromDOM($child)
244
if ($child->nodeType == XML_TEXT_NODE) {
245
$this->_text = $child->nodeValue;
247
$extensionElement = new Zend_Gdata_App_Extension_Element();
248
$extensionElement->transferFromDOM($child);
249
$this->_extensionElements[] = $extensionElement;
254
* Given a DOMNode representing an attribute, tries to map the data into
255
* instance members. If no mapping is defined, the name and value are
256
* stored in an array.
258
* @param DOMNode $attribute The DOMNode attribute needed to be handled
260
protected function takeAttributeFromDOM($attribute)
262
$arrayIndex = ($attribute->namespaceURI != '')?(
263
$attribute->namespaceURI . ':' . $attribute->name):
265
$this->_extensionAttributes[$arrayIndex] =
266
array('namespaceUri' => $attribute->namespaceURI,
267
'name' => $attribute->localName,
268
'value' => $attribute->nodeValue);
272
* Transfers each child and attribute into member variables.
273
* This is called when XML is received over the wire and the data
274
* model needs to be built to represent this XML.
276
* @param DOMNode $node The DOMNode that represents this object's data
278
public function transferFromDOM($node)
280
foreach ($node->childNodes as $child) {
281
$this->takeChildFromDOM($child);
283
foreach ($node->attributes as $attribute) {
284
$this->takeAttributeFromDOM($attribute);
289
* Parses the provided XML text and generates data model classes for
290
* each know element by turning the XML text into a DOM tree and calling
291
* transferFromDOM($element). The first data model element with the same
292
* name as $this->_rootElement is used and the child elements are
293
* recursively parsed.
295
* @param string $xml The XML text to parse
297
public function transferFromXML($xml)
300
// Load the feed as an XML DOMDocument object
301
@ini_set('track_errors', 1);
302
$doc = new DOMDocument();
303
$success = @$doc->loadXML($xml);
304
@ini_restore('track_errors');
306
require_once 'Zend/Gdata/App/Exception.php';
307
throw new Zend_Gdata_App_Exception("DOMDocument cannot parse XML: $php_errormsg");
309
$element = $doc->getElementsByTagName($this->_rootElement)->item(0);
311
require_once 'Zend/Gdata/App/Exception.php';
312
throw new Zend_Gdata_App_Exception('No root <' . $this->_rootElement . '> element');
314
$this->transferFromDOM($element);
316
require_once 'Zend/Gdata/App/Exception.php';
317
throw new Zend_Gdata_App_Exception('XML passed to transferFromXML cannot be null');
322
* Converts this element and all children into XML text using getDOM()
324
* @return string XML content
326
public function saveXML()
328
$element = $this->getDOM();
329
return $element->ownerDocument->saveXML($element);
333
* Alias for saveXML() returns XML content for this element and all
336
* @return string XML content
338
public function getXML()
340
return $this->saveXML();
344
* Alias for saveXML()
346
* Can be overridden by children to provide more complex representations
349
* @return string Encoded string content
351
public function encode()
353
return $this->saveXML();
357
* Get the full version of a namespace prefix
359
* Looks up a prefix (atom:, etc.) in the list of registered
360
* namespaces and returns the full namespace URI if
361
* available. Returns the prefix, unmodified, if it's not
364
* @param string $prefix The namespace prefix to lookup.
365
* @param integer $majorVersion The major protocol version in effect.
367
* @param integer $minorVersion The minor protocol version in effect.
368
* Defaults to null (use latest).
371
public function lookupNamespace($prefix,
373
$minorVersion = null)
375
// Check for a memoized result
376
$key = $prefix . ' ' .
377
(is_null($majorVersion) ? 'NULL' : $majorVersion) .
378
' '. (is_null($minorVersion) ? 'NULL' : $minorVersion);
379
if (array_key_exists($key, self::$_namespaceLookupCache))
380
return self::$_namespaceLookupCache[$key];
381
// If no match, return the prefix by default
384
// Find tuple of keys that correspond to the namespace we should use
385
if (isset($this->_namespaces[$prefix])) {
386
// Major version search
387
$nsData = $this->_namespaces[$prefix];
388
$foundMajorV = Zend_Gdata_App_Util::findGreatestBoundedValue(
389
$majorVersion, $nsData);
390
// Minor version search
391
$nsData = $nsData[$foundMajorV];
392
$foundMinorV = Zend_Gdata_App_Util::findGreatestBoundedValue(
393
$minorVersion, $nsData);
395
$result = $nsData[$foundMinorV];
399
self::$_namespaceLookupCache[$key] = $result;
405
* Add a namespace and prefix to the registered list
407
* Takes a prefix and a full namespace URI and adds them to the
408
* list of registered namespaces for use by
409
* $this->lookupNamespace().
411
* WARNING: Currently, registering a namespace will NOT invalidate any
412
* memoized data stored in $_namespaceLookupCache. Under normal
413
* use, this behavior is acceptable. If you are adding
414
* contradictory data to the namespace lookup table, you must
415
* call flushNamespaceLookupCache().
417
* @param string $prefix The namespace prefix
418
* @param string $namespaceUri The full namespace URI
419
* @param integer $majorVersion The major protocol version in effect.
421
* @param integer $minorVersion The minor protocol version in effect.
422
* Defaults to null (use latest).
425
public function registerNamespace($prefix,
430
$this->_namespaces[$prefix][$majorVersion][$minorVersion] =
435
* Flush namespace lookup cache.
437
* Empties the namespace lookup cache. Call this function if you have
438
* added data to the namespace lookup table that contradicts values that
439
* may have been cached during a previous call to lookupNamespace().
441
public static function flushNamespaceLookupCache()
443
self::$_namespaceLookupCache = array();
447
* Add an array of namespaces to the registered list.
449
* Takes an array in the format of:
450
* namespace prefix, namespace URI, major protocol version,
451
* minor protocol version and adds them with calls to ->registerNamespace()
453
* @param array $namespaceArray An array of namespaces.
456
public function registerAllNamespaces($namespaceArray)
458
foreach($namespaceArray as $namespace) {
459
$this->registerNamespace(
460
$namespace[0], $namespace[1], $namespace[2], $namespace[3]);
466
* Magic getter to allow access like $entry->foo to call $entry->getFoo()
467
* Alternatively, if no getFoo() is defined, but a $_foo protected variable
468
* is defined, this is returned.
470
* TODO Remove ability to bypass getFoo() methods??
472
* @param string $name The variable name sought
474
public function __get($name)
476
$method = 'get'.ucfirst($name);
477
if (method_exists($this, $method)) {
478
return call_user_func(array(&$this, $method));
479
} else if (property_exists($this, "_${name}")) {
480
return $this->{'_' . $name};
482
require_once 'Zend/Gdata/App/InvalidArgumentException.php';
483
throw new Zend_Gdata_App_InvalidArgumentException(
484
'Property ' . $name . ' does not exist');
489
* Magic setter to allow acces like $entry->foo='bar' to call
490
* $entry->setFoo('bar') automatically.
492
* Alternatively, if no setFoo() is defined, but a $_foo protected variable
493
* is defined, this is returned.
495
* TODO Remove ability to bypass getFoo() methods??
497
* @param string $name
498
* @param string $value
500
public function __set($name, $val)
502
$method = 'set'.ucfirst($name);
503
if (method_exists($this, $method)) {
504
return call_user_func(array(&$this, $method), $val);
505
} else if (isset($this->{'_' . $name}) || ($this->{'_' . $name} === null)) {
506
$this->{'_' . $name} = $val;
508
require_once 'Zend/Gdata/App/InvalidArgumentException.php';
509
throw new Zend_Gdata_App_InvalidArgumentException(
510
'Property ' . $name . ' does not exist');
515
* Magic __isset method
517
* @param string $name
519
public function __isset($name)
521
$rc = new ReflectionClass(get_class($this));
522
$privName = '_' . $name;
523
if (!($rc->hasProperty($privName))) {
524
require_once 'Zend/Gdata/App/InvalidArgumentException.php';
525
throw new Zend_Gdata_App_InvalidArgumentException(
526
'Property ' . $name . ' does not exist');
528
if (isset($this->{$privName})) {
529
if (is_array($this->{$privName})) {
530
if (count($this->{$privName}) > 0) {
545
* Magic __unset method
547
* @param string $name
549
public function __unset($name)
551
if (isset($this->{'_' . $name})) {
552
if (is_array($this->{'_' . $name})) {
553
$this->{'_' . $name} = array();
555
$this->{'_' . $name} = null;
561
* Magic toString method allows using this directly via echo
562
* Works best in PHP >= 4.2.0
564
* @return string The text representation of this object
566
public function __toString()
568
return $this->getText();