File: BaseRule.php

package info (click to toggle)
simplesamlphp 1.19.7-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 42,920 kB
  • sloc: php: 202,044; javascript: 14,867; xml: 2,700; sh: 225; perl: 82; makefile: 70; python: 5
file content (184 lines) | stat: -rw-r--r-- 5,121 bytes parent folder | download | duplicates (3)
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
<?php

namespace SimpleSAML\Module\statistics\Statistics\Rulesets;

use SimpleSAML\Configuration;
use SimpleSAML\Module\statistics\DateHandler;
use SimpleSAML\Module\statistics\DateHandlerMonth;
use SimpleSAML\Module\statistics\StatDataset;

/**
 * @author Andreas Åkre Solberg <andreas.solberg@uninett.no>
 * @package SimpleSAMLphp
 */
class BaseRule
{
    /** @var \SimpleSAML\Configuration */
    protected $statconfig;

    /** @var \SimpleSAML\Configuration */
    protected $ruleconfig;

    /** @var string */
    protected $ruleid = '';

    /** @var array */
    protected $available = [];


    /**
     * Constructor
     *
     * @param \SimpleSAML\Configuration $statconfig
     * @param \SimpleSAML\Configuration $ruleconfig
     * @param string $ruleid
     * @param array $available
     */
    public function __construct(Configuration $statconfig, Configuration $ruleconfig, $ruleid, array $available)
    {
        $this->statconfig = $statconfig;
        $this->ruleconfig = $ruleconfig;
        $this->ruleid = $ruleid;

        if (array_key_exists($ruleid, $available)) {
            $this->available = $available[$ruleid];
        }
    }


    /**
     * @return string
     */
    public function getRuleID()
    {
        return $this->ruleid;
    }


    /**
     * @return array
     */
    public function availableTimeRes()
    {
        $timeresConfigs = $this->statconfig->getValue('timeres');
        $available_times = [];
        foreach ($timeresConfigs as $tres => $tresconfig) {
            if (array_key_exists($tres, $this->available)) {
                $available_times[$tres] = $tresconfig['name'];
            }
        }
        return $available_times;
    }


    /**
     * @param string $timeres
     * @return array
     */
    public function availableFileSlots($timeres)
    {
        $timeresConfigs = $this->statconfig->getValue('timeres');
        $timeresConfig = $timeresConfigs[$timeres];

        if (isset($timeresConfig['customDateHandler']) && $timeresConfig['customDateHandler'] == 'month') {
            $datehandler = new DateHandlerMonth(0);
        } else {
            $datehandler = new DateHandler($this->statconfig->getValue('offset', 0));
        }

        /*
         * Get list of avaiable times in current file (rule)
         */
        $available_times = [];
        foreach ($this->available[$timeres] as $slot) {
            $available_times[$slot] = $datehandler->prettyHeader(
                $slot,
                $slot + 1,
                $timeresConfig['fileslot'],
                $timeresConfig['dateformat-period']
            );
        }
        return $available_times;
    }


    /**
     * @param string $preferTimeRes
     * @return string
     */
    protected function resolveTimeRes($preferTimeRes)
    {
        $timeresavailable = array_keys($this->available);
        $timeres = $timeresavailable[0];

        // Then check if the user have provided one that is valid
        if (in_array($preferTimeRes, $timeresavailable, true)) {
            $timeres = $preferTimeRes;
        }
        return $timeres;
    }


    /**
     * @param string $timeres
     * @param string $preferTime
     * @return int
     */
    protected function resolveFileSlot($timeres, $preferTime)
    {
        // Get which time (fileslot) to use.. First get a default, which is the most recent one.
        $fileslot = $this->available[$timeres][count($this->available[$timeres]) - 1];
        // Then check if the user have provided one.
        if (in_array($preferTime, $this->available[$timeres], true)) {
            $fileslot = $preferTime;
        }
        return $fileslot;
    }


    /**
     * @param string $timeres
     * @param string $preferTime
     * @return array
     */
    public function getTimeNavigation($timeres, $preferTime)
    {
        $fileslot = $this->resolveFileSlot($timeres, $preferTime);

        // Extract previous and next time slots...
        $available_times_prev = null;
        $available_times_next = null;

        $timeslots = array_values($this->available[$timeres]);
        sort($timeslots, SORT_NUMERIC);
        $timeslotindex = array_flip($timeslots);

        if ($timeslotindex[$fileslot] > 0) {
            $available_times_prev = $timeslots[$timeslotindex[$fileslot] - 1];
        }
        if ($timeslotindex[$fileslot] < (count($timeslotindex) - 1)) {
            $available_times_next = $timeslots[$timeslotindex[$fileslot] + 1];
        }
        return ['prev' => $available_times_prev, 'next' => $available_times_next];
    }


    /**
     * @param string $preferTimeRes
     * @param string $preferTime
     * @return \SimpleSAML\Module\statistics\StatDataset
     */
    public function getDataSet($preferTimeRes, $preferTime)
    {
        $timeres = $this->resolveTimeRes($preferTimeRes);
        $fileslot = $this->resolveFileSlot($timeres, $preferTime);
        $dataset = new StatDataset(
            $this->statconfig,
            $this->ruleconfig,
            $this->ruleid,
            $timeres,
            $fileslot
        );
        return $dataset;
    }
}