Completed
Push — master ( 2d125d...510a23 )
by Gaetano
10:25
created

EzDateAndTime::hashToFieldSettings()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 7
nc 3
nop 2
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\ComplexField;
4
5
use Kaliop\eZMigrationBundle\API\FieldValueConverterInterface;
6
use Kaliop\eZMigrationBundle\API\FieldDefinitionConverterInterface;
7
8
class EzDateAndTime extends AbstractComplexField implements FieldValueConverterInterface, FieldDefinitionConverterInterface
9
{
10
    /**
11
     * @param int|string|null $fieldValue use a timestamp; if as string, prepend it with @ sign
12
     * @param array $context The context for execution of the current migrations. Contains f.e. the path to the migration
13
     * @return int|string
14
     */
15
    public function hashToFieldValue($fieldValue, array $context = array())
16
    {
17
        return $fieldValue;
18
    }
19
20
    /**
21
     * @param \eZ\Publish\Core\FieldType\DateAndTime\Value $fieldValue
22
     * @param array $context
23
     * @return int timestamp
24
     */
25
    public function fieldValueToHash($fieldValue, array $context = array())
26
    {
27
        $date = $fieldValue->value;
28
        return $date == null ? null: $date->getTimestamp();
29
    }
30
31
    public function fieldSettingsToHash($settingsValue, array $context = array())
32
    {
33
        if (is_array($settingsValue) && isset($settingsValue['dateInterval']) && is_object($settingsValue['dateInterval'])) {
34
            $settingsValue['dateInterval'] = $this->format($settingsValue['dateInterval']);
35
        }
36
        return $settingsValue;
37
    }
38
39
    public function hashToFieldSettings($settingsHash, array $context = array())
40
    {
41
        if (is_array($settingsHash) && isset($settingsHash['dateInterval']) && is_string($settingsHash['dateInterval'])) {
42
            // ISO-8601 format, loose matching because I am lazy
43
            if (preg_match('/^P[0-9YMDWTHS]+$/', $settingsHash['dateInterval'])) {
44
                $settingsHash['dateInterval'] = new \DateInterval($settingsHash['dateInterval']);
45
            } else {
46
                $settingsHash['dateInterval'] = \DateInterval::createFromDateString($settingsHash['dateInterval']);
47
            }
48
        }
49
        return $settingsHash;
50
    }
51
52
    /**
53
     * @param \DateInterval $dateInterval
54
     * @return string
55
     * @todo add support for DateInterval::$invert
56
     */
57
    protected function format(\DateInterval $dateInterval)
58
    {
59
        $format = array();
60
        if (0 != $dateInterval->y) {
61
            $format[] = $dateInterval->y.' years';
62
        }
63
        if (0 != $dateInterval->m) {
64
            $format[] = $dateInterval->m.' months';
65
        }
66
        if (0 != $dateInterval->d) {
67
            $format[] = $dateInterval->d.' days';
68
        }
69
        //if (0 < $dateInterval->h || 0 < $dateInterval->i || 0 < $dateInterval->s) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
70
        //    $format .= 'T';
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
71
        //}
72
        if (0 != $dateInterval->h) {
73
            $format[] = $dateInterval->h.' hours';
74
        }
75
        if (0 != $dateInterval->i) {
76
            $format[] = $dateInterval->i.' minutes';
77
        }
78
        if (0 != $dateInterval->s) {
79
            $format[] = $dateInterval->s.' seconds';
80
        }
81
        return implode( ', ', $format);
82
    }
83
}
84