PluginCycle   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 2

1 Method

Rating   Name   Duplication   Size   Complexity  
C process() 0 41 11
1
<?php
2
/**
3
 * Copyright (c) 2013-2017
4
 *
5
 * @category  Library
6
 * @package   Dwoo\Plugins\Functions
7
 * @author    Jordi Boggiano <[email protected]>
8
 * @author    David Sanchez <[email protected]>
9
 * @copyright 2008-2013 Jordi Boggiano
10
 * @copyright 2013-2017 David Sanchez
11
 * @license   http://dwoo.org/LICENSE Modified BSD License
12
 * @version   1.3.2
13
 * @date      2017-01-06
14
 * @link      http://dwoo.org/
15
 */
16
17
namespace Dwoo\Plugins\Functions;
18
19
use Dwoo\Plugin;
20
21
/**
22
 * Cycles between several values and returns one of them on each call
23
 * <pre>
24
 *  * name : the cycler name, specify if you need to have multiple concurrent cycles running
25
 *  * values : an array of values or a string of values delimited by $delimiter
26
 *  * print : if false, the pointer will go to the next one but not print anything
27
 *  * advance : if false, the pointer will not advance to the next value
28
 *  * delimiter : the delimiter used to split values if they are provided as a string
29
 *  * assign : if set, the value is saved in that variable instead of being output
30
 *  * reset : if true, the pointer is reset to the first value
31
 * </pre>
32
 * This software is provided 'as-is', without any express or implied warranty.
33
 * In no event will the authors be held liable for any damages arising from the use of this software.
34
 */
35
class PluginCycle extends Plugin
36
{
37
    protected $cycles = array();
38
39
    /**
40
     * @param string $name
41
     * @param null   $values
42
     * @param bool   $print
43
     * @param bool   $advance
44
     * @param string $delimiter
45
     * @param null   $assign
46
     * @param bool   $reset
47
     *
48
     * @return string|null
49
     */
50
    public function process($name = 'default', $values = null, $print = true, $advance = true, $delimiter = ',', $assign = null, $reset = false)
51
    {
52
        if ($values !== null) {
53
            if (is_string($values)) {
54
                $values = explode($delimiter, $values);
55
            }
56
57
            if (!isset($this->cycles[$name]) || $this->cycles[$name]['values'] !== $values) {
58
                $this->cycles[$name]['index'] = 0;
59
            }
60
61
            $this->cycles[$name]['values'] = array_values($values);
62
        } elseif (isset($this->cycles[$name])) {
63
            $values = $this->cycles[$name]['values'];
64
        }
65
66
        if ($reset) {
67
            $this->cycles[$name]['index'] = 0;
68
        }
69
70
        if ($print) {
71
            $out = $values[$this->cycles[$name]['index']];
72
        } else {
73
            $out = null;
74
        }
75
76
        if ($advance) {
77
            if ($this->cycles[$name]['index'] >= count($values) - 1) {
78
                $this->cycles[$name]['index'] = 0;
79
            } else {
80
                ++ $this->cycles[$name]['index'];
81
            }
82
        }
83
84
        if ($assign === null) {
85
            return $out;
86
        }
87
        $this->core->assignInScope($out, $assign);
88
89
        return null;
90
    }
91
}
92