Source for file KeywordStringParser.class.php

Documentation is available at KeywordStringParser.class.php

  1. <?php
  2. /**
  3. * @since 5/24/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: KeywordStringParser.class.php,v 1.3 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. require_once(dirname(__FILE__)."/StringParser.class.php");
  15.  
  16.  
  17. /**
  18. * KeywordStringParser matches keywords to common times.
  19. *
  20. * Valid keywords:
  21. * <br/>
  22. * <br/>Now
  23. * - now
  24. * Today
  25. * - today
  26. * - current
  27. * Tomorrow
  28. * - tomorrow
  29. * Yesterday
  30. * - yesterday
  31. *
  32. * @since 5/24/05
  33. * @package harmoni.primitives.chronology.string_parsers
  34. *
  35. * @copyright Copyright &copy; 2005, Middlebury College
  36. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  37. *
  38. * @version $Id: KeywordStringParser.class.php,v 1.3 2007/09/04 20:25:25 adamfranco Exp $
  39. *
  40. * @link http://harmoni.sourceforge.net/
  41. * @author Adam Franco <adam AT adamfranco DOT com> <afranco AT middlebury DOT edu>
  42. */
  43. class KeywordStringParser
  44. extends StringParser
  45. {
  46.  
  47. /*******************************************************
  48. * Instance Methods
  49. *********************************************************/
  50.  
  51. /**
  52. * Answer True if this parser can handle the format of the string passed.
  53. *
  54. * @return boolean
  55. * @access public
  56. * @since 5/24/05
  57. */
  58. function canHandle () {
  59. $term = trim(strtolower($this->input));
  60. return (
  61. in_array($term, $this->nowArray())
  62. || in_array($term, $this->todayArray())
  63. || in_array($term, $this->tomorrowArray())
  64. || in_array($term, $this->yesterdayArray())
  65. );
  66. }
  67. /**
  68. * Parse the input string and set our elements based on the contents of the
  69. * input string. Elements not found in the string will be null.
  70. *
  71. * @return void
  72. * @access private
  73. * @since 5/24/05
  74. */
  75. function parse () {
  76. $term = trim(strtolower($this->input));
  77. if (in_array($term, $this->nowArray()))
  78. $timespan = Timespan::current();
  79. else if (in_array($term, $this->todayArray()))
  80. $timespan = Date::today();
  81. else if (in_array($term, $this->tomorrowArray()))
  82. $timespan = Date::tomorrow();
  83. else if (in_array($term, $this->yesterdayArray()))
  84. $timespan = Date::yesterday();
  85. $dateAndTime =$timespan->start();
  86. $offset =$dateAndTime->offset();
  87.  
  88. $this->setYear($dateAndTime->year());
  89. $this->setMonth($dateAndTime->month());
  90. $this->setDay($dateAndTime->dayOfMonth());
  91. $this->setHour($dateAndTime->hour());
  92. $this->setMinute($dateAndTime->minute());
  93. $this->setSecond($dateAndTime->second());
  94. $this->setOffsetHour($offset->hours());
  95. $this->setOffsetMinute($offset->minutes());
  96. $this->setOffsetSecond($offset->seconds());
  97. }
  98.  
  99. /*******************************************************
  100. * Instance Methods - Accessing
  101. *********************************************************/
  102.  
  103. /**
  104. * Answer the array of keywords that refer to now.
  105. *
  106. * @return array
  107. * @access protected
  108. * @since 5/24/05
  109. */
  110. function nowArray () {
  111. return array(
  112. 'now'
  113. );
  114. }
  115. /**
  116. * Answer the array of keywords that refer to tomorrow.
  117. *
  118. * @return array
  119. * @access protected
  120. * @since 5/24/05
  121. */
  122. function todayArray () {
  123. return array(
  124. 'today',
  125. 'current'
  126. );
  127. }
  128. /**
  129. * Answer the array of keywords that refer to tomorrow.
  130. *
  131. * @return array
  132. * @access protected
  133. * @since 5/24/05
  134. */
  135. function tomorrowArray () {
  136. return array(
  137. 'tomorrow'
  138. );
  139. }
  140. /**
  141. * Answer the array of keywords that refer to yesterday.
  142. *
  143. * @return array
  144. * @access protected
  145. * @since 5/24/05
  146. */
  147. function yesterdayArray () {
  148. return array(
  149. 'yesterday'
  150. );
  151. }
  152. }
  153.  
  154. ?>

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