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

DateTransformation::getDate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
ccs 3
cts 4
cp 0.75
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.0625
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