Source for file tif.php

Documentation is available at tif.php

  1. <?php
  2.  
  3. /**
  4.  * Ternary if operation
  5.  *
  6.  * It evaluates the first argument and returns the second if it's true, or the third if it's false
  7.  * <pre>
  8.  *  * rest : you can not use named parameters to call this, use it either with three arguments in the correct order (expression, true result, false result) or write it as in php (expression ? true result : false result)
  9.  * </pre>
  10.  * This software is provided 'as-is', without any express or implied warranty.
  11.  * In no event will the authors be held liable for any damages arising from the use of this software.
  12.  *
  13.  * @author     Jordi Boggiano <j.boggiano@seld.be>
  14.  * @copyright  Copyright (c) 2008, Jordi Boggiano
  15.  * @license    http://dwoo.org/LICENSE   Modified BSD License
  16.  * @link       http://dwoo.org/
  17.  * @version    1.0.0
  18.  * @date       2008-10-23
  19.  * @package    Dwoo
  20.  */
  21. function Dwoo_Plugin_tif_compile(Dwoo_Compiler $compilerarray $rest)
  22. {
  23.     // load if plugin
  24.     if (!class_exists('Dwoo_Plugin_if'false)) {
  25.         try {
  26.             $compiler->getDwoo()->getLoader()->loadPlugin('if');
  27.         catch (Exception $e{
  28.             throw new Dwoo_Compilation_Exception($compiler'Tif: the if plugin is required to use Tif');
  29.         }
  30.     }
  31.  
  32.     if (count($rest== 1{
  33.         return $rest[0];
  34.     }
  35.     
  36.     // fetch false result and remove the ":" if it was present
  37.     $falseResult array_pop($rest);
  38.  
  39.     if (trim(end($rest)'"\''=== ':'{
  40.         // remove the ':' if present
  41.         array_pop($rest);
  42.     elseif (trim(end($rest)'"\''=== '?' || count($rest=== 1{
  43.         if ($falseResult === '?' || $falseResult === ':'{
  44.             throw new Dwoo_Compilation_Exception($compiler'Tif: incomplete tif statement, value missing after '.$falseResult);
  45.         }
  46.         // there was in fact no false result provided, so we move it to be the true result instead
  47.         $trueResult $falseResult;
  48.         $falseResult "''";
  49.     }
  50.  
  51.     // fetch true result if needed
  52.     if (!isset($trueResult)) {
  53.         $trueResult array_pop($rest);
  54.         // no true result provided so we use the expression arg
  55.         if ($trueResult === '?'{
  56.             $trueResult true;
  57.         }
  58.     }
  59.  
  60.     // remove the '?' if present
  61.     if (trim(end($rest)'"\''=== '?'{
  62.         array_pop($rest);
  63.     }
  64.  
  65.     // check params were correctly provided
  66.     if (empty($rest|| $trueResult === null || $falseResult === null{
  67.         throw new Dwoo_Compilation_Exception($compiler'Tif: you must provide three parameters serving as <expression> ? <true value> : <false value>');
  68.     }
  69.  
  70.     // parse condition
  71.     $condition Dwoo_Plugin_if::replaceKeywords($rest$compiler);
  72.  
  73.     return '(('.implode(' '$condition).') ? '.($trueResult===true implode(' '$condition$trueResult).' : '.$falseResult.')';
  74. }

Documentation generated on Sun, 07 Feb 2010 17:54:00 +0000 by phpDocumentor 1.4.0