1<?php
2
3use dokuwiki\Utf8\PhpString;
4
5/**
6 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author     Thomas Schäfer <thomas@hilbershome.de>
8 */
9
10class helper_plugin_usecounter extends DokuWiki_Plugin {
11
12    protected $counterArray;
13
14    public function __construct() {
15        $this->counterArray = array();
16    }
17
18    /**
19     * This helper should be kept singular throughout the whole run.
20     */
21    public function isSingleton() {
22        return true;
23    }
24
25    public function getMethods()
26    {
27        $result = [];
28        $result[] = [
29            'name' => 'incUsageOf',
30            'desc' => 'Indicates that the object with the given identifier is used (once more).',
31            'params' => array(
32                'id' => 'string',
33				),
34        ];
35        $result[] = [
36            'name' => 'amountOfUses',
37            'desc' => 'Returns the amount of registered uses of this object.',
38            'params' => array(
39                'id' => 'string',
40				),
41            'return' => ['amount' => 'string'],
42        ];
43        $result[] = [
44            'name' => 'resetUseCounter',
45            'desc' => 'Resets the counter.',
46        ];
47        return $result;
48    }
49
50    /**
51      * Indicates that the object with the given identifier is used (once more).
52      *
53      * @param string $id The identifier/key name of the object.
54      */
55    public function incUsageOf(string $id) {
56
57        // Its important to keep using "amountOfUses" here in order to ensure
58        // initialization of the array entry for this $id!
59        $this->counterArray[$id] = $this->amountOfUses($id) + 1;
60    }
61
62    /**
63      * Returns the amount of registered uses of this object.
64      *
65      * @param string $id The identifier/key name of the object.
66      *
67      * @return The amount of uses that were registered via incUsageOf().
68      */
69    public function amountOfUses(string $id) {
70        if (!array_key_exists($id, $this->counterArray)) {
71            $this->counterArray[$id] = 0;
72        }
73
74        return $this->counterArray[$id];
75    }
76
77    /**
78      * Resets the counter. This method is supposed to be called
79      * automatically by this plugin's action module.
80      */
81    public function resetUseCounter() {
82        $this->counterArray = array();
83    }
84}
85