/** * Creates a control word token from a stream of characters * * @param Jstewmc\Stream $stream a stream of characters (the current character * in $characters must be the backslash character ("\")) * @return Jstewmc\Rtf\Token\Control\Word|false * @throws InvalidArgumentException if the current character in $stream is * not the backslash character ("\") * @throws InvalidArgumentException if the next character in $stream is not * an alphabetic character * @since 0.1.0 * @since 0.2.0 renamed from createFromSource() to createFromStream(); replaced * argument $characters, an array of characters, to $stream, an instance of * Jstewmc\Stream */ public static function createFromStream(\Jstewmc\Stream\Stream $stream) { $token = false; // if a current character exists if ($stream->current()) { // if the current character is the backslash character if ($stream->current() === '\\') { // if the next character exists if ($stream->next() !== false) { // if the now current character is an alphabetic character if (ctype_alpha($stream->current())) { // get the control word's word $word = self::readWord($stream); // if the current character is a digit or hyphen, get the word's parameter if (ctype_digit($stream->current()) || $stream->current() == '-') { $parameter = self::readParameter($stream); } else { $parameter = null; } // create the control word token $token = new Word($word, $parameter); // if the current character is a space delimiter, set the flag; otherwise, // it is not a space character, and it should not be consumed; it's the // start of another token; rollback to the previous character to leave // the pointer on the last character of this token // if ($stream->current() === ' ') { $token->setIsSpaceDelimited(true); } else { $token->setIsSpaceDelimited(false); $stream->previous(); } } else { throw new \InvalidArgumentException(__METHOD__ . "() expects the next element in parameter one, characters, to " . "be an alphabetic character"); } } else { // hmmm, do nothing? } } else { throw new \InvalidArgumentException(__METHOD__ . "() expects the current element in parameter one, characters, to " . "be the backslash character"); } } return $token; }
/** * __toString() should return string if not space delimited */ public function testToString_returnsString_ifNotSpaceDelimited() { $word = new Word(); $word->setWord('b'); $word->setIsSpaceDelimited(false); $this->assertEquals('\\b', (string) $word); return; }