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.  * This file is released under the LGPL
  14.  * "GNU Lesser General Public License"
  15.  * More information can be found here:
  16.  * {@link http://www.gnu.org/copyleft/lesser.html}
  17.  *
  18.  * @author     Jordi Boggiano <j.boggiano@seld.be>
  19.  * @copyright  Copyright (c) 2008, Jordi Boggiano
  20.  * @license    http://www.gnu.org/copyleft/lesser.html  GNU Lesser General Public License
  21.  * @link       http://dwoo.org/
  22.  * @version    0.9.1
  23.  * @date       2008-05-30
  24.  * @package    Dwoo
  25.  */
  26. function Dwoo_Plugin_tif_compile(Dwoo_Compiler $compilerarray $rest)
  27. {
  28.     // load if plugin
  29.     if (!class_exists('Dwoo_Plugin_if'false)) {
  30.         try {
  31.             $compiler->getDwoo()->getLoader()->loadPlugin('if');
  32.         catch (Exception $e{
  33.             throw new Dwoo_Compilation_Exception($compiler'Tif: the if plugin is required to use Tif');
  34.         }
  35.     }
  36.  
  37.     // fetch false result and remove the ":" if it was present
  38.     $falseResult array_pop($rest);
  39.  
  40.     if (trim(end($rest)'"\''=== ':'{
  41.         // remove the ':' if present
  42.         array_pop($rest);
  43.     elseif (trim(end($rest)'"\''=== '?' || count($rest=== 1{
  44.         // there was in fact no false result provided, so we move it to be the true result instead
  45.         $trueResult $falseResult;
  46.         $falseResult "''";
  47.     }
  48.  
  49.     // fetch true result if needed
  50.     if (!isset($trueResult)) {
  51.         $trueResult array_pop($rest);
  52.         // no true result provided so we use the expression arg
  53.         if ($trueResult === '?'{
  54.             $trueResult true;
  55.         }
  56.     }
  57.  
  58.     // remove the '?' if present
  59.     if (trim(end($rest)'"\''=== '?'{
  60.         array_pop($rest);
  61.     }
  62.  
  63.     // check params were correctly provided
  64.     if (empty($rest|| empty($trueResult|| empty($falseResult)) {
  65.         throw new Dwoo_Compilation_Exception($compiler'Tif: you must provide three parameters serving as <expression> ? <true value> : <false value>');
  66.     }
  67.  
  68.     // parse condition
  69.     $condition Dwoo_Plugin_if::replaceKeywords($rest$compiler);
  70.  
  71.     return '(('.implode(' '$condition).') ? '.($trueResult===true implode(' '$condition$trueResult).' : '.$falseResult.')';
  72. }

Documentation generated on Sun, 07 Sep 2008 23:58:00 +0200 by phpDocumentor 1.4.0