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

AbstractDateHandler::expand()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.2
c 0
b 0
f 0
nc 4
cc 4
eloc 12
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
   * Get the format in which the dates are saved in Drupal's database.
12
   *
13
   * @return string
14
   *   The format in which the dates are saved in Drupal's database.
15
   */
16
  abstract protected function getDateFormat();
17
18
  /**
19
   * Converts a date string into the format expected by Drupal.
20
   *
21
   * @return string
22
   *   The re-formatted date string.
23
   */
24
  protected function formatDateValue($value) {
25
26
    $date = new \DateTime($value);
27
    return $date->format($this->getDateFormat());
28
  }
29
30
  /**
31
   * {@inheritdoc}
32
   */
33
  public function expand($values) {
34
35
    $return = array();
36
    if (isset($this->fieldInfo['columns']['value2'])) {
37
      foreach ($values as $value) {
38
        $return[$this->language][] = array(
39
          'value' => $this->formatDateValue($value[0]),
40
          'value2' => $this->formatDateValue($value[1]),
41
        );
42
      }
43
    }
44
    else {
45
      foreach ($values as $value) {
46
        $return[$this->language][] = array(
47
          'value' => $this->formatDateValue($value),
48
        );
49
      }
50
    }
51
    return $return;
52
  }
53
54
}
55