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

AbstractDateHandler   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 50
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A formatDateValue() 0 9 2
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
   * 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
    if (isset($this->fieldInfo['columns']['value2'])) {
40
      foreach ($values as $value) {
41
        $return[$this->language][] = array(
42
          'value' => $this->formatDateValue($value[0]),
43
          'value2' => $this->formatDateValue($value[1]),
44
        );
45
      }
46
    }
47
    else {
48
      foreach ($values as $value) {
49
        $return[$this->language][] = array(
50
          'value' => $this->formatDateValue($value),
51
        );
52
      }
53
    }
54
    return $return;
55
  }
56
57
}
58