PHP Classes

File: adapter.php

Recommend this page to a friend!
  Classes of Malik Naik  >  PHP Design Patterns Repository  >  adapter.php  >  Download  
File: adapter.php
Role: Example script
Content type: text/plain
Description: Example script
Class: PHP Design Patterns Repository
Implementation of common design patterns in PHP
Author: By
Last change:
Date: 10 months ago
Size: 973 bytes
 

Contents

Class file image Download
<?php

require_once 'vendor/autoload.php';

use
DesignPattern\Adapter\XMLData;
use
DesignPattern\Adapter\JSONData;
use
DesignPattern\Adapter\XMLToJSONAdapter;

// Store XML Data as string.
$xml_string = <<<XML
<?xml version="1.0" encoding="UTF-8" ?>
<books>
    <book>
        <title>Programming PHP</title>
        <author>Rasmus Lerdorf</author>
    </book>
    <book>
        <title>PHP Cookbook</title>
        <author>Adam Trachtenberg and David Sklar</author>
    </book>
</books>
XML;

// Instantiate an XMLData object using the above xml.
$xml = new XMLData($xml_string);

// Pass the XMLData object to the adapter that returns the json data as string
// after converting the xml data to json.
$adapter = new XMLToJSONAdapter($xml);

// Get the JSON Data from the adapter
print $adapter->getJsonData() . "\n";

// Outputs:
//
// {"book":[{"title":"Programming PHP","author":"Rasmus Lerdorf"},{"title":"PHP Cookbook","author":"Adam Trachtenberg and David Sklar"}]}