Completed
Pull Request — master (#105)
by
unknown
02:53
created

AbstractDateHandler::formatDateValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace Drupal\Driver\Fields\Drupal7;
4
5
/**
6
 * A base handler for the various types of Date fields in Drupal 7.
7
 */
8
abstract class AbstractDateHandler extends AbstractHandler {
9
10
  /**
11
   * The format in which the dates are saved in Drupal's database.
12
   *
13
   * @var string
14
   */
15
  protected $dateFormat = NULL;
16
17
  /**
18
   * Converts a date string into the format expected by Drupal.
19
   *
20
   * @return string
21
   *   The re-formatted date string.
22
   */
23
  protected function formatDateValue($value) {
24
25
    if (empty($this->dateFormat)) {
26
      return $value;
27
    }
28
29
    $date = new \DateTime($value);
30
    return $date->format($this->dateFormat);
31
  }
32
33
  /**
34
   * {@inheritdoc}
35
   */
36
  public function expand($values) {
37
38
    $return = array();
39
    foreach ($values as $value) {
40
      if (is_array($value) && isset($this->fieldInfo['columns']['value2'])) {
41
        $return[$this->language][] = array(
42
          'value' => $this->formatDateValue($value[0]),
43
          'value2' => $this->formatDateValue($value[1]),
44
        );
45
      }
46
      else {
47
        $return[$this->language][] = array(
48
          'value' => $this->formatDateValue($value),
49
        );
50
      }
51
    }
52
    return $return;
53
  }
54
55
}
56