Basically I have a class that sends a SOAP request for room information receives a response, it can only handle one room at a time.. eg:
class roomParser {
private $numRooms;
private $adults;
private $dailyPrice;
public function parse(){}
public function send(){}
};
$room = new roomParser( $arrival, $departue );
$return = $room->parse();
if ( $return ) { }
Now I have the dilemma of basically supporting multiple rooms, and for each room I have to separately keep information of the dailyPrice, # of adults, so I have to sessionize each rooms information since its a multiple step form..
Should I just create multiple instances of my object, or somehow modify my class so it supports any # of rooms in a rooms array, and in the rooms array it contains properties for each room?
Edit #1: After taking advice I tried implementing the Command pattern:
<?php
interface Parseable {
public function parse( $arr, $dept );
}
class Room implements Parseable {
protected $_adults;
protected $_kids;
protected $_startDate;
protected $_endDate;
protected $_hotelCode;
protected $_sessionNs;
protected $_minRate;
protected $_maxRate;
protected $_groupCode;
protected $_rateCode;
protected $_promoCode;
protected $_confCode;
protected $_currency = 'USD';
protected $_soapAction;
protected $_soapHeaders;
protected $_soapServer;
protected $_responseXml;
protected $_requestXml;
public function __construct( $startdate,$enddate,$rooms=1,$adults=2,$kids=0 ) {
$this->setNamespace(SESSION_NAME);
$this->verifyDates( $startdate, $enddate );
$this->_rooms= $rooms;
$this->_adults= $adults;
$this->_kids= $kids;
$this->setSoapAction();
$this->setRates();
}
public function parse( $arr, $dept ) {
$this->_price = $arr * $dept * rand();
return $this;
}
public function setNamespace( $namespace ) {
$this->_sessionNs = $namespace;
}
private function verifyDates( $startdate, $enddate ) {}
public function setSoapAction( $str= 'CheckAvailability' ) {
$this->_soapAction = $str;
}
public function setRates( $rates='' ) { }
private function getSoapHeader() {
return '<?xml version="1.0" encoding="utf-8"?>
<soap:Header>
</soap:Header>';
}
private function getSoapFooter() {
return '</soap:Envelope>';
}
private function getSource() {
return '<POS>
<Source><RequestorId ID="" ID_Context="" /></Source>
</POS>';
}
function requestXml() {
$this->_requestXml = $this->getSoapHeader();
$this->_requestXml .='<soap:Body></soap:Body>';
return $this->_requestXml;
}
private function setSoapHeaders ($contentLength) {
$this->_soapHeaders = array('POST /url HTTP/1.1',
'Host: '.SOAP_HOST,
'Content-Type: text/xml; charset=utf-8',
'Content-Length: '.$contentLength);
}
}
class RoomParser extends SplObjectStorage {
public function attach( Parseable $obj ) {
parent::attach( $obj );
}
public function parseRooms( $arr, $dept ) {
for ( $this->rewind(); $this->valid(); $this->next() ) {
$ret = $this->current()->parse( $arr, $dept );
echo $ret->getPrice(), PHP_EOL;
}
}
}
$arrive = '12/28/2010';
$depart = '01/02/2011';
$rooms = new RoomParser( $arrive, $depart);
$rooms->attach( new Room( '12/28/2010', '01/02/2011') );
$rooms->attach( new Room( '12/29/2010', '01/04/2011') );
echo $rooms->count(), ' Rooms', PHP_EOL;