/** * Parses a control word token * * @param Jstewmc\Rtf\Token\Control\Word $token the control word token * @param Jstewmc\Rtf\Element\Group $group the current group * @return void * @since 0.1.0 */ protected function parseControlWord(Token\Control\Word $token, Element\Group $group) { // if a class exists for the control word $filename = ucfirst($token->getWord()); $classname = "Jstewmc\\Rtf\\Element\\Control\\Word\\{$filename}"; if (class_exists($classname)) { // instantiate the control word element and break $word = new $classname(); } else { // otherwise, instantiate a generic control word $word = new Element\Control\Word\Word(); $word->setWord($token->getWord()); } // set the element's parameter $word->setParameter($token->getParameter()); $word->setIsSpaceDelimited($token->getIsSpaceDelimited()); // append the element $word->setParent($group); $group->appendChild($word); return; }
/** * parse() should parse a small document */ public function testParse_parsesDocumentSmall() { $tokens = [new Token\Group\Open(), new Token\Control\Word('rtf', 1), new Token\Control\Word('ansi'), new Token\Control\Word('deff', 0), new Token\Group\Open(), new Token\Control\Word('fonttbl'), new Token\Group\Open(), new Token\Control\Word('f', 0), new Token\Control\Word('fnil'), new Token\Control\Word('fcharset', 0), new Token\Text('Courier New;'), new Token\Group\Close(), new Token\Group\Close(), new Token\Group\Open(), new Token\Control\Symbol('*'), new Token\Control\Word('generator'), new Token\Text('Msftedit 5.41.15.1516;'), new Token\Group\Close(), new Token\Control\Word('viewkind', 4), new Token\Control\Word('uc', 1), new Token\Control\Word('pard'), new Token\Control\Word('lang', 1033), new Token\Control\Word('f', 0), new Token\Control\Word('fs', 20), new Token\Text('My dog is not like other dogs.'), new Token\Control\Word('par'), new Token\Text('He doesn\'t care to walk, '), new Token\Control\Word('par'), new Token\Text('He doesn\'t bark, he doesn\'t howl.'), new Token\Control\Word('par'), new Token\Text('He goes "Tick, tock. Tick, tock."'), new Token\Control\Word('par'), new Token\Group\Close()]; $parser = new Parser(); $root = $parser->parse($tokens); // create the expected elements in order... $groupA = new Element\Group(); $a_1 = new Element\Control\Word\Word(); $a_1->setWord('rtf'); $a_1->setParameter(1); $a_1->setParent($groupA); $a_2 = new Element\Control\Word\Word(); $a_2->setWord('ansi'); $a_2->setParent($groupA); $a_3 = new Element\Control\Word\Word(); $a_3->setWord('deff'); $a_3->setParameter(0); $a_3->setParent($groupA); $groupB = new Element\Group(); $groupB->setParent($groupA); $b_1 = new Element\Control\Word\Word(); $b_1->setWord('fonttbl'); $b_1->setParent($groupB); $groupC = new Element\Group(); $groupC->setParent($groupB); $c_1 = new Element\Control\Word\Word(); $c_1->setWord('f'); $c_1->setParameter(0); $c_1->setParent($groupC); $c_2 = new Element\Control\Word\Word(); $c_2->setWord('fnil'); $c_2->setParent($groupC); $c_3 = new Element\Control\Word\Word(); $c_3->setWord('fcharset'); $c_3->setParameter(0); $c_3->setParent($groupC); $c_4 = new Element\Text('Courier New;'); $c_4->setParent($groupC); $groupD = new Element\Group(); $groupD->setParent($groupA); $d_1 = new Element\Control\Symbol\Asterisk(); $d_1->setParent($groupD); $d_2 = new Element\Control\Word\Word(); $d_2->setWord('generator'); $d_2->setParent($groupD); $d_3 = new Element\Text('Msftedit 5.41.15.1516;'); $d_3->setParent($groupD); // back to a $a_4 = new Element\Control\Word\Word(); $a_4->setWord('viewkind'); $a_4->setParameter(4); $a_4->setParent($groupA); $a_5 = new Element\Control\Word\Word(); $a_5->setWord('uc'); $a_5->setParameter(1); $a_5->setParent($groupA); $a_6 = new Element\Control\Word\Pard(); $a_6->setParent($groupA); $a_7 = new Element\Control\Word\Word(); $a_7->setWord('lang'); $a_7->setParameter(1033); $a_7->setParent($groupA); $a_8 = new Element\Control\Word\Word(); $a_8->setWord('f'); $a_8->setParameter(0); $a_8->setParent($groupA); $a_9 = new Element\Control\Word\Word(); $a_9->setWord('fs'); $a_9->setParameter(20); $a_9->setParent($groupA); $a_10 = new Element\Text('My dog is not like other dogs.'); $a_10->setParent($groupA); $a_11 = new Element\Control\Word\Par(); $a_11->setParent($groupA); $a_12 = new Element\Text('He doesn\'t care to walk, '); $a_12->setParent($groupA); $a_13 = new Element\Control\Word\Par(); $a_13->setParent($groupA); $a_14 = new Element\Text('He doesn\'t bark, he doesn\'t howl.'); $a_14->setParent($groupA); $a_15 = new Element\Control\Word\Par(); $a_15->setParent($groupA); $a_16 = new Element\Text('He goes "Tick, tock. Tick, tock."'); $a_16->setParent($groupA); $a_17 = new Element\Control\Word\Par(); $a_17->setParent($groupA); // now, set the relationships... $groupD->appendChild($d_1)->appendChild($d_2)->appendChild($d_3); $groupC->appendChild($c_1)->appendChild($c_2)->appendChild($c_3)->appendChild($c_4); $groupB->appendChild($b_1)->appendChild($groupC); $groupA->appendChild($a_1)->appendChild($a_2)->appendChild($a_3)->appendChild($groupB)->appendChild($groupD)->appendChild($a_4)->appendChild($a_5)->appendChild($a_6)->appendChild($a_7)->appendChild($a_8)->appendChild($a_9)->appendChild($a_10)->appendChild($a_11)->appendChild($a_12)->appendChild($a_13)->appendChild($a_14)->appendChild($a_15)->appendChild($a_16)->appendChild($a_17); // phew! $this->assertEquals($groupA, $root); return; }