Created
July 23, 2009 18:03
-
-
Save infynyxx/153323 to your computer and use it in GitHub Desktop.
Example of Template Pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
abstract class MobileStreamsReqest { | |
private $_username; | |
private $_password; | |
private $_URI; | |
private $_version; | |
protected $parameters = array(); | |
protected $requestAttrbutes = array(); | |
protected $requestString; | |
const ATTRIBUTE_REQUIRED = "required"; | |
const ATTRIBUTE_OPTIONAL = "optional"; | |
public function __construct($username, $password, $URI, $version="1.1") { | |
$this->_username = $username; | |
$this->_password = $password; | |
$this->_URI = $URI; | |
$this->_version = $version; | |
$this->setParameters(); | |
} | |
protected abstract function setParameters(); | |
protected abstract function formatRequestString($attributes = array()); | |
private function validateParameters($attributes = array()) { | |
if (!empty($attributes)) { | |
$keys_array = asort(array_keys($attributes)); | |
$required = asort($this->getRequiredParameters()); | |
$intersected = asort(array_intersect($required, $keys_array)); | |
if ($intersected == $required) | |
return TRUE; | |
} | |
return FALSE; | |
} | |
public function doRequest($attributes = array()) { | |
$formatted_string = $this->formatRequestString($attributes); | |
$this->setRequestString($formatted_string); | |
$headers = array(); | |
$headers[] = "Content-Type: text/xml"; | |
$plist = "xml=".$this->requestString; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $this->_URI); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $plist); //adding POST data | |
curl_setopt($ch, CURLOPT_POST, TRUE); //data sent as POST | |
$result = curl_exec($ch); | |
curl_close($ch); | |
return $result; | |
} | |
private function setRequestString($str) { | |
$xml = '<?xml version="1.0" encoding="utf-16"?>'; | |
$xml .= '<MSCDP username="'.$this->_username.'" password="'.$this->_password.'" version="'.$this->_version.'">'; | |
$xml .= '<REQUEST>'; | |
$xml .= $str; | |
$xml .= '</REQUEST>'; | |
$xml .= '</MSCDP>'; | |
$this->requestString = $xml; | |
} | |
private function getRequiredParameters() { | |
$parameter_keys = array_keys($this->parameters); | |
$required_parameters = array(); | |
foreach ($this->parameters as $key => $value) { | |
if ($value == self::ATTRIBUTE_REQUIRED) | |
$required_parameters[] = $key; | |
} | |
return $required_parameters; | |
} | |
} | |
class Request_Content_Info extends MobileStreamsReqest { | |
public function __construct($username, $password, $URI) { | |
parent::__construct($username, $password, $URI); | |
} | |
protected function setParameters() { | |
$this->parameters = array( | |
'id' => self::ATTRIBUTE_REQUIRED, | |
'reference' => self::ATTRIBUTE_REQUIRED, | |
'service' => self::ATTRIBUTE_REQUIRED | |
); | |
} | |
protected function formatRequestString($attributes = array()) { | |
$parameters = array_keys($this->parameters); | |
$xml = '<CONTENT_INFO '.$parameters[0].'="'.$attributes[$parameters[0]].'" '.$parameters[2].'="'.$attributes[$parameters[2]].'" '.$parameters[1].'="'.$attributes[$parameters[1]].'" />'; | |
return $xml; | |
} | |
} | |
class Request_Customer_Content extends MobileStreamsReqest { | |
public function __construct($username, $password, $URI) { | |
parent::__construct($username, $password, $URI); | |
} | |
protected function setParameters() { | |
$this->parameters = array( | |
'id' => self::ATTRIBUTE_REQUIRED, | |
'device' => self::ATTRIBUTE_OPTIONAL, | |
'genre' => self::ATTRIBUTE_OPTIONAL , | |
'itens' => self::ATTRIBUTE_OPTIONAL, | |
'offset' => self::ATTRIBUTE_OPTIONAL, | |
'service' => self::ATTRIBUTE_OPTIONAL, | |
'device_info' => self::ATTRIBUTE_OPTIONAL, | |
'media_type' => self::ATTRIBUTE_OPTIONAL, | |
'notify' => self::ATTRIBUTE_OPTIONAL, | |
'start' => self::ATTRIBUTE_OPTIONAL, | |
'end' => self::ATTRIBUTE_OPTIONAL | |
); | |
} | |
protected function formatRequestString($attributes = array()) { | |
$parameters = array_keys($this->parameters); | |
$xml = '<CONTENT_INFO '.$parameters[0].'="'.$attributes[$parameters[0]].'" '.$parameters[2].'="'.$attributes[$parameters[2]].'" '.$parameters[1].'="'.$attributes[$parameters[1]].'" />'; | |
return $xml; | |
} | |
} | |
////////////////////////////////////////////// | |
//////////////////PROGRAM///////////////////// | |
////////////////////////////////////////////// | |
$request_content_info = new Request_Content_Info("username","secret", "http://cgi.mscdp.com/xmlold"); | |
echo $r->doRequest(array( | |
'id' => 1, | |
'service' => 167, | |
'reference' => "017798" | |
)); //XML response from server | |
$request_customer_content = new Request_Customer_Content("username","secret", "http://cgi.mscdp.com/xmlold"); | |
echo $r->doRequest(array( | |
'id' => 1, | |
'service' => 167, | |
'reference' => "017798" | |
)); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment