Example #1
0
 /**
  * Remove tokens that don't fit our definitions and re-orders tokens when
  * necessary.
  *
  * @param array $tokens Array of tagged tokens.
  *
  * @return array  Filtered tagged tokens.
  */
 public function postTokenize($tokens)
 {
     $tokens = parent::postTokenize($tokens);
     // Catch ambiguous constructs like "heute morgen/morgen früh".
     foreach ($tokens as $num => $token) {
         if ($num >= 2 && ($repeater = $token->getTag('repeater')) && $repeater instanceof Horde_Date_Repeater_Day && ($grabber = $tokens[$num - 1]->getTag('grabber')) && $grabber == 'next') {
             $token = new Horde_Date_Parser_Token('');
             $token->tag('repeater_day_portion', new Horde_Date_Repeater_DayPortion('morning'));
             array_splice($tokens, $num - 1, 2, array($token));
             return $tokens;
         }
     }
     return $tokens;
 }
Example #2
0
 public function testToken()
 {
     $token = new Horde_Date_Parser_Token('foo');
     $this->assertEquals('foo', $token->word);
     $this->assertEquals(0, count($token->tags));
     $this->assertFalse($token->tagged());
     $token->tag('foo', 'mytag');
     $this->assertEquals(1, count($token->tags));
     $this->assertTrue($token->tagged());
     $this->assertInternalType('string', $token->getTag('foo'));
     $token->tag('bar', 5);
     $this->assertEquals(2, count($token->tags));
     $this->assertInternalType('int', $token->getTag('bar'));
     $token->untag('foo');
     $this->assertEquals(1, count($token->tags));
     $this->assertInternalType('int', $token->getTag('bar'));
 }
Example #3
0
 /**
  * handle aliases of am/pm
  * 5:00 in the morning -> 5:00 am
  * 7:00 in the evening -> 7:00 pm
  */
 public function dealiasAndDisambiguateTimes($tokens, $options)
 {
     $dayPortionIndex = null;
     foreach ($tokens as $i => $t) {
         if ($t->getTag('repeater_day_portion')) {
             $dayPortionIndex = $i;
             break;
         }
     }
     $timeIndex = null;
     foreach ($tokens as $i => $t) {
         if ($t->getTag('repeater_time')) {
             $timeIndex = $i;
             break;
         }
     }
     if ($dayPortionIndex !== null && $timeIndex !== null) {
         $t1 = $tokens[$dayPortionIndex];
         $t1tag = $t1->getTag('repeater_day_portion');
         if ($t1tag->type == 'morning') {
             $t1->untag('repeater_day_portion');
             $t1->tag('repeater_day_portion', new Horde_Date_Repeater_DayPortion('am'));
         } elseif (in_array($t1tag->type, array('afternoon', 'evening', 'night'))) {
             $t1->untag('repeater_day_portion');
             $t1->tag('repeater_day_portion', new Horde_Date_Repeater_DayPortion('pm'));
         }
     }
     // handle ambiguous times if ambiguousTimeRange is specified
     if (!isset($options['ambiguousTimeRange']) || $options['ambiguousTimeRange'] != 'none') {
         $ttokens = array();
         foreach ($tokens as $i => $t0) {
             $ttokens[] = $t0;
             $t1 = isset($tokens[$i + 1]) ? $tokens[$i + 1] : null;
             if ($t0->getTag('repeater_time') && $t0->getTag('repeater_time')->ambiguous && (!$t1 || !$t1->getTag('repeater_day_portion'))) {
                 $distoken = new Horde_Date_Parser_Token('disambiguator');
                 $distoken->tag('repeater_day_portion', new Horde_Date_Repeater_DayPortion($options['ambiguousTimeRange']));
                 $ttokens[] = $distoken;
             }
         }
         $tokens = $ttokens;
     }
     return $tokens;
 }