1: <?php
2: /**
3: * Copyright (c) CMNWorks
4: *
5: * Licensed under The MIT License
6: * For full copyright and license information, please see the LICENSE.txt
7: * Redistributions of files must retain the above copyright notice.
8: *
9: * @copyright Copyright (c) CMNWorks Christopher M. Natan
10: * @author Christopher M. Natan
11: * @link http://cmnworks.com
12: * @since 1.8.8
13: * @license http://www.opensource.org/licenses/mit-license.php MIT License
14: */
15: namespace Cakestrap\View\Helper;
16:
17: use Cake\View\StringTemplate;
18: use Cake\View\Helper;
19:
20: /**
21: * Handles magic methods for Bootstrap helpers.
22: *
23: * @package Cakestrap\View\Helper
24: */
25: abstract class Basic extends Helper
26: {
27: /**
28: * Contains a particular html template
29: *
30: * @var string
31: */
32: protected $_template;
33:
34: /**
35: * A variable that contains the CakePHP StringTemplate object
36: *
37: * @var StringTemplate object
38: */
39: protected $_stringTemplate;
40:
41: /**
42: * Default helper options.
43: * These options are merged with the user-provided options.
44: *
45: * @var array
46: */
47: protected $_options = [];
48:
49: /**
50: * Variable that cloned the default param options.
51: *
52: * @var array
53: */
54: protected $_clone;
55:
56: /**
57: * Collected contents from the magic methods
58: *
59: * @var array
60: */
61: protected $_contents;
62:
63: /**
64: * PHP reserves all function names starting with __ as magical.
65: *
66: * @param string $method
67: * @param array $args
68: * @return object $this
69: */
70: public function __call($method, $args)
71: {
72: $this->{$method} = (isset($args[0]) ? $args[0] : $args);
73:
74: return $this;
75: }
76:
77: /**
78: * This method will merge the default helper options
79: * and user-provided options.
80: *
81: * @param array $options
82: * @return object $this
83: */
84: public function options($options = [])
85: {
86: $this->_clone = $this->_options;
87: $this->_options = array_merge($this->_options, $options);
88:
89: return $this;
90: }
91:
92: /**
93: * For helpers that supports body
94: *
95: * @param array | string $body
96: * @return object $this
97: */
98: public function body($body)
99: {
100: if(is_array($body)) {
101: $this->body = implode('', $body);
102: } else {
103: $this->body = $body;
104: }
105:
106: return $this;
107: }
108:
109: /**
110: * Automatically assign bootstrap template to loaded helper.
111: *
112: * @param string $name Template name
113: * @return void
114: */
115: public function assignTemplate($name = null)
116: {
117: $template = new Templates();
118: $exist = method_exists($template, $name);
119:
120: if($exist) {
121: $template = (array)$template::$name();
122: $this->_stringTemplate = new StringTemplate();
123: $this->_stringTemplate->add($template);
124: }
125:
126: $this->_template = $template;
127: }
128:
129: /**
130: * Now we need to reset the values to support multiple
131: * bootstrap helper in a page.
132: *
133: * @return void
134: */
135: protected function _reset()
136: {
137: $this->_options = $this->_clone;
138: $this->_contents = [];
139: $this->_item = [];
140: $this->_button = [];
141: $this->_active = null;
142: $this->_in = null;
143: $this->radio = null;
144: $this->checked = null;
145: }
146: }