PHP Classes
elePHPant
Icontem

File: BNR_reader.class.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Cristian Navalici  >  XML Currency Reader  >  BNR_reader.class.php  >  Download  
File: BNR_reader.class.php
Role: Class source
Content type: text/plain
Description: Specific implementation for National Bank of Romania xml file
Class: XML Currency Reader
Retrieve currency exchange data from XML files
Author: By
Last change:
Date: 6 years ago
Size: 3,631 bytes
 

Contents

Class file image Download
<?php
/**
 * BNR READER CLASS
 *
 * this class is intended to provide an easy way to read the xml with currencies
 * from the bank site, and returns it for further usage in a readable form
 *
 * @author Cristian Năvălici {@link http://www.lemonsoftware.eu} lemonsoftware [at] gmail [.] com
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 * @version 1.0 26 Oct 2008
 *
 */

class BNR_reader extends XML_currency_reader{
   
/**
    * xml file on the BNR site
    */
   
protected $xmlpath = 'http://www.bnro.ro/nbrfxrates.xml';

   
// ----------------------------------------------------------------------------
    /**
    * CONSTRUCTOR
    *
    *
    * @param none
    * @return void
    */
   
public function __construct() { }


   
// ----------------------------------------------------------------------------
    /**
    * PARSE RATE CONTENT
    *
    * this method is used to extract specific information about a currency
    * particular usage for National Bank of Romania
    *
    * @param array - $content - array containing arrays of currencies and rates [2]
    * @param string - currency name three letters (currency symbol)
    * @return array [rate][multiplier] | empty array of not found
    */
   
public function parse_rate_contents($content, $currency = 'EUR') {
        try {
            if ( !
$content ) return array();

           
// cycle for each value
           
foreach ( $content as $con ) {
               
$name = $con['currency'];
                if (
$name == $currency ) {
                    return array(
'rate' => $con['rate'], 'multiplier' => $con['multiplier']);
                }
            }

            return array();

        } catch (
Exception $e) {
            echo
'BNR_reader->parse_rate_contents: ' .$e->getMessage(); exit();
        }
    }


   
// ----------------------------------------------------------------------------
    /**
    * PARSE THE DOMDocument
    *
    * parse the xml content and returns it into a readable form (array)
    * this is a specific functions for National Bank Of Romania
    *
    * @param none
    * @return array - of arrays - for structure see doc or look into the code
    */
   
public function parse_domdoc() {
        try {
            if ( !
$this->domdoc ) {
                throw new
Exception('No DOMDocument. DOMconnector() must be run before.');
            }

           
$dom = $this->domdoc;

           
$sender_nl = $dom->getElementsByTagName('Sender');
           
$sender_name = $sender_nl->item(0)->nodeValue;

           
$sending_date_nl = $dom->getElementsByTagName('SendingDate');
           
$sending_date = $sending_date_nl->item(0)->nodeValue;

           
// in Cube node we have all the rates
           
$cube_nl = $dom->getElementsByTagName('Cube')->item(0);

            foreach (
$cube_nl->childNodes as $c ) {
               
$mul = ( $c->hasAttribute('multiplier') ) ? (int)$c->getAttribute('multiplier') : 1;

               
$currency = $c->getAttribute('currency');
               
$rate = $c->nodeValue;
               
$rates[] = array('currency' => $currency, 'rate' => $rate, 'multiplier' => $mul);
            }

            return array(
$sender_name, $sending_date, $rates);

        } catch (
Exception $e) {
            echo
'BNR_reader->DOMconnector: ' .$e->getMessage();
        } catch (
DOMException $e) {
            echo
'DOM: BNR_reader->DOMconnector: ' .$e->getMessage();
        }
    }

}
// class
?>