Completed
Push — master ( 363bb4...91ed68 )
by Yohan
02:04
created

Factory::getStrictDates()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
16
17
/**
18
 * The period factory.
19
 *
20
 * Contains methods that instantiate periods from given arguments
21
 *
22
 * @author Yohan Giarelli <[email protected]>
23
 */
24
class Factory implements FactoryInterface
25
{
26
    /**
27
     * @var array
28
     */
29
    protected $options;
30
31
    /**
32
     * @var OptionsResolverInterface
33
     */
34
    protected $resolver;
35
36
    /**
37
     * @param array $options
38
     */
39
    public function __construct(array $options = array())
40
    {
41
        $this->options = $this->resolveOptions($options);
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function createSecond(\DateTime $begin)
48
    {
49
        return new $this->options['second_class']($begin, $this);
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function createMinute(\DateTime $begin)
56
    {
57
        return new $this->options['minute_class']($begin, $this);
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function createHour(\DateTime $begin)
64
    {
65
        return new $this->options['hour_class']($begin, $this);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function createDay(\DateTime $begin)
72
    {
73
        return new $this->options['day_class']($begin, $this);
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function createWeek(\DateTime $begin)
80
    {
81
        return new $this->options['week_class']($begin, $this);
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function createMonth(\DateTime $begin)
88
    {
89
        return new $this->options['month_class']($begin, $this);
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function createYear(\DateTime $begin)
96
    {
97
        return new $this->options['year_class']($begin, $this);
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function createRange(\DateTime $begin, \DateTime $end)
104
    {
105
        return new $this->options['range_class']($begin, $end, $this);
106
    }
107
108
    /**
109
     * @param string $name
110
     * @param mixed  $value
111
     */
112
    public function setOption($name, $value)
113
    {
114
        $this->resolver->replaceDefaults($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 OptionsResolverInterface $resolver
160
     */
161
    protected function setDefaultOptions(OptionsResolverInterface $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