1<?php 2 3/** 4 * Smarty Method RegisterFilter 5 * 6 * Smarty::registerFilter() method 7 * 8 * @package Smarty 9 * @subpackage PluginsInternal 10 * @author Uwe Tews 11 */ 12class Smarty_Internal_Method_RegisterFilter 13{ 14 /** 15 * Valid for Smarty and template object 16 * 17 * @var int 18 */ 19 public $objMap = 3; 20 21 /** 22 * Valid filter types 23 * 24 * @var array 25 */ 26 private $filterTypes = array('pre' => true, 'post' => true, 'output' => true, 'variable' => true); 27 28 /** 29 * Registers a filter function 30 * 31 * @api Smarty::registerFilter() 32 * 33 * @link http://www.smarty.net/docs/en/api.register.filter.tpl 34 * 35 * @param \Smarty_Internal_TemplateBase|\Smarty_Internal_Template|\Smarty $obj 36 * @param string $type filter type 37 * @param callback $callback 38 * @param string|null $name optional filter name 39 * 40 * @return \Smarty|\Smarty_Internal_Template 41 * @throws \SmartyException 42 */ 43 public function registerFilter(Smarty_Internal_TemplateBase $obj, $type, $callback, $name = null) 44 { 45 $smarty = $obj->_getSmartyObj(); 46 $this->_checkFilterType($type); 47 $name = isset($name) ? $name : $this->_getFilterName($callback); 48 if (!is_callable($callback)) { 49 throw new SmartyException("{$type}filter '{$name}' not callable"); 50 } 51 $smarty->registered_filters[ $type ][ $name ] = $callback; 52 return $obj; 53 } 54 55 /** 56 * Return internal filter name 57 * 58 * @param callback $function_name 59 * 60 * @return string internal filter name 61 */ 62 public function _getFilterName($function_name) 63 { 64 if (is_array($function_name)) { 65 $_class_name = (is_object($function_name[ 0 ]) ? get_class($function_name[ 0 ]) : $function_name[ 0 ]); 66 return $_class_name . '_' . $function_name[ 1 ]; 67 } elseif (is_string($function_name)) { 68 return $function_name; 69 } else { 70 return 'closure'; 71 } 72 } 73 74 /** 75 * Check if filter type is valid 76 * 77 * @param string $type 78 * 79 * @throws \SmartyException 80 */ 81 public function _checkFilterType($type) 82 { 83 if (!isset($this->filterTypes[ $type ])) { 84 throw new SmartyException("Illegal filter type '{$type}'"); 85 } 86 } 87} 88