Update core 8.3.0

This commit is contained in:
Rob Davies 2017-04-13 15:53:35 +01:00
parent da7a7918f8
commit cd7a898e66
6144 changed files with 132297 additions and 87747 deletions

View file

@ -41,14 +41,14 @@ class DateTimePlus {
/**
* An array of possible date parts.
*/
protected static $dateParts = array(
protected static $dateParts = [
'year',
'month',
'day',
'hour',
'minute',
'second',
);
];
/**
* The value of the time value passed to the constructor.
@ -88,7 +88,7 @@ class DateTimePlus {
/**
* An array of errors encountered when creating this date.
*/
protected $errors = array();
protected $errors = [];
/**
* The DateTime object.
@ -108,7 +108,7 @@ class DateTimePlus {
* @return static
* A new DateTimePlus object.
*/
public static function createFromDateTime(\DateTime $datetime, $settings = array()) {
public static function createFromDateTime(\DateTime $datetime, $settings = []) {
return new static($datetime->format(static::FORMAT), $datetime->getTimezone(), $settings);
}
@ -130,10 +130,10 @@ class DateTimePlus {
* @return static
* A new DateTimePlus object.
*
* @throws \Exception
* @throws \InvalidArgumentException
* If the array date values or value combination is not correct.
*/
public static function createFromArray(array $date_parts, $timezone = NULL, $settings = array()) {
public static function createFromArray(array $date_parts, $timezone = NULL, $settings = []) {
$date_parts = static::prepareArray($date_parts, TRUE);
if (static::checkArray($date_parts)) {
// Even with validation, we can end up with a value that the
@ -144,7 +144,7 @@ class DateTimePlus {
return new static($iso_date, $timezone, $settings);
}
else {
throw new \Exception('The array contains invalid values.');
throw new \InvalidArgumentException('The array contains invalid values.');
}
}
@ -164,12 +164,12 @@ class DateTimePlus {
* @return static
* A new DateTimePlus object.
*
* @throws \Exception
* @throws \InvalidArgumentException
* If the timestamp is not numeric.
*/
public static function createFromTimestamp($timestamp, $timezone = NULL, $settings = array()) {
public static function createFromTimestamp($timestamp, $timezone = NULL, $settings = []) {
if (!is_numeric($timestamp)) {
throw new \Exception('The timestamp must be numeric.');
throw new \InvalidArgumentException('The timestamp must be numeric.');
}
$datetime = new static('', $timezone, $settings);
$datetime->setTimestamp($timestamp);
@ -202,11 +202,12 @@ class DateTimePlus {
* @return static
* A new DateTimePlus object.
*
* @throws \Exception
* If the a date cannot be created from the given format, or if the
* created date does not match the input value.
* @throws \InvalidArgumentException
* If the a date cannot be created from the given format.
* @throws \UnexpectedValueException
* If the created date does not match the input value.
*/
public static function createFromFormat($format, $time, $timezone = NULL, $settings = array()) {
public static function createFromFormat($format, $time, $timezone = NULL, $settings = []) {
if (!isset($settings['validate_format'])) {
$settings['validate_format'] = TRUE;
}
@ -218,7 +219,7 @@ class DateTimePlus {
$date = \DateTime::createFromFormat($format, $time, $datetimeplus->getTimezone());
if (!$date instanceof \DateTime) {
throw new \Exception('The date cannot be created from a format.');
throw new \InvalidArgumentException('The date cannot be created from a format.');
}
else {
// Functions that parse date is forgiving, it might create a date that
@ -236,7 +237,7 @@ class DateTimePlus {
$datetimeplus->setTimezone($date->getTimezone());
if ($settings['validate_format'] && $test_time != $time) {
throw new \Exception('The created date does not match the input value.');
throw new \UnexpectedValueException('The created date does not match the input value.');
}
}
return $datetimeplus;
@ -257,7 +258,7 @@ class DateTimePlus {
* - debug: (optional) Boolean choice to leave debug values in the
* date object for debugging purposes. Defaults to FALSE.
*/
public function __construct($time = 'now', $timezone = NULL, $settings = array()) {
public function __construct($time = 'now', $timezone = NULL, $settings = []) {
// Unpack settings.
$this->langcode = !empty($settings['langcode']) ? $settings['langcode'] : NULL;
@ -267,10 +268,11 @@ class DateTimePlus {
$prepared_timezone = $this->prepareTimezone($timezone);
try {
$this->errors = [];
if (!empty($prepared_time)) {
$test = date_parse($prepared_time);
if (!empty($test['errors'])) {
$this->errors[] = $test['errors'];
$this->errors = $test['errors'];
}
}
@ -284,7 +286,6 @@ class DateTimePlus {
// Clean up the error messages.
$this->checkErrors();
$this->errors = array_unique($this->errors);
}
/**
@ -309,7 +310,7 @@ class DateTimePlus {
if (!method_exists($this->dateTimeObject, $method)) {
throw new \BadMethodCallException(sprintf('Call to undefined method %s::%s()', get_class($this), $method));
}
return call_user_func_array(array($this->dateTimeObject, $method), $args);
return call_user_func_array([$this->dateTimeObject, $method], $args);
}
/**
@ -345,7 +346,7 @@ class DateTimePlus {
if (!method_exists('\DateTime', $method)) {
throw new \BadMethodCallException(sprintf('Call to undefined method %s::%s()', get_called_class(), $method));
}
return call_user_func_array(array('\DateTime', $method), $args);
return call_user_func_array(['\DateTime', $method], $args);
}
/**
@ -441,7 +442,7 @@ class DateTimePlus {
public function checkErrors() {
$errors = \DateTime::getLastErrors();
if (!empty($errors['errors'])) {
$this->errors += $errors['errors'];
$this->errors = array_merge($this->errors, $errors['errors']);
}
// Most warnings are messages that the date could not be parsed
// which causes it to be altered. For validation purposes, a warning
@ -450,6 +451,8 @@ class DateTimePlus {
if (!empty($errors['warnings'])) {
$this->errors[] = 'The date is invalid.';
}
$this->errors = array_values(array_unique($this->errors));
}
/**
@ -528,24 +531,24 @@ class DateTimePlus {
public static function prepareArray($array, $force_valid_date = FALSE) {
if ($force_valid_date) {
$now = new \DateTime();
$array += array(
$array += [
'year' => $now->format('Y'),
'month' => 1,
'day' => 1,
'hour' => 0,
'minute' => 0,
'second' => 0,
);
];
}
else {
$array += array(
$array += [
'year' => '',
'month' => '',
'day' => '',
'hour' => '',
'minute' => '',
'second' => '',
);
];
}
return $array;
}
@ -576,7 +579,7 @@ class DateTimePlus {
}
// Testing for valid time is reversed. Missing time is OK,
// but incorrect values are not.
foreach (array('hour', 'minute', 'second') as $key) {
foreach (['hour', 'minute', 'second'] as $key) {
if (array_key_exists($key, $array)) {
$value = $array[$key];
switch ($key) {
@ -627,7 +630,7 @@ class DateTimePlus {
* @return string
* The formatted value of the date.
*/
public function format($format, $settings = array()) {
public function format($format, $settings = []) {
// If there were construction errors, we can't format the date.
if ($this->hasErrors()) {

View file

@ -0,0 +1,57 @@
<?php
namespace Drupal\Component\Datetime;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* Provides a class for obtaining system time.
*/
class Time implements TimeInterface {
/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* Constructs a Time object.
*
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack.
*/
public function __construct(RequestStack $request_stack) {
$this->requestStack = $request_stack;
}
/**
* {@inheritdoc}
*/
public function getRequestTime() {
return $this->requestStack->getCurrentRequest()->server->get('REQUEST_TIME');
}
/**
* {@inheritdoc}
*/
public function getRequestMicroTime() {
return $this->requestStack->getCurrentRequest()->server->get('REQUEST_TIME_FLOAT');
}
/**
* {@inheritdoc}
*/
public function getCurrentTime() {
return time();
}
/**
* {@inheritdoc}
*/
public function getCurrentMicroTime() {
return microtime(TRUE);
}
}

View file

@ -0,0 +1,149 @@
<?php
namespace Drupal\Component\Datetime;
/**
* Defines an interface for obtaining system time.
*/
interface TimeInterface {
/**
* Returns the timestamp for the current request.
*
* This method should be used to obtain the current system time at the start
* of the request. It will be the same value for the life of the request
* (even for long execution times).
*
* This method can replace instances of
* @code
* $request_time = $_SERVER['REQUEST_TIME'];
* $request_time = REQUEST_TIME;
* $request_time = $requestStack->getCurrentRequest()->server->get('REQUEST_TIME');
* $request_time = $request->server->get('REQUEST_TIME');
* @endcode
* and most instances of
* @code
* $time = time();
* @endcode
* with
* @code
* $request_time = \Drupal::time()->getRequestTime();
* @endcode
* or the equivalent using the injected service.
*
* Using the time service, rather than other methods, is especially important
* when creating tests, which require predictable timestamps.
*
* @return int
* A Unix timestamp.
*
* @see \Drupal\Component\Datetime\TimeInterface::getRequestMicroTime()
* @see \Drupal\Component\Datetime\TimeInterface::getCurrentTime()
* @see \Drupal\Component\Datetime\TimeInterface::getCurrentMicroTime()
*/
public function getRequestTime();
/**
* Returns the timestamp for the current request with microsecond precision.
*
* This method should be used to obtain the current system time, with
* microsecond precision, at the start of the request. It will be the same
* value for the life of the request (even for long execution times).
*
* This method can replace instances of
* @code
* $request_time_float = $_SERVER['REQUEST_TIME_FLOAT'];
* $request_time_float = $requestStack->getCurrentRequest()->server->get('REQUEST_TIME_FLOAT');
* $request_time_float = $request->server->get('REQUEST_TIME_FLOAT');
* @endcode
* and many instances of
* @code
* $microtime = microtime();
* $microtime = microtime(TRUE);
* @endcode
* with
* @code
* $request_time = \Drupal::time()->getRequestMicroTime();
* @endcode
* or the equivalent using the injected service.
*
* Using the time service, rather than other methods, is especially important
* when creating tests, which require predictable timestamps.
*
* @return float
* A Unix timestamp with a fractional portion.
*
* @see \Drupal\Component\Datetime\TimeInterface::getRequestTime()
* @see \Drupal\Component\Datetime\TimeInterface::getCurrentTime()
* @see \Drupal\Component\Datetime\TimeInterface::getCurrentMicroTime()
*/
public function getRequestMicroTime();
/**
* Returns the current system time as an integer.
*
* This method should be used to obtain the current system time, at the time
* the method was called.
*
* This method can replace many instances of
* @code
* $time = time();
* @endcode
* with
* @code
* $request_time = \Drupal::time()->getCurrentTime();
* @endcode
* or the equivalent using the injected service.
*
* This method should only be used when the current system time is actually
* needed, such as with timers or time interval calculations. If only the
* time at the start of the request is needed,
* use TimeInterface::getRequestTime().
*
* Using the time service, rather than other methods, is especially important
* when creating tests, which require predictable timestamps.
*
* @return int
* A Unix timestamp.
*
* @see \Drupal\Component\Datetime\TimeInterface::getRequestTime()
* @see \Drupal\Component\Datetime\TimeInterface::getRequestMicroTime()
* @see \Drupal\Component\Datetime\TimeInterface::getCurrentMicroTime()
*/
public function getCurrentTime();
/**
* Returns the current system time with microsecond precision.
*
* This method should be used to obtain the current system time, with
* microsecond precision, at the time the method was called.
*
* This method can replace many instances of
* @code
* $microtime = microtime();
* $microtime = microtime(TRUE);
* @endcode
* with
* @code
* $request_time = \Drupal::time()->getCurrentMicroTime();
* @endcode
* or the equivalent using the injected service.
*
* This method should only be used when the current system time is actually
* needed, such as with timers or time interval calculations. If only the
* time at the start of the request and microsecond precision is needed,
* use TimeInterface::getRequestMicroTime().
*
* Using the time service, rather than other methods, is especially important
* when creating tests, which require predictable timestamps.
*
* @return float
* A Unix timestamp with a fractional portion.
*
* @see \Drupal\Component\Datetime\TimeInterface::getRequestTime()
* @see \Drupal\Component\Datetime\TimeInterface::getRequestMicroTime()
* @see \Drupal\Component\Datetime\TimeInterface::getCurrentTime()
*/
public function getCurrentMicroTime();
}