-
Notifications
You must be signed in to change notification settings - Fork 81
Open
Labels
Description
Just expanding on the previous patch. A better idea would be to remove the responsibility of the writer class having to valid the format is valid. So in the class below i have removed that responsibility to a new format class, which can then make use of type hinting to enforce the correct type is given to the constructor of the writer class.
Not tested this code and it's almost 1am for me, but it's just to show you a rough idea of how it would work and how it would make adding new formats a little easier in the future. Hope it helps or at least gives you more ideas.
/**
* use this class to enforce that a valid instance of feed_format is passed to the feed_writer
* this removes the responsibility of the feed_writer checking the feed format and encapsulates the formats
* might want to consider refactoring some other features out of the single writer class and use inheritance
* to give each format specific features (i.e. feed_writer = abstract class, feed_writer_rss1 +
* rss_writer_atom + feed_writer_rss2 all extend feed_writer making it easy to add future feed formats) -
* but one step at a time ;)
*/
class feed_format {
/**
* formats available
*/
const ATOM = 'ATOM';
const RSS1 = 'RSS 1';
const RSS2 = 'RSS 2';
/**
* default format to use if given an invalid format on construct
*/
const DEFAULT = 'RSS 2';
/**
* currently selected format
*/
private $format;
/**
* what format to request
*/
public function __construct($format) {
//uppercase so it will still match the constants used
$this->format = strtoupper($format);
//check it's valid, else use default
switch ($this->format) {
case self::ATOM:
case self::RSS1:
case self::RSS2:
break;
default:
trigger_error("Incorrect feed format used. Defaulting to: " . self::DEFAULT, E_USER_NOTICE);
$this->format = self::DEFAULT;
}
}
/**
* returns the currently selected format so the feed_writer class can ask questions about it's type via methods
*/
public function get_format() {
return $this->format;
}
}
/**
* writer class construct signature
*/
class feed_writer {
/**
* now you can't give it an incorrect format as it will only accept an instance of feed_format
*/
public function __construct(feed_format $format) {
//...code here
}
}
//example
$feed = new feed_writer(new feed_format(feed_format::RSS));Just a thought.
Kind regards,
Scott
Reactions are currently unavailable