views:

96

answers:

3

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;
A: 

I would say that making multiple instances of the object makes sense. It's how the objects works.

Petr Peller
+3  A: 

Well what you've defined is an object that handles a single room, so naturally, if you wanted to handle multiple rooms, you should create an object that is simply a collection of these single-room objects.

If you intend to interact with your MultiRoomParser in the same way that you do your RoomParsers, this scenario may be a good candidate for the Composite Pattern. Basically, your MultiRoomParser would contain a collection of RoomParsers, and when you call a method such as parse() on your MultiRoomParser, it simply iterates through all RoomParsers in its collection and calls parse() on each element.

Shakedown
Forgive me for not mentioning this, but if I had to store generic information for all the rooms, like currency and the SOAP URI to use, would that go on the MultiRoomParser?
meder
That information could go in the MultiRoomParser, though keep in mind that once you put currency and SOAP information in the MultiRoomParser, that MultiRoomParser becomes less general and more specific and thus re-use may be harder, or the cases in which you could use it would be more limited.Perhaps you could create another class that contains the currency and SOAP information along with a MultiRoomParser.
Shakedown
+1  A: 

Given from the information in the question, I'd probably use a Command Pattern

All Rooms should implement a parse() command

interface Parseable
{
    public function parse($arr, $dept);
}

A room instance could look like this

class Room implements Parseable
{
    protected $_price;
    protected $_adults;
    public function parse($arr, $dept) {
         // nonsense calculation, exchange with your parse logic
        $this->_price = $arr * $dept * rand();
        return $this;
    }
    public function getPrice()
    {
        return $this->_price;
    }
}

To go through them, I'd add them to an Invoker that stores all rooms and knows how to invoke their parse() method and also knows what to do with the return from parse(), if necessary

class RoomParser extends SplObjectStorage
{
    // makes sure we only have objects implementing parse() in store      
    public function attach(Parseable $obj)
    {
        parent::attach($obj);
    }

    // invoking all parse() methods in Rooms
    public function parseRooms($arr, $dept)
    {
        for($this->rewind(); $this->valid(); $this->next()) {
            $ret = $this->current()->parse($arr, $dept);
            // do something with $ret
            echo $ret->getPrice(), PHP_EOL;
        }
    }
    // other methods
}

And then you could use it like this:

$parser = new RoomParser;
$parser->attach(new Room);
$parser->attach(new Room);
$parser->attach(new Room);
$parser->attach(new Room);
echo $parser->count(), ' Rooms', PHP_EOL;

$parser->parseRooms(1,2);

Note that the Invoker extends SplObjectStorage, so it implements Countable, Iterator, Traversable, Serializable and ArrayAccess.

Gordon
The Command pattern is useful for separating the method caller from the method provider, allowing for the method call to be "postponed", and also for undoing the action performed by the method. Given the information provided by the OP, it doesn't seem like these are the requirements, and thus implementing this pattern may be unnecessary. It is a fun pattern though :-)
Shakedown
@Shakedown Yes and No. A Command Pattern *can* but does not have to provide *undo*. The above code is pretty much what you suggested as a solution yourself, except that I took into account that the OP has no need for a tree. So I could argue that because all Rooms are leafs anyway, Composite is not the right choice, but then again, we could also agree that it has something of both, Command behavior and Composite structure.
Gordon
Where would you store properties that apply to all rooms? The way my system works is that you have to use the same arrival/departure dates for all rooms. Currently my `Room` class expects arrival/departure for first 2 parameters, should I just feed the same variable to all invocations of `new Room` or make the `Room` class grab it from `roomParser`?
meder
I added the classes I'm working on now for specifics.
meder
@Fedor Got multiple options. If they apply to all rooms at the same time, you could set them as static properties of `Room` or use a *Flyweight Pattern*. If they can differ, simply pass the property values from `RoomParser` to the `Room` object. I would not let the Rooms know the Parser though. Also, I think I would make the SOAP client into a separate class and share the instance between all Rooms. Can you explain some more about how it was working before?
Gordon