DateTransformation::apply()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 10
cc 2
nc 2
nop 1
crap 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