Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / migrate / src / Plugin / migrate / process / FormatDate.php
1 <?php
2
3 namespace Drupal\migrate\Plugin\migrate\process;
4
5 use Drupal\Component\Datetime\DateTimePlus;
6 use Drupal\migrate\MigrateException;
7 use Drupal\migrate\MigrateExecutableInterface;
8 use Drupal\migrate\ProcessPluginBase;
9 use Drupal\migrate\Row;
10
11 /**
12  * Converts date/datetime from one format to another.
13  *
14  * Available configuration keys
15  * - from_format: The source format string as accepted by
16  *   @link http://php.net/manual/datetime.createfromformat.php \DateTime::createFromFormat. @endlink
17  * - to_format: The destination format.
18  * - timezone: String identifying the required time zone, see
19  *   DateTimePlus::__construct().
20  * - settings: keyed array of settings, see DateTimePlus::__construct().
21  *
22  * Examples:
23  *
24  * Example usage for date only fields (DATETIME_DATE_STORAGE_FORMAT):
25  * @code
26  * process:
27  *   field_date:
28  *     plugin: format_date
29  *     from_format: 'm/d/Y'
30  *     to_format: 'Y-m-d'
31  *     source: event_date
32  * @endcode
33  *
34  * If the source value was '01/05/1955' the transformed value would be
35  * 1955-01-05.
36  *
37  * Example usage for datetime fields (DATETIME_DATETIME_STORAGE_FORMAT):
38  * @code
39  * process:
40  *   field_time:
41  *     plugin: format_date
42  *     from_format: 'm/d/Y H:i:s'
43  *     to_format: 'Y-m-d\TH:i:s'
44  *     source: event_time
45  * @endcode
46  *
47  * If the source value was '01/05/1955 10:43:22' the transformed value would be
48  * 1955-01-05T10:43:22.
49  *
50  * Example usage for datetime fields with a timezone and settings:
51  * @code
52  * process:
53  *   field_time:
54  *     plugin: format_date
55  *     from_format: 'Y-m-d\TH:i:sO'
56  *     to_format: 'Y-m-d\TH:i:s'
57  *     timezone: 'America/Managua'
58  *     settings:
59  *       validate_format: false
60  *     source: event_time
61  * @endcode
62  *
63  * If the source value was '2004-12-19T10:19:42-0600' the transformed value
64  * would be 2004-12-19T10:19:42.
65  *
66  * @see \DateTime::createFromFormat()
67  * @see \Drupal\Component\Datetime\DateTimePlus::__construct()
68  * @see \Drupal\migrate\Plugin\MigrateProcessInterface
69  *
70  * @MigrateProcessPlugin(
71  *   id = "format_date"
72  * )
73  */
74 class FormatDate extends ProcessPluginBase {
75
76   /**
77    * {@inheritdoc}
78    */
79   public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
80     if (empty($value)) {
81       return '';
82     }
83
84     // Validate the configuration.
85     if (empty($this->configuration['from_format'])) {
86       throw new MigrateException('Format date plugin is missing from_format configuration.');
87     }
88     if (empty($this->configuration['to_format'])) {
89       throw new MigrateException('Format date plugin is missing to_format configuration.');
90     }
91
92     $fromFormat = $this->configuration['from_format'];
93     $toFormat = $this->configuration['to_format'];
94     $timezone = isset($this->configuration['timezone']) ? $this->configuration['timezone'] : NULL;
95     $settings = isset($this->configuration['settings']) ? $this->configuration['settings'] : [];
96
97     // Attempts to transform the supplied date using the defined input format.
98     // DateTimePlus::createFromFormat can throw exceptions, so we need to
99     // explicitly check for problems.
100     try {
101       $transformed = DateTimePlus::createFromFormat($fromFormat, $value, $timezone, $settings)->format($toFormat);
102     }
103     catch (\InvalidArgumentException $e) {
104       throw new MigrateException(sprintf('Format date plugin could not transform "%s" using the format "%s". Error: %s', $value, $fromFormat, $e->getMessage()), $e->getCode(), $e);
105     }
106     catch (\UnexpectedValueException $e) {
107       throw new MigrateException(sprintf('Format date plugin could not transform "%s" using the format "%s". Error: %s', $value, $fromFormat, $e->getMessage()), $e->getCode(), $e);
108     }
109
110     return $transformed;
111   }
112
113 }