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.     // fetch false result and remove the ":" if it was present
  33.     $falseResult array_pop($rest);
  34.  
  35.     if (trim(end($rest)'"\''=== ':'{
  36.         // remove the ':' if present
  37.         array_pop($rest);
  38.     elseif (trim(end($rest)'"\''=== '?' || count($rest=== 1{
  39.         // there was in fact no false result provided, so we move it to be the true result instead
  40.         $trueResult $falseResult;
  41.         $falseResult "''";
  42.     }
  43.  
  44.     // fetch true result if needed
  45.     if (!isset($trueResult)) {
  46.         $trueResult array_pop($rest);
  47.         // no true result provided so we use the expression arg
  48.         if ($trueResult === '?'{
  49.             $trueResult true;
  50.         }
  51.     }
  52.  
  53.     // remove the '?' if present
  54.     if (trim(end($rest)'"\''=== '?'{
  55.         array_pop($rest);
  56.     }
  57.  
  58.     // check params were correctly provided
  59.     if (empty($rest|| empty($trueResult|| empty($falseResult)) {
  60.         throw new Dwoo_Compilation_Exception($compiler'Tif: you must provide three parameters serving as <expression> ? <true value> : <false value>');
  61.     }
  62.  
  63.     // parse condition
  64.     $condition Dwoo_Plugin_if::replaceKeywords($rest$compiler);
  65.  
  66.     return '(('.implode(' '$condition).') ? '.($trueResult===true implode(' '$condition$trueResult).' : '.$falseResult.')';
  67. }

Documentation generated on Sat, 18 Jul 2009 21:05:24 +0200 by phpDocumentor 1.4.0