PluginCounter   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
D process() 0 45 15
1
<?php
2
/**
3
 * Copyright (c) 2013-2016
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-2016 David Sanchez
11
 * @license   http://dwoo.org/LICENSE Modified BSD License
12
 * @version   1.3.0
13
 * @date      2016-09-19
14
 * @link      http://dwoo.org/
15
 */
16
17
namespace Dwoo\Plugins\Functions;
18
19
use Dwoo\Plugin;
20
21
/**
22
 * Initiates a counter that is incremented every time you call it
23
 * <pre>
24
 *  * name : the counter name, define it if you want to have multiple concurrent counters
25
 *  * start : the start value, if it's set, it will reset the counter to this value, defaults to 1
26
 *  * skip : the value to add to the counter at each call, defaults to 1
27
 *  * direction : "up" (default) or "down" to define whether the counter increments or decrements
28
 *  * print : if false, the counter will not output the current count, defaults to true
29
 *  * assign : if set, the counter is saved into the given variable and does not output anything, overriding the print
30
 *  parameter
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 PluginCounter extends Plugin
36
{
37
    protected $counters = array();
38
39
    /**
40
     * @param string $name
41
     * @param null   $start
42
     * @param null   $skip
43
     * @param null   $direction
44
     * @param null   $print
45
     * @param null   $assign
46
     *
47
     * @return mixed
48
     */
49
    public function process($name = 'default', $start = null, $skip = null, $direction = null, $print = null, $assign = null)
50
    {
51
        // init counter
52
        if (!isset($this->counters[$name])) {
53
            $this->counters[$name] = array(
54
                'count'     => $start === null ? 1 : (int)$start,
55
                'skip'      => $skip === null ? 1 : (int)$skip,
56
                'print'     => $print === null ? true : (bool)$print,
57
                'assign'    => $assign === null ? null : (string)$assign,
58
                'direction' => strtolower($direction) === 'down' ? - 1 : 1,
59
            );
60
        } // increment
61
        else {
62
            // override setting if present
63
            if ($skip !== null) {
64
                $this->counters[$name]['skip'] = (int)$skip;
65
            }
66
67
            if ($direction !== null) {
68
                $this->counters[$name]['direction'] = strtolower($direction) === 'down' ? - 1 : 1;
69
            }
70
71
            if ($print !== null) {
72
                $this->counters[$name]['print'] = (bool)$print;
73
            }
74
75
            if ($assign !== null) {
76
                $this->counters[$name]['assign'] = (string)$assign;
77
            }
78
79
            if ($start !== null) {
80
                $this->counters[$name]['count'] = (int)$start;
81
            } else {
82
                $this->counters[$name]['count'] += ($this->counters[$name]['skip'] * $this->counters[$name]['direction']);
83
            }
84
        }
85
86
        $out = $this->counters[$name]['count'];
87
88
        if ($this->counters[$name]['assign'] !== null) {
89
            $this->core->assignInScope($out, $this->counters[$name]['assign']);
90
        } elseif ($this->counters[$name]['print'] === true) {
91
            return $out;
92
        }
93
    }
94
}
95