Advertisement
Guest User

Variation of pattern Inerpreter in PHP

a guest
Apr 16th, 2016
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.71 KB | None | 0 0
  1. <?php
  2.  
  3. //-----------------------------------------------------------------------------------------
  4.  
  5. function Interpret($name, &$context, $chain)
  6. {
  7.   if (!array_key_exists($name, $context)) $context[$name] = array();
  8.   $con = $context[$name];
  9.   if (!array_key_exists("start", $con)) $con["start"] = 0;
  10.  
  11.   $start = $con["start"];
  12.   $res = true;
  13.   for($i = $start; $i<count($chain); $i++)
  14.   {
  15.     if (!$chain[$i]($context))
  16.     {
  17.       $con["start"] = $i;
  18.       $res = false;
  19.       break;
  20.     }
  21.   }
  22.   $context[$name] = $con;
  23.   return $res;
  24. }
  25.  
  26.  
  27. //-----------------------------------------------------------------------------------------
  28.  
  29. session_start();
  30.  
  31. if (!array_key_exists("context", $_SESSION))
  32. {  
  33.   $_SESSION["context"] = array();
  34.   $_SESSION["context"]["required_values"] = array();  
  35.   $_SESSION["context"]["entered_values"] = array();
  36.   $_SESSION["names"] = 0;
  37. }
  38.  
  39. $_SESSION["context"]["required_values"] = array();  
  40. $_SESSION["context"]["entered_values"] = $_GET;
  41.  
  42. $prog = array();
  43.  
  44.  
  45. //-----------------------------------------------------------------------------------------
  46.  
  47. $prog[] = function(&$context)
  48. {
  49.   $context["qwe"] = 5;
  50.   return true;
  51. };
  52. $prog[] = function(&$context)
  53. {
  54.   $context["qwe"] *= 2;
  55.   return true;
  56. };
  57. $prog[] = function(&$context)
  58. {
  59.   $prog = array();
  60.  
  61.   if (!array_key_exists("qwe10", $context)) $context["qwe10"] = ($context["qwe"] > 10);
  62.  
  63.   if ($context["qwe10"])
  64.   $prog[] = function(&$context)
  65.   {
  66.     $context["qwe"] /= 4;
  67.     return true;
  68.   };
  69.   else
  70.   $prog[] = function(&$context)
  71.   {
  72.     if (array_key_exists("increment", $context["entered_values"]))
  73.     {
  74.       $context["qwe"] += is_numeric($context["entered_values"]["increment"]) ? $context["entered_values"]["increment"] : 0;
  75.       return true;
  76.     }
  77.     else
  78.     {
  79.       $context["required_values"][] = "increment";
  80.       return false;
  81.     }
  82.   };
  83.  
  84.   return Interpret("qwe10node", $_SESSION["context"], $prog);
  85. };
  86.  
  87. //-----------------------------------------------------------------------------------------
  88.  
  89. $res = Interpret("main", $_SESSION["context"], $prog);
  90.  
  91. echo "Full state: ".json_encode($_SESSION["context"]);
  92. echo "<br />";
  93.  
  94. if ($res)
  95. {
  96.   echo "Result: ".$_SESSION["context"]["qwe"];
  97.   echo "<br />";
  98.   echo "Done";
  99.   $_SESSION = array();
  100. }
  101. else
  102. {
  103.   if (count($_SESSION["context"]["required_values"]) > 0)
  104.   {    
  105.     echo '<form action="main.php">';
  106.  
  107.     foreach ($_SESSION["context"]["required_values"] as $name)
  108.     {
  109.       echo "Enter $name: <input type='text' name='$name'><br>";
  110.     }
  111.     echo '<input type="submit"><br></form><br>';
  112.   }
  113. }
  114.  
  115. //-----------------------------------------------------------------------------------------
  116.  
  117. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement