comparison vendor/sebastian/comparator/src/DateTimeComparator.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
1 <?php 1 <?php
2 /* 2 /*
3 * This file is part of the Comparator package. 3 * This file is part of sebastian/comparator.
4 * 4 *
5 * (c) Sebastian Bergmann <sebastian@phpunit.de> 5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6 * 6 *
7 * For the full copyright and license information, please view the LICENSE 7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code. 8 * file that was distributed with this source code.
16 class DateTimeComparator extends ObjectComparator 16 class DateTimeComparator extends ObjectComparator
17 { 17 {
18 /** 18 /**
19 * Returns whether the comparator can compare two values. 19 * Returns whether the comparator can compare two values.
20 * 20 *
21 * @param mixed $expected The first value to compare 21 * @param mixed $expected The first value to compare
22 * @param mixed $actual The second value to compare 22 * @param mixed $actual The second value to compare
23 *
23 * @return bool 24 * @return bool
24 */ 25 */
25 public function accepts($expected, $actual) 26 public function accepts($expected, $actual)
26 { 27 {
27 return ($expected instanceof \DateTime || $expected instanceof \DateTimeInterface) && 28 return ($expected instanceof \DateTime || $expected instanceof \DateTimeInterface) &&
38 * @param bool $ignoreCase Case is ignored when set to true 39 * @param bool $ignoreCase Case is ignored when set to true
39 * @param array $processed List of already processed elements (used to prevent infinite recursion) 40 * @param array $processed List of already processed elements (used to prevent infinite recursion)
40 * 41 *
41 * @throws ComparisonFailure 42 * @throws ComparisonFailure
42 */ 43 */
43 public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false, array &$processed = array()) 44 public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false, array &$processed = [])
44 { 45 {
45 $delta = new \DateInterval(sprintf('PT%sS', abs($delta))); 46 /** @var \DateTimeInterface $expected */
47 /** @var \DateTimeInterface $actual */
48 $delta = new \DateInterval(\sprintf('PT%dS', \abs($delta)));
46 49
47 $expectedLower = clone $expected; 50 $actualClone = (clone $actual)
48 $expectedUpper = clone $expected; 51 ->setTimezone(new \DateTimeZone('UTC'));
49 52
50 if ($actual < $expectedLower->sub($delta) || 53 $expectedLower = (clone $expected)
51 $actual > $expectedUpper->add($delta)) { 54 ->setTimezone(new \DateTimeZone('UTC'))
55 ->sub($delta);
56
57 $expectedUpper = (clone $expected)
58 ->setTimezone(new \DateTimeZone('UTC'))
59 ->add($delta);
60
61 if ($actualClone < $expectedLower || $actualClone > $expectedUpper) {
52 throw new ComparisonFailure( 62 throw new ComparisonFailure(
53 $expected, 63 $expected,
54 $actual, 64 $actual,
55 $this->dateTimeToString($expected), 65 $this->dateTimeToString($expected),
56 $this->dateTimeToString($actual), 66 $this->dateTimeToString($actual),
62 72
63 /** 73 /**
64 * Returns an ISO 8601 formatted string representation of a datetime or 74 * Returns an ISO 8601 formatted string representation of a datetime or
65 * 'Invalid DateTimeInterface object' if the provided DateTimeInterface was not properly 75 * 'Invalid DateTimeInterface object' if the provided DateTimeInterface was not properly
66 * initialized. 76 * initialized.
67 *
68 * @param \DateTimeInterface $datetime
69 * @return string
70 */ 77 */
71 private function dateTimeToString($datetime) 78 private function dateTimeToString(\DateTimeInterface $datetime): string
72 { 79 {
73 $string = $datetime->format('Y-m-d\TH:i:s.uO'); 80 $string = $datetime->format('Y-m-d\TH:i:s.uO');
74 81
75 return $string ? $string : 'Invalid DateTimeInterface object'; 82 return $string ?: 'Invalid DateTimeInterface object';
76 } 83 }
77 } 84 }