Mercurial > hg > isophonics-drupal-site
comparison vendor/sebastian/comparator/src/DateTimeComparator.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 1fec387a4317 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 /* | |
3 * This file is part of the Comparator package. | |
4 * | |
5 * (c) Sebastian Bergmann <sebastian@phpunit.de> | |
6 * | |
7 * For the full copyright and license information, please view the LICENSE | |
8 * file that was distributed with this source code. | |
9 */ | |
10 | |
11 namespace SebastianBergmann\Comparator; | |
12 | |
13 /** | |
14 * Compares DateTimeInterface instances for equality. | |
15 */ | |
16 class DateTimeComparator extends ObjectComparator | |
17 { | |
18 /** | |
19 * Returns whether the comparator can compare two values. | |
20 * | |
21 * @param mixed $expected The first value to compare | |
22 * @param mixed $actual The second value to compare | |
23 * @return bool | |
24 */ | |
25 public function accepts($expected, $actual) | |
26 { | |
27 return ($expected instanceof \DateTime || $expected instanceof \DateTimeInterface) && | |
28 ($actual instanceof \DateTime || $actual instanceof \DateTimeInterface); | |
29 } | |
30 | |
31 /** | |
32 * Asserts that two values are equal. | |
33 * | |
34 * @param mixed $expected First value to compare | |
35 * @param mixed $actual Second value to compare | |
36 * @param float $delta Allowed numerical distance between two values to consider them equal | |
37 * @param bool $canonicalize Arrays are sorted before comparison when set to true | |
38 * @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 * | |
41 * @throws ComparisonFailure | |
42 */ | |
43 public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false, array &$processed = array()) | |
44 { | |
45 $delta = new \DateInterval(sprintf('PT%sS', abs($delta))); | |
46 | |
47 $expectedLower = clone $expected; | |
48 $expectedUpper = clone $expected; | |
49 | |
50 if ($actual < $expectedLower->sub($delta) || | |
51 $actual > $expectedUpper->add($delta)) { | |
52 throw new ComparisonFailure( | |
53 $expected, | |
54 $actual, | |
55 $this->dateTimeToString($expected), | |
56 $this->dateTimeToString($actual), | |
57 false, | |
58 'Failed asserting that two DateTime objects are equal.' | |
59 ); | |
60 } | |
61 } | |
62 | |
63 /** | |
64 * Returns an ISO 8601 formatted string representation of a datetime or | |
65 * 'Invalid DateTimeInterface object' if the provided DateTimeInterface was not properly | |
66 * initialized. | |
67 * | |
68 * @param \DateTimeInterface $datetime | |
69 * @return string | |
70 */ | |
71 private function dateTimeToString($datetime) | |
72 { | |
73 $string = $datetime->format('Y-m-d\TH:i:s.uO'); | |
74 | |
75 return $string ? $string : 'Invalid DateTimeInterface object'; | |
76 } | |
77 } |