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

AbstractDateHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 47
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
getDateFormat() 0 1 ?
A formatDateValue() 0 5 1
A expand() 0 20 4
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