openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

DslFor.class.php (1005B)


      1 <?php
      2 
      3 namespace dsl\ast;
      4 
      5 use dsl\DslRuntimeException;
      6 use dsl\DslToken;
      7 use dsl\standard\StandardArray;
      8 
      9 class DslFor implements DslStatement
     10 {
     11 	private $name;
     12 	private $list;
     13 	private $statements;
     14 
     15 	/**
     16 	 * DslFor constructor.
     17 	 *
     18 	 * @param $name String
     19 	 * @param $list DslToken[]
     20 	 * @param $statements DslToken[]
     21 	 */
     22 	public function __construct($name, $list, $statements)
     23 	{
     24 		$this->name = $name;
     25 		$this->list = new DslExpression( $list );
     26 		$this->statements = new DslStatementList( $statements );
     27 	}
     28 
     29 
     30 	public function execute( & $context ) {
     31 
     32 		$list = $this->list->execute( $context );
     33 
     34 		if   ( ! $list instanceof StandardArray )
     35 			throw new DslRuntimeException('for value is not an array');
     36 
     37 		$copiedContext = $context;
     38 		foreach( $list->getInternalValue() as $loopVar ) {
     39 
     40 			// copy loop var to current loop context
     41 			$copiedContext[ $this->name ] = $loopVar;
     42 
     43 			// Execute "for" block
     44 			$this->statements->execute( $copiedContext );
     45 		}
     46 	}
     47 
     48 	public function parse($tokens)
     49 	{
     50 	}
     51 }