My favorites | Sign in
Project Home Downloads Wiki Issues Source
Project Information
Members
Featured
Downloads
Links

Summary

lexa-xml-serialization is a lightweight library for serializing PHP objects to XML like it's done in .NET:

.NET
PHP
        [XmlRoot("person")]
	class Person {
	
		[XmlElement("first-name")]
		public string FirstName { get; set; }
		
		[XmlElement("last-name")]
		public string LastName { get; set; }
		
		[XmlElement("birth-date")]
		public DateTime BirthDate { get; set; }
					
	}
    /** @XmlRoot(person) */
    class Person {

        /** @XmlElement(string, first-name) */
        private $firstName;

        /** @XmlElement(string, last-name) */
        private $lastName;

        /** @XmlElement(date, birth-date) */
        private $birthDate;

        // getters and setters are skipped for brevity
    }

You are welcome and recommended to explore the FirstLook tutorial article.

Basic concepts

My xml serializer requires that you mark serializable properties of your classes with doc-comment annotations:

/** @XmlAttribute(type, xmlName) */
private $myProp;

/** @XmlElement(type, xmlName) */
private $anotherProp;

The properties are not required to be public. The lettercase of annotations doesn't matter. Whitespaces are not essential. Parameters are optional: type defaults to string, xmlName defaults to the property name. Parameter values must not be enclosed in quotes. Empty brackets may be omitted.

The only requirement for an xml-serializable class is that it must have a default constructor. That is all the constructor arguments must be optional.

The xml tag for the root object is customized via the XmlRoot annotation:

/** @XmlRoot(burger) */
class BigBacon {

}

Supported data types

The annotation's type parameter may take the following values:

type PHP type
string or nothing string
int, integer integer
bool, boolean boolean
float, double double
date, datetime, \DateTime DateTime
MyClass OwnerNamespace\MyClass
\MyClass MyClass in the root namespace

A property may hold values of different types:

/**
  *  @XmlElement(string, custom-size)
  *  @XmlElement(int, pixel-size)
  */
private $size;

This means that integer values of the size property will be serialized as <pixel-size> tags, and string values as <custom-size>.

API

use Lexa\XmlSerialization\XmlSerializer;

// To serailize
DOMDocument XmlSerializer::serialize(object $obj);

// To unserialize
object XmlSerializer::unserialize(DOMDocument $dom, string $className);

// To generate an XML Schema
DOMDocument XmlSerializer::generateSchema(string $className);

Serializing arrays and collections

Arrays and object-oriented collections (such as ArrayObject or SplStack) are supported. If the serializer determines that the property value is traversable then it will run the foreach loop over it and dump all items into the resulting DOM. Note that no extra container will be created:

class Table {
    /**
      *  @XmlElement(TextField, text-field)
      *  @XmlElement(ComboBoxField, combo-field)
      *  @XmlElement(BooleanField, checkbox-field)
      */
    private $fields;

    function __construct() {
        $this->fields = new \ArrayObject();
    }
}

The above code may end up with something like:

<Table>
  <text-field name="name" />
  <combo-field name="gender" />
</Table>

Arrays and collections must be explicitly initialized in the contstuctor, so that they could be recognized during unserialization process. And they must be empty by default.

Nested arrays and associative arrays (with string keys) are not supported.

Your feedback

Feel free to email me at bclexa@gmail.com

Thank you

Powered by Google Project Hosting