1<?php
2
3/**
4 * Smarty Method RegisterObject
5 *
6 * Smarty::registerObject() method
7 *
8 * @package    Smarty
9 * @subpackage PluginsInternal
10 * @author     Uwe Tews
11 */
12class Smarty_Internal_Method_RegisterObject
13{
14    /**
15     * Valid for Smarty and template object
16     *
17     * @var int
18     */
19    public $objMap = 3;
20
21    /**
22     * Registers object to be used in templates
23     *
24     * @api  Smarty::registerObject()
25     * @link http://www.smarty.net/docs/en/api.register.object.tpl
26     *
27     * @param \Smarty_Internal_TemplateBase|\Smarty_Internal_Template|\Smarty $obj
28     * @param string                                                          $object_name
29     * @param object                                                          $object                     the
30     *                                                                                                    referenced
31     *                                                                                                    PHP
32     *                                                                                                    object
33     *                                                                                                    to
34     *                                                                                                    register
35     *
36     * @param array                                                           $allowed_methods_properties list of
37     *                                                                                                    allowed
38     *                                                                                                    methods
39     *                                                                                                    (empty
40     *                                                                                                    = all)
41     *
42     * @param bool                                                            $format                     smarty
43     *                                                                                                    argument
44     *                                                                                                    format,
45     *                                                                                                    else
46     *                                                                                                    traditional
47     *
48     * @param array                                                           $block_methods              list of
49     *                                                                                                    block-methods
50     *
51     * @return \Smarty|\Smarty_Internal_Template
52     * @throws \SmartyException
53     */
54    public function registerObject(
55        Smarty_Internal_TemplateBase $obj,
56        $object_name,
57        $object,
58        $allowed_methods_properties = array(),
59        $format = true,
60        $block_methods = array()
61    ) {
62        $smarty = $obj->_getSmartyObj();
63        // test if allowed methods callable
64        if (!empty($allowed_methods_properties)) {
65            foreach ((array)$allowed_methods_properties as $method) {
66                if (!is_callable(array($object, $method)) && !property_exists($object, $method)) {
67                    throw new SmartyException("Undefined method or property '$method' in registered object");
68                }
69            }
70        }
71        // test if block methods callable
72        if (!empty($block_methods)) {
73            foreach ((array)$block_methods as $method) {
74                if (!is_callable(array($object, $method))) {
75                    throw new SmartyException("Undefined method '$method' in registered object");
76                }
77            }
78        }
79        // register the object
80        $smarty->registered_objects[ $object_name ] =
81            array($object, (array)$allowed_methods_properties, (boolean)$format, (array)$block_methods);
82        return $obj;
83    }
84}
85