Factory::setOption()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of CalendR, a Fréquence web project.
5
 *
6
 * (c) 2013 Fréquence web
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CalendR\Period;
13
14
use Symfony\Component\OptionsResolver\OptionsResolver;
15
16
/**
17
 * The period factory.
18
 *
19
 * Contains methods that instantiate periods from given arguments
20
 *
21
 * @author Yohan Giarelli <[email protected]>
22
 */
23
class Factory implements FactoryInterface
24
{
25
    /**
26
     * @var array
27
     */
28
    protected $options;
29
30
    /**
31
     * @var OptionsResolver
32
     */
33
    protected $resolver;
34
35
    /**
36
     * @param array $options
37
     */
38
    public function __construct(array $options = array())
39
    {
40
        $this->options = $this->resolveOptions($options);
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function createSecond(\DateTime $begin)
47
    {
48
        return new $this->options['second_class']($begin, $this);
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function createMinute(\DateTime $begin)
55
    {
56
        return new $this->options['minute_class']($begin, $this);
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function createHour(\DateTime $begin)
63
    {
64
        return new $this->options['hour_class']($begin, $this);
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function createDay(\DateTime $begin)
71
    {
72
        return new $this->options['day_class']($begin, $this);
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function createWeek(\DateTime $begin)
79
    {
80
        return new $this->options['week_class']($begin, $this);
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function createMonth(\DateTime $begin)
87
    {
88
        return new $this->options['month_class']($begin, $this);
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function createYear(\DateTime $begin)
95
    {
96
        return new $this->options['year_class']($begin, $this);
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function createRange(\DateTime $begin, \DateTime $end)
103
    {
104
        return new $this->options['range_class']($begin, $end, $this);
105
    }
106
107
    /**
108
     * @param string $name
109
     * @param mixed  $value
110
     */
111
    public function setOption($name, $value)
112
    {
113
        $this->resolver->clear();
114
        $this->resolver->setDefaults($this->options);
115
        $this->options = $this->resolver->resolve(array($name => $value));
116
    }
117
118
    /**
119
     * @param string $name
120
     *
121
     * @return mixed
122
     */
123
    public function getOption($name)
124
    {
125
        return $this->options[$name];
126
    }
127
128
    /**
129
     * @param array $options
130
     *
131
     * @return array
132
     */
133
    protected function resolveOptions(array $options)
134
    {
135
        if (null === $this->resolver) {
136
            $this->resolver = new OptionsResolver();
137
            $this->resolver->setDefaults(
138
                array(
139
                    'second_class' => 'CalendR\Period\Second',
140
                    'minute_class' => 'CalendR\Period\Minute',
141
                    'hour_class' => 'CalendR\Period\Hour',
142
                    'day_class' => 'CalendR\Period\Day',
143
                    'week_class' => 'CalendR\Period\Week',
144
                    'month_class' => 'CalendR\Period\Month',
145
                    'year_class' => 'CalendR\Period\Year',
146
                    'range_class' => 'CalendR\Period\Range',
147
                    'first_weekday' => Day::MONDAY,
148
                )
149
            );
150
            $this->setDefaultOptions($this->resolver);
151
        }
152
153
        return $this->resolver->resolve($options);
154
    }
155
156
    /**
157
     * Override this method if you have to change default/allowed options.
158
     *
159
     * @param OptionsResolver $resolver
160
     */
161
    protected function setDefaultOptions(OptionsResolver $resolver)
162
    {
163
    }
164
165
    /**
166
     * {@inheritdoc}
167
     */
168
    public function setFirstWeekday($firstWeekday)
169
    {
170
        $this->setOption('first_weekday', $firstWeekday);
171
    }
172
173
    /**
174
     * {@inheritdoc}
175
     */
176
    public function getFirstWeekday()
177
    {
178
        return $this->getOption('first_weekday');
179
    }
180
181
    /**
182
     * {@inheritdoc}
183
     */
184
    public function findFirstDayOfWeek($dateTime)
185
    {
186
        $day = clone $dateTime;
187
        $delta = ((int) $day->format('w') - $this->getFirstWeekday() + 7) % 7;
188
        $day->sub(new \DateInterval(sprintf('P%sD', $delta)));
189
190
        return $day;
191
    }
192
}
193