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

AbstractDateHandler   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A formatDateValue() 0 9 2
A expand() 0 18 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
    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