Source for file StringParser.class.php

Documentation is available at StringParser.class.php

  1. <?php
  2. /**
  3. * @since 5/23/05
  4. * @package harmoni.primitives.chronology.string_parsers
  5. *
  6. * @copyright Copyright &copy; 2005, Middlebury College
  7. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  8. *
  9. * @version $Id: StringParser.class.php,v 1.7 2007/09/04 20:25:25 adamfranco Exp $
  10. *
  11. * @link http://harmoni.sourceforge.net/
  12. * @author Adam Franco <adam AT adamfranco DOT com> <afranco AT middlebury DOT edu>
  13. */
  14.  
  15. require_once(dirname(__FILE__)."/../../Objects/SObject.class.php");
  16. require_once(dirname(__FILE__)."/../Month.class.php");
  17.  
  18.  
  19. /**
  20. * StringParser and its decendent classes form a Strategy pattern. They classes
  21. * that each implement a differnt method (strategy) for parsing strings into
  22. * dates and times.
  23. *
  24. * To try to parse a string using all (general) StringParsers use the
  25. * {@link getParserFor getParserFor($aString)} method to iterate through the
  26. * parsers until one is found that can handle the input:
  27. * <code>
  28. * $parser = StringParser::getParserFor($aString);
  29. *
  30. * if (!$parser)
  31. * die("'".$aString."' is not in a valid format.");
  32. *
  33. * $result = Date::withYearMonthDay($parser->year(), $parser->month(), $parser->day());
  34. * </code>
  35. *
  36. * To use StringParsers individually, use the canHandle($aString) method to find out if it is
  37. * appropriate to use this parse for a given string. If it is appropriate, create
  38. * a new StringParser with the given string and access its elements for the results:
  39. * <code>
  40. * $parser = new ANSI58216StringParser($aString);
  41. *
  42. * if (!$parser)
  43. * die("'".$aString."' is not in a valid format.");
  44. *
  45. * $result = Duration::withDaysHoursMinutesSeconds($parser->day(), $parser->hour(),
  46. * $parser->minute(), $parser->second());
  47. * </code>
  48. *
  49. * To create new StringParsers, implement the canHandle() and parse() methods.
  50. *
  51. * @since 5/23/05
  52. * @package harmoni.primitives.chronology.string_parsers
  53. *
  54. * @copyright Copyright &copy; 2005, Middlebury College
  55. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  56. *
  57. * @version $Id: StringParser.class.php,v 1.7 2007/09/04 20:25:25 adamfranco Exp $
  58. *
  59. * @link http://harmoni.sourceforge.net/
  60. * @author Adam Franco <adam AT adamfranco DOT com> <afranco AT middlebury DOT edu>
  61. */
  62. class StringParser
  63. extends SObject {
  64.  
  65. /*******************************************************
  66. * Class Methods
  67. *********************************************************/
  68.  
  69.  
  70. /**
  71. * Answer the parser that was able to successfully parse the input string
  72. * or FALSE if none could handle the input string.
  73. *
  74. * @param string $aString
  75. * @return mixed object StringParser OR FALSE
  76. * @access public
  77. * @since 5/24/05
  78. * @static
  79. */
  80. function getParserFor ( $aString ) {
  81. // Go through our parsers and try to find one that understands the format.
  82. $parserClasses = array(
  83. 'ISO8601StringParser',
  84. 'ISO8601TimeStringParser',
  85. 'DayMonthNameYearStringParser',
  86. 'MonthNameDayYearStringParser',
  87. 'MonthNumberDayYearStringParser',
  88. 'KeywordStringParser',
  89. 'TimeStringParser',
  90. 'DateAndTimeStringParser'
  91. );
  92. $handled = FALSE;
  93. while (!$handled && current($parserClasses)) {
  94. $parserClass = current($parserClasses);
  95. $parser = new $parserClass($aString);
  96. if ($parser->canHandle()) {
  97. $handled = TRUE;
  98. break;
  99. } else {
  100. next($parserClasses);
  101. }
  102. }
  103. if ($handled && is_object($parser))
  104. return $parser;
  105. else {
  106. $false =FALSE;
  107. return $false;
  108. }
  109. }
  110.  
  111. /*******************************************************
  112. * Instance Variables
  113. *********************************************************/
  114.  
  115. /**
  116. * @var string $input; The input string
  117. * @access private
  118. * @since 5/23/05
  119. */
  120. var $input;
  121. /**
  122. * @var integer $year; The year found in the input
  123. * @access private
  124. * @since 5/23/05
  125. */
  126. var $year = NULL;
  127. /**
  128. * @var integer $month; The month found in the input
  129. * @access private
  130. * @since 5/23/05
  131. */
  132. var $month = NULL;
  133. /**
  134. * @var integer $day; The day found in the input
  135. * @access private
  136. * @since 5/23/05
  137. */
  138. var $day = NULL;
  139. /**
  140. * @var integer $hour; The hour found in the input
  141. * @access private
  142. * @since 5/23/05
  143. */
  144. var $hour = NULL;
  145. /**
  146. * @var integer $minute; The minute found in the input
  147. * @access private
  148. * @since 5/23/05
  149. */
  150. var $minute = NULL;
  151. /**
  152. * @var integer $second; The second found in the input
  153. * @access private
  154. * @since 5/23/05
  155. */
  156. var $second = NULL;
  157. /**
  158. * @var integer $offsetHour; The hour offset from UTC found in the input
  159. * @access private
  160. * @since 5/23/05
  161. */
  162. var $offsetHour = NULL;
  163. /**
  164. * @var integer $offsetMinute; The minute offset from UTC found in the input
  165. * @access private
  166. * @since 5/23/05
  167. */
  168. var $offsetMinute = NULL;
  169. /**
  170. * @var integer $offsetSecond; The second offset from UTC found in the input
  171. * @access private
  172. * @since 5/23/05
  173. */
  174. var $offsetSecond = NULL;
  175. /*******************************************************
  176. * Instance Methods
  177. *********************************************************/
  178.  
  179.  
  180. /**
  181. * Create a new parser with the given input string.
  182. *
  183. * @param string $aString
  184. * @return object StringParser
  185. * @access public
  186. * @since 5/23/05
  187. */
  188. function StringParser ( $aString ) {
  189. $this->input = $aString;
  190. if ($this->canHandle())
  191. $this->parse();
  192. }
  193. /**
  194. * Answer True if this parser can handle the format of the string passed.
  195. *
  196. * @return boolean
  197. * @access public
  198. * @since 5/23/05
  199. */
  200. function canHandle () {
  201. die(__CLASS__.'::'.__FUNCTION__.'() was not overridden by a child class.');
  202. }
  203. /**
  204. * Parse the input string and set our elements based on the contents of the
  205. * input string. Elements not found in the string will be null.
  206. *
  207. * @return void
  208. * @access private
  209. * @since 5/23/05
  210. */
  211. function parse () {
  212. die(__CLASS__.'::'.__FUNCTION__.'() was not overridden by a child class.');
  213. }
  214.  
  215. /*******************************************************
  216. * Instance Methods - Accessing
  217. *********************************************************/
  218.  
  219. /**
  220. * Answer the year or NULL.
  221. *
  222. * @return integer
  223. * @access public
  224. * @since 5/23/05
  225. */
  226. function year () {
  227. return $this->year;
  228. }
  229. /**
  230. * Answer the month or NULL.
  231. *
  232. * @return integer
  233. * @access public
  234. * @since 5/23/05
  235. */
  236. function month () {
  237. return $this->month;
  238. }
  239. /**
  240. * Answer the day or NULL.
  241. *
  242. * @return integer
  243. * @access public
  244. * @since 5/23/05
  245. */
  246. function day () {
  247. return $this->day;
  248. }
  249. /**
  250. * Answer the hour or NULL.
  251. *
  252. * @return integer
  253. * @access public
  254. * @since 5/23/05
  255. */
  256. function hour () {
  257. return $this->hour;
  258. }
  259. /**
  260. * Answer the minute or NULL.
  261. *
  262. * @return integer
  263. * @access public
  264. * @since 5/23/05
  265. */
  266. function minute () {
  267. return $this->minute;
  268. }
  269. /**
  270. * Answer the second or NULL.
  271. *
  272. * @return integer
  273. * @access public
  274. * @since 5/23/05
  275. */
  276. function second () {
  277. return $this->second;
  278. }
  279. /**
  280. * Answer the hour offset from UTC or NULL.
  281. *
  282. * @return integer
  283. * @access public
  284. * @since 5/23/05
  285. */
  286. function offsetHour () {
  287. return $this->offsetHour;
  288. }
  289. /**
  290. * Answer the minute offset from UTC or NULL.
  291. *
  292. * @return integer
  293. * @access public
  294. * @since 5/23/05
  295. */
  296. function offsetMinute () {
  297. return $this->offsetMinute;
  298. }
  299. /**
  300. * Answer the second offset from UTC or NULL.
  301. *
  302. * @return integer
  303. * @access public
  304. * @since 5/23/05
  305. */
  306. function offsetSecond () {
  307. return $this->offsetSecond;
  308. }
  309. /*******************************************************
  310. * Instance Methods - Setting (private
  311. *********************************************************/
  312.  
  313. /**
  314. * Set the year
  315. *
  316. * @param integer $anInteger
  317. * @return void
  318. * @access private
  319. * @since 5/23/05
  320. */
  321. function setYear ( $anInteger ) {
  322. $this->year = intval($anInteger);
  323. }
  324. /**
  325. * Set the month
  326. *
  327. * @param mixed $anIntOrString
  328. * @return void
  329. * @access private
  330. * @since 5/23/05
  331. */
  332. function setMonth ( $anIntOrString ) {
  333. if (!$anIntOrString) {
  334. $this->month = NULL;
  335. } else if (is_numeric($anIntOrString)) {
  336. $this->month = intval($anIntOrString);
  337. } else {
  338. $this->month = Month::indexOfMonth($anIntOrString);
  339. }
  340. }
  341. /**
  342. * Set the day
  343. *
  344. * @param integer $anInteger
  345. * @return void
  346. * @access private
  347. * @since 5/23/05
  348. */
  349. function setDay ( $anInteger ) {
  350. $this->day = intval($anInteger);
  351. }
  352. /**
  353. * Set the hour
  354. *
  355. * @param integer $anInteger
  356. * @return void
  357. * @access private
  358. * @since 5/23/05
  359. */
  360. function setHour ( $anInteger ) {
  361. $this->hour = intval($anInteger);
  362. }
  363. /**
  364. * Set the minute
  365. *
  366. * @param integer $anInteger
  367. * @return void
  368. * @access private
  369. * @since 5/23/05
  370. */
  371. function setMinute ( $anInteger ) {
  372. $this->minute = intval($anInteger);
  373. }
  374. /**
  375. * Set the second
  376. *
  377. * @param float $aFloat
  378. * @return void
  379. * @access private
  380. * @since 5/23/05
  381. */
  382. function setSecond ( $anInteger ) {
  383. $this->second = $anInteger;
  384. }
  385. /**
  386. * Set the hour offset from UTC
  387. *
  388. * @param integer $anInteger
  389. * @return void
  390. * @access private
  391. * @since 5/23/05
  392. */
  393. function setOffsetHour ( $anInteger ) {
  394. $this->offsetHour = intval($anInteger);
  395. }
  396. /**
  397. * Set the minute offset from UTC
  398. *
  399. * @param integer $anInteger
  400. * @return void
  401. * @access private
  402. * @since 5/23/05
  403. */
  404. function setOffsetMinute ( $anInteger ) {
  405. $this->offsetMinute = intval($anInteger);
  406. }
  407. /**
  408. * Set the second offset from UTC
  409. *
  410. * @param integer $anInteger
  411. * @return void
  412. * @access private
  413. * @since 5/23/05
  414. */
  415. function setOffsetSecond ( $anInteger ) {
  416. $this->offsetSecond = $anInteger;
  417. }
  418. }
  419.  
  420. require_once(dirname(__FILE__)."/RegexStringParser.class.php");
  421. require_once(dirname(__FILE__)."/TwoDigitYearStringParser.class.php");
  422.  
  423. require_once(dirname(__FILE__)."/ANSI58216StringParser.class.php");
  424. require_once(dirname(__FILE__)."/ISO8601StringParser.class.php");
  425. require_once(dirname(__FILE__)."/ISO8601TimeStringParser.class.php");
  426. require_once(dirname(__FILE__)."/DayMonthNameYearStringParser.class.php");
  427. require_once(dirname(__FILE__)."/MonthNameDayYearStringParser.class.php");
  428. require_once(dirname(__FILE__)."/MonthNumberDayYearStringParser.class.php");
  429. require_once(dirname(__FILE__)."/KeywordStringParser.class.php");
  430. require_once(dirname(__FILE__)."/TimeStringParser.class.php");
  431. require_once(dirname(__FILE__)."/DateAndTimeStringParser.class.php");
  432.  
  433. ?>

Documentation generated on Wed, 19 Sep 2007 10:26:54 -0400 by phpDocumentor 1.3.0RC3