You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smarty_internal_method_registerfilter.php 2.4KB

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