示例#1
0
 public function testToString()
 {
     $list = new \r8\Quoter\Parsed();
     $list->addSection(new \r8\Quoter\Section\Unquoted("snippet"));
     $list->addSection(new \r8\Quoter\Section\Quoted("inQuotes", '(', ')'));
     $this->assertSame("snippet(inQuotes)", $list->__toString());
     $this->assertSame("snippet(inQuotes)", "{$list}");
 }
示例#2
0
 /**
  * Breaks a string up according the settings in this instance
  *
  * @param String $string The string to parse
  * @return Object Returns a \r8\Quoter\Parsed object
  */
 public function parse($string)
 {
     $string = (string) $string;
     $openQuotes = $this->getOpenQuotes();
     $result = new \r8\Quoter\Parsed();
     do {
         // Find the next open quote and it's offset
         list($openOffset, $openQuote) = self::findNext($string, $openQuotes, $this->escape);
         // If a quote couldn't be found, break out of the loop
         if ($openOffset === FALSE) {
             $result->addSection(new \r8\Quoter\Section\Unquoted($string));
             break;
         } else {
             if ($openOffset > 0) {
                 // Construct the unquoted section and add it to the result
                 $result->addSection(new \r8\Quoter\Section\Unquoted(substr($string, 0, $openOffset)));
             }
         }
         // Remove the unquoted section from the string
         $string = substr($string, $openOffset + strlen($openQuote));
         // Look for the close quote
         list($closeOffset, $closeQuote) = self::findNext($string, $this->getCloseQuotesFor($openQuote), $this->escape);
         if ($closeOffset === FALSE) {
             $quoted = $string;
         } else {
             $quoted = substr($string, 0, $closeOffset);
             $string = substr($string, $closeOffset + strlen($closeQuote));
         }
         // Construct the quoted section and add it to the result
         $result->addSection(new \r8\Quoter\Section\Quoted($quoted, $openQuote, $closeQuote));
     } while ($closeOffset !== FALSE && $string !== FALSE);
     return $result;
 }