Completed
Push — master ( 1910c5...2d50e1 )
by Basil
53:47
created

OpeningHoursValue   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setDay() 0 18 5
A getValue() 0 4 1
1
<?php
2
3
namespace luya\web\jsonld;
4
5
use yii\base\InvalidConfigException;
6
7
8
/**
9
 * Opening Hours value.
10
 * 
11
 * Days: Mo, Tu, We, Th, Fr, Sa, Su.
12
 * Time: Times are specified using 24:00 time. For example, 3pm is specified as 15:00.
13
 * 
14
 * Output: Mo 10:00-23:00, We 11:00-22:00
15
 * 
16
 * @author Basil Suter <[email protected]>
17
 * @since 1.0.14
18
 */
19
class OpeningHoursValue extends BaseValue
20
{
21
    private $_days = [];
22
    
23
    const DAY_MONDAY = 1;
24
    const DAY_TUESDAY = 2;
25
    const DAY_WEDNESDAY = 3;
26
    const DAY_THURSDAY = 4;
27
    const DAY_FRIDAY = 5;
28
    const DAY_SATURDAY = 6;
29
    const DAY_SUNDAY = 7;
30
31
    private $_dayMap = [
32
        self::DAY_MONDAY => 'Mo',
33
        self::DAY_TUESDAY => 'Tu',
34
        self::DAY_WEDNESDAY => 'We',
35
        self::DAY_THURSDAY => 'Th',
36
        self::DAY_FRIDAY => 'Fr',
37
        self::DAY_SATURDAY => 'Sa',
38
        self::DAY_SUNDAY => 'Su',
39
    ];
40
41
    /**
42
     * Set a a value for a day.
43
     * 
44
     * ```php
45
     * setDay(OpeningHoursValue::DAY_MONDAY, ['08:00' => '12:00', '14:00' => '20:00']);
46
     * ```
47
     *
48
     * @param [type] $name
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
49
     * @param array $time
50
     * @return void
51
     */
52
    public function setDay($name, array $time)
53
    {
54
        if (is_numeric($name)) {
55
            if (!array_key_exists($name, $this->_dayMap)) {
56
                throw new InvalidConfigException("The given day numeric value {$name} is not available in the list of valid day values.");
57
            }
58
59
            $name = $this->_dayMap[$name];
60
        }
61
62
        if (!array_search($name, $this->_dayMap)) {
63
            throw new InvalidConfigException("The given day {$name} is not in the list of valid day names.");
64
        }
65
66
        foreach ($time as $from => $to) {
67
            $this->_days[] = "{$name} {$from}-{$to}";
68
        }
69
    }
70
71
    public function getValue()
72
    {
73
        return implode(", ", $this->_days);
74
    }
75
}