Timezone::transform()   B
last analyzed

Complexity

Conditions 6
Paths 7

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 25
c 0
b 0
f 0
ccs 12
cts 12
cp 1
rs 8.439
nc 7
cc 6
eloc 13
nop 2
crap 6
1
<?php
2
/**
3
 * inetprocess/transformation
4
 *
5
 * PHP Version 5.3
6
 *
7
 * @author Emmanuel Dyan
8
 * @copyright 2005-2015 iNet Process
9
 *
10
 * @package inetprocess/transformation
11
 *
12
 * @license GNU General Public License v2.0
13
 *
14
 * @link http://www.inetprocess.com
15
 */
16
17
namespace Inet\Transformation\Rule;
18
19
use Inet\Transformation\Exception\TransformationException;
20
21
/**
22
 * Transforms a Date format to another
23
 */
24
class Timezone extends AbstractRule
25
{
26
    /**
27
     * Operate the transformation
28
     *
29
     * @param string $input
30
     * @param array  $arguments
31
     *
32
     * @throws Inet\Transformation\Exception\TransformationException
33
     *
34
     * @return string
35
     */
36 7
    public function transform($input, array $arguments)
37
    {
38
        // I should have two arguments: old format / new format
39 7
        if (count($arguments) !== 2 && count($arguments) !== 3) {
40 2
            throw new TransformationException('Rule Timezone Expects 2 or 3 arguments');
41
        }
42
43
        // Validate the date with the format provided
44 5
        $timezone = (array_key_exists(2, $arguments) ? $arguments[2] : date_default_timezone_get());
45 5
        $dateTime = \DateTime::createFromFormat($arguments[0], $input, new \DateTimeZone($timezone));
46 5
        if ($dateTime === false) {
47 1
            throw new TransformationException("Input ($input) or format ({$arguments[0]}) is not valid");
48
        }
49
50
        // Change the TimeZone
51
        try {
52 4
            $dateTime->setTimezone(new \DateTimeZone($arguments[1]));
53 4
        } catch (\Exception $e) {
54 1
            throw new TransformationException("Timezone '{$arguments[1]}' is not valid");
55
        }
56
57 3
        $output = $dateTime->format($arguments[0]);
58
59 3
        return $output;
60
    }
61
}
62