summaryrefslogtreecommitdiff
path: root/lib/phptal/PHPTAL/RepeatControllerGroups.php
blob: e6690ba5a4f3df3187430f199c0f54eb9ecb4804 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
/**
 * PHPTAL templating engine
 *
 * PHP Version 5
 *
 * @category HTML
 * @package  PHPTAL
 * @author   Laurent Bedubourg <lbedubourg@motion-twin.com>
 * @author   Kornel Lesiński <kornel@aardvarkmedia.co.uk>
 * @author   Iván Montes <drslump@pollinimini.net>
 * @license  http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
 * @version  SVN: $Id$
 * @link     http://phptal.org/
 */

/**
 * Keeps track of variable contents when using grouping in a path (first/ and last/)
 *
 * @package PHPTAL
 * @subpackage Php
 */
class PHPTAL_RepeatControllerGroups
{
    protected $dict = array();
    protected $cache = array();
    protected $data = null;
    protected $vars = array();
    protected $branch;


    public function __construct()
    {
        $this->dict = array();
        $this->reset();
    }

    /**
     * Resets the result caches. Use it to signal an iteration in the loop
     *
     */
    public function reset()
    {
        $this->cache = array();
    }

    /**
     * Checks if the data passed is the first one in a group
     *
     * @param mixed $data   The data to evaluate
     *
     * @return Mixed    True if the first item in the group, false if not and
     *                  this same object if the path is not finished
     */
    public function first($data)
    {
        if ( !is_array($data) && !is_object($data) && !is_null($data) ) {

            if ( !isset($this->cache['F']) ) {

                $hash = md5($data);

                if ( !isset($this->dict['F']) || $this->dict['F'] !== $hash ) {
                    $this->dict['F'] = $hash;
                    $res = true;
                } else {
                    $res = false;
                }

                $this->cache['F'] = $res;
            }

            return $this->cache['F'];
        }

        $this->data = $data;
        $this->branch = 'F';
        $this->vars = array();
        return $this;
    }

    /**
     * Checks if the data passed is the last one in a group
     *
     * @param mixed $data   The data to evaluate
     *
     * @return Mixed    True if the last item in the group, false if not and
     *                  this same object if the path is not finished
     */
    public function last($data)
    {
        if ( !is_array($data) && !is_object($data) && !is_null($data) ) {

            if ( !isset($this->cache['L']) ) {

                $hash = md5($data);

                if (empty($this->dict['L'])) {
                    $this->dict['L'] = $hash;
                    $res = false;
                } elseif ($this->dict['L'] !== $hash) {
                    $this->dict['L'] = $hash;
                    $res = true;
                } else {
                    $res = false;
                }

                $this->cache['L'] = $res;
            }

            return $this->cache['L'];
        }

        $this->data = $data;
        $this->branch = 'L';
        $this->vars = array();
        return $this;
    }

    /**
     * Handles variable accesses for the tal path resolver
     *
     * @param string $var   The variable name to check
     *
     * @return Mixed    An object/array if the path is not over or a boolean
     *
     * @todo    replace the PHPTAL_Context::path() with custom code
     */
    public function __get($var)
    {
        // When the iterator item is empty we just let the tal
        // expression consume by continuously returning this
        // same object which should evaluate to true for 'last'
        if ( is_null($this->data) ) {
            return $this;
        }

        // Find the requested variable
        $value = PHPTAL_Context::path($this->data, $var, true);

        // Check if it's an object or an array
        if ( is_array($value) || is_object($value) ) {
            // Move the context to the requested variable and return
            $this->data = $value;
            $this->addVarName($var);
            return $this;
        }

        // get a hash of the variable contents
        $hash = md5($value);

        // compute a path for the variable to use as dictionary key
        $path = $this->branch . $this->getVarPath() . $var;

        // If we don't know about this var store in the dictionary
        if ( !isset($this->cache[$path]) ) {

            if ( !isset($this->dict[$path]) ) {
                $this->dict[$path] = $hash;
                $res = $this->branch === 'F';
            } else {
                // Check if the value has changed
                if ($this->dict[$path] !== $hash) {
                    $this->dict[$path] = $hash;
                    $res = true;
                } else {
                    $res = false;
                }
            }

            $this->cache[$path] = $res;
        }

        return $this->cache[$path];

    }

    /**
     * Adds a variable name to the current path of variables
     *
     * @param string $varname  The variable name to store as a path part
     * @access protected
     */
    protected function addVarName($varname)
    {
        $this->vars[] = $varname;
    }

    /**
     * Returns the current variable path separated by a slash
     *
     * @return String  The current variable path
     * @access protected
     */
    protected function getVarPath()
    {
        return implode('/', $this->vars) . '/';
    }
}