eZ Publish  [4.0]
eztemplatephpoperator.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZTemplatePHPOperator class
00004 //
00005 // Created on: <01-Mar-2002 13:50:09 amos>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.0.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00011 // SOFTWARE LICENSE: GNU General Public License v2.0
00012 // NOTICE: >
00013 //   This program is free software; you can redistribute it and/or
00014 //   modify it under the terms of version 2.0  of the GNU General
00015 //   Public License as published by the Free Software Foundation.
00016 //
00017 //   This program is distributed in the hope that it will be useful,
00018 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //   GNU General Public License for more details.
00021 //
00022 //   You should have received a copy of version 2.0 of the GNU General
00023 //   Public License along with this program; if not, write to the Free
00024 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 //   MA 02110-1301, USA.
00026 //
00027 //
00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00029 //
00030 
00031 /*!
00032   \class eZTemplatePHPOperator eztemplatephpoperator.php
00033   \ingroup eZTemplateOperators
00034   \brief Makes it easy to add php functions as operators
00035 
00036   This class makes it easy to add existing PHP functions as template operators.
00037   It maps a template operator to a PHP function, the function must take one
00038   parameter and return the result.
00039   The redirection is done by supplying an associative array to the class,
00040   each key is the operatorname and the value is the PHP function name.
00041 
00042   Example:
00043 \code
00044 $tpl->registerOperators( new eZTemplatePHPOperator( array( "upcase" => "strtoupper",
00045                                                            "reverse" => "strrev" ) ) );
00046 \endcode
00047 */
00048 
00049 class eZTemplatePHPOperator
00050 {
00051     /*!
00052      Initializes the object with the redirection array.
00053     */
00054     function eZTemplatePHPOperator( $php_names )
00055     {
00056         if ( !is_array( $php_names ) )
00057             $php_names = array( $php_names );
00058         $this->PHPNames = $php_names;
00059         reset( $php_names );
00060         while ( list( $key, $val ) = each( $php_names ) )
00061         {
00062             $this->Operators[] = $key;
00063         }
00064     }
00065 
00066     /*!
00067      Returns the template operators.
00068     */
00069     function operatorList()
00070     {
00071         return $this->Operators;
00072     }
00073 
00074     function operatorTemplateHints()
00075     {
00076         $hints = array();
00077         foreach ( array_keys( $this->PHPNames ) as $name )
00078         {
00079             $hints[$name] = array( 'input' => true,
00080                                    'output' => true,
00081                                    'parameters' => false,
00082                                    'element-transformation' => true,
00083                                    'transform-parameters' => true,
00084                                    'input-as-parameter' => 'always',
00085                                    'element-transformation-func' => 'phpOperatorTransformation');
00086         }
00087         return $hints;
00088     }
00089 
00090     function phpOperatorTransformation( $operatorName, &$node, $tpl, &$resourceData,
00091                                         $element, $lastElement, $elementList, $elementTree, &$parameters )
00092     {
00093         $values = array();
00094         $function = $operatorName;
00095 
00096         if ( ( count( $parameters ) != 1) )
00097         {
00098             return false;
00099         }
00100         $newElements = array();
00101         $phpname = $this->PHPNames[$operatorName];
00102 
00103         $values[] = $parameters[0];
00104         $code = "%output% = $phpname( %1% );\n";
00105 
00106         $newElements[] = eZTemplateNodeTool::createCodePieceElement( $code, $values );
00107         return $newElements;
00108     }
00109 
00110     /*!
00111      Executes the PHP function for the operator $op_name.
00112     */
00113     function modify( $tpl, $operatorName, $operatorParameters, $rootNamespace, $currentNamespace, &$value, $namedParameters, $placement )
00114     {
00115         $phpname = $this->PHPNames[$operatorName];
00116         if ( $value !== null )
00117             $operand = $value;
00118         else
00119             $operand = $tpl->elementValue( $operatorParameters[0], $rootNamespace, $currentNamespace, $placement );
00120         $value = $phpname( $operand );
00121     }
00122 
00123     /// The array of operators, used for registering operators
00124     public $Operators;
00125     /// The associative array of operator/php function redirection
00126     public $PHPNames;
00127 }
00128 
00129 ?>