<?php require_once "StateMachine.class.php"; echo "StateMachine\n\n"; /* Set up: */ $state = new StateMachine(); $state->addState("idle")->addState("symbol")->addState("variable")->addState("doublequoted")->addState("singlequoted")->addState("escaped")->addEvent("idle", "dollarsign", "variable")->addEvent("idle", "doublequote", "doublequoted")->addEvent("idle", "singlequote", "singlequoted")->addEvent("idle", "alpha", "symbol")->addEvent("idle", "nonalpha", "symbol")->addEvent("symbol", "alpha", "symbol")->addEventAny("symbol", "idle")->addEvent("variable", "alpha", "variable")->addEventAny("variable")->addEvent("doublequoted", "escape", "escaped")->addEvent("doublequoted", "dollarsign", "variable")->addEvent("doublequoted", "doublequote", "idle")->addEvent("singlequoted", "singlequote", "idle")->addEvent("singlequoted", "singlequote", "idle")->addEventAny("escaped")->start("idle"); /* Example: */ $parse_this = "This text has some \"DOBLE QUOTED PARTS WITH \$vars AND \\e\\s\\capes ON IT\", along with some 'single quoted text too with futile e\\s\\c\\a\\p\\e\\s'\"(because they don't work on single quotes)\" and some \$variable_names\n"; echo $parse_this; $triggers = array("\\" => "escape", '"' => "doublequote", "'" => "singlequote", '$' => "dollarsign"); // first character intentionally left blank to add 1 to position count of the // rest $alpha = " abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789"; $stored = ""; for ($i = 0; $i < strlen($parse_this); $i++) { $char = $parse_this[$i]; if (isset($triggers[$char])) { $state->trigger($triggers[$char]); } else { $state->trigger(strpos($alpha, $char) ? "alpha" : "nonalpha"); } if (!$state->changed() || $state->previous() == "escaped") { $stored .= $char; } elseif ($state->state() == "escaped") { } else { switch ($state->previous()) { case "idle": $stored = ""; break; case "symbol":
/** * New states are added to the end only * * @return void */ public function testNewStatesAreAddedToTheEnd() { $stateMachine = new StateMachine(); $stateMachine->addState("<?xml version=\"1.0\"?><listofitems><item id=\"1\" index=\"1\">First</item><item id=\"2\">Second</item></listofitems>"); $stateMachine->addState("<?xml version=\"1.0\"?><listofitems><item id=\"2\">second</item><item id=\"3\">Third</item></listofitems>"); $stateMachine->addState("<?xml version=\"1.0\"?><listofitems><item id=\"1\" index=\"1\">First</item><item id=\"2\">Second</item></listofitems>"); $stateMachine->addState("<?xml version=\"1.0\"?><listofitems><item id=\"4\">Fourth</item></listofitems>"); $this->assertEquals(true, $stateMachine->verifyIntegrity()); $stateMachine->addState("<?xml version=\"1.0\"?><listofitems><item id=\"5\">fifth</item><item id=\"3\">Third</item></listofitems>"); $this->assertXmlStringEqualsXmlString("<?xml version=\"1.0\"?><listofitems><item id=\"2\">second</item><item id=\"3\">Third</item></listofitems>", $stateMachine->getState(2)); $this->assertEquals(true, $stateMachine->verifyIntegrity()); }