Giter VIP home page Giter VIP logo

simple-xml-mapper's Introduction

SimpleXMLMapper

Uses reflection to map XML to PHP objects. Inspired in part by sabre/xml.

Usage Example

<?xml version="1.0" encoding="utf-8"?>
<car>
    <manufacturer>
        <name>Volkswagen</name>
        <founded>1937-01-04 00:00:00</founded>
    </manufacturer>
    <model>Golf R</model>
    <year>2016</year>
    <msrp>39995.95</msrp>
    <colours>
        <colour>Lapiz Blue</colour>
        <colour>Oryx White</colour>
    </colours>
    <options>
        <option>
            <name>19-inch "Pretoria" Wheels</name>
        </option>
        <option>
            <name>Technology Package</name>
        </option>
    </options>
    <hybrid>false</hybrid>
    <awd>true</awd>
</car>
use DateTime;
use SimpleXMLElement;
use SimpleXmlMapper\XmlMapper;
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;

class Car
{
    /**
     * @var Manufacturer
     */
    public $manufacturer;

    /**
     * @var string
     */
    public $model;

    /**
     * @var int
     */
    public $year;

    /**
     * @var float
     */
    public $msrp;

    /**
     * @var array
     */
    public $colours = [];

    /**
     * @var Option[]
     */
    public $options = [];

    /**
     * @var bool
     */
    public $hybrid;

    /**
     * @var bool
     */
    public $awd;
}

class Manufacturer
{
    /**
     * @var string
     */
    public $name;

    /**
     * @var DateTime
     */
    public $founded;
}

class Option
{
    /**
     * @var string
     */
    public $name;
}

$listExtractors = [new ReflectionExtractor];
$typeExtractors = [new PhpDocExtractor];
$extractor = new PropertyInfoExtractor($listExtractors, $typeExtractors);

$mapper = new XmlMapper($extractor);
$mapper->addType(DateTime::class, function ($xml) {
    return DateTime::createFromFormat('Y-m-d H:i:s', $xml);
});

$xml = new SimpleXMLElement(file_get_contents('car.xml'));
$car = $mapper->map($xml, Car::class);

simple-xml-mapper's People

Contributors

robgridley avatar lighth7015 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.