DateTransformer::transform()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
/*
3
 * WellCommerce Open-Source E-Commerce Platform
4
 *
5
 * This file is part of the WellCommerce package.
6
 *
7
 * (c) Adam Piotrowski <[email protected]>
8
 *
9
 * For the full copyright and license information,
10
 * please view the LICENSE file that was distributed with this source code.
11
 */
12
13
namespace WellCommerce\Component\Form\DataTransformer;
14
15
use DateTime;
16
use Symfony\Component\PropertyAccess\PropertyAccess;
17
use Symfony\Component\PropertyAccess\PropertyPathInterface;
18
19
/**
20
 * Class DateTransformer
21
 *
22
 * @author  Adam Piotrowski <[email protected]>
23
 */
24
class DateTransformer implements DataTransformerInterface
25
{
26
    /**
27
     * @var string Date format
28
     */
29
    private $format;
30
31
    /**
32
     * @var \Symfony\Component\PropertyAccess\PropertyAccessor
33
     */
34
    private $propertyAccessor;
35
36
    /**
37
     * Constructor
38
     *
39
     * @param $format
40
     */
41
    public function __construct($format)
42
    {
43
        $this->format           = $format;
44
        $this->propertyAccessor = PropertyAccess::createPropertyAccessor();
45
    }
46
47
    /**
48
     * Transforms date object to string using given format
49
     *
50
     * @param DateTime|string $dateTime
0 ignored issues
show
Bug introduced by
There is no parameter named $dateTime. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
51
     *
52
     * @return string
53
     */
54
    public function transform($modelData)
55
    {
56
        if ($modelData instanceof DateTime) {
57
            return $modelData->format($this->format);
58
        }
59
60
        return null;
61
    }
62
63
    /**
64
     * Transforms date string to DateTime object
65
     *
66
     * @param object                $modelData
67
     * @param PropertyPathInterface $propertyPath
68
     * @param mixed                 $value
69
     */
70
    public function reverseTransform($modelData, PropertyPathInterface $propertyPath, $value)
71
    {
72
        if (false === $date = $this->createDateFromString($value)) {
73
            $date = null;
74
        }
75
76
        $this->propertyAccessor->setValue($modelData, $propertyPath, $date);
77
    }
78
79
    /**
80
     * Creates DateTime from string
81
     *
82
     * @param $date
83
     *
84
     * @return DateTime|false
85
     */
86
    private function createDateFromString($date)
87
    {
88
        return DateTime::createFromFormat($this->format, $date);
89
    }
90
}
91