Completed
Branch feature/refactor (5950f1)
by Andrea Marco
01:25
created

DateTransformation   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
eloc 9
dl 0
loc 21
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A apply() 0 13 2
1
<?php
2
3
namespace Cerbero\Transformer\Transformations;
4
5
use DateTime;
6
use Illuminate\Support\Carbon;
7
8
/**
9
 * Transform a value into a date time instance or a formatted date.
10
 *
11
 */
12
class DateTransformation extends AbstractTransformation
13
{
14
    /**
15
     * Apply the transformation
16
     *
17
     * @param array $parameters
18
     * @return mixed
19
     */
20 3
    public function apply(array $parameters)
21
    {
22 3
        if (!isset($parameters[0])) {
23 3
            return new DateTime($this->value);
24
        }
25
26 3
        $format = $parameters[0];
27 3
        $fromTimezone = $parameters[1] ?? 'UTC';
28 3
        $toTimezone = $parameters[2] ?? 'UTC';
29
30 3
        return Carbon::parse($this->value, $fromTimezone)
31 3
            ->timezone($toTimezone)
32 3
            ->format($format);
33
    }
34
}
35