PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / trunk
Kubio AI Page Builder vtrunk
2.9.0 2.8.6 2.8.5 2.8.4 2.8.3 2.8.2 2.8.1 trunk 1.0.0 1.0.1 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.9.0 2.0.0 2.1.1 2.1.2 2.1.3 2.2.0 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.5 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.6.2 2.6.3 2.6.5 2.6.6 2.6.7 2.7.0 2.7.1 2.7.2 2.7.3 2.8.0
kubio / vendor / sebastian / comparator / src / DateTimeComparator.php
kubio / vendor / sebastian / comparator / src Last commit date
exceptions 1 year ago ArrayComparator.php 1 year ago Comparator.php 1 year ago ComparisonFailure.php 1 year ago DOMNodeComparator.php 1 year ago DateTimeComparator.php 1 year ago DoubleComparator.php 1 year ago ExceptionComparator.php 1 year ago Factory.php 1 year ago MockObjectComparator.php 1 year ago NumericComparator.php 1 year ago ObjectComparator.php 1 year ago ResourceComparator.php 1 year ago ScalarComparator.php 1 year ago SplObjectStorageComparator.php 1 year ago TypeComparator.php 1 year ago
DateTimeComparator.php
96 lines
1 <?php declare(strict_types=1);
2 /*
3 * This file is part of sebastian/comparator.
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 namespace SebastianBergmann\Comparator;
11
12 use function abs;
13 use function floor;
14 use function sprintf;
15 use DateInterval;
16 use DateTime;
17 use DateTimeInterface;
18 use DateTimeZone;
19 use Exception;
20
21 /**
22 * Compares DateTimeInterface instances for equality.
23 */
24 class DateTimeComparator extends ObjectComparator
25 {
26 /**
27 * Returns whether the comparator can compare two values.
28 *
29 * @param mixed $expected The first value to compare
30 * @param mixed $actual The second value to compare
31 *
32 * @return bool
33 */
34 public function accepts($expected, $actual)
35 {
36 return ($expected instanceof DateTime || $expected instanceof DateTimeInterface) &&
37 ($actual instanceof DateTime || $actual instanceof DateTimeInterface);
38 }
39
40 /**
41 * Asserts that two values are equal.
42 *
43 * @param mixed $expected First value to compare
44 * @param mixed $actual Second value to compare
45 * @param float $delta Allowed numerical distance between two values to consider them equal
46 * @param bool $canonicalize Arrays are sorted before comparison when set to true
47 * @param bool $ignoreCase Case is ignored when set to true
48 * @param array $processed List of already processed elements (used to prevent infinite recursion)
49 *
50 * @throws Exception
51 * @throws ComparisonFailure
52 */
53 public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false, array &$processed = [])/*: void*/
54 {
55 /** @var DateTimeInterface $expected */
56 /** @var DateTimeInterface $actual */
57 $absDelta = abs($delta);
58 $delta = new DateInterval(sprintf('PT%dS', $absDelta));
59 $delta->f = $absDelta - floor($absDelta);
60
61 $actualClone = (clone $actual)
62 ->setTimezone(new DateTimeZone('UTC'));
63
64 $expectedLower = (clone $expected)
65 ->setTimezone(new DateTimeZone('UTC'))
66 ->sub($delta);
67
68 $expectedUpper = (clone $expected)
69 ->setTimezone(new DateTimeZone('UTC'))
70 ->add($delta);
71
72 if ($actualClone < $expectedLower || $actualClone > $expectedUpper) {
73 throw new ComparisonFailure(
74 $expected,
75 $actual,
76 $this->dateTimeToString($expected),
77 $this->dateTimeToString($actual),
78 false,
79 'Failed asserting that two DateTime objects are equal.'
80 );
81 }
82 }
83
84 /**
85 * Returns an ISO 8601 formatted string representation of a datetime or
86 * 'Invalid DateTimeInterface object' if the provided DateTimeInterface was not properly
87 * initialized.
88 */
89 private function dateTimeToString(DateTimeInterface $datetime): string
90 {
91 $string = $datetime->format('Y-m-d\TH:i:s.uO');
92
93 return $string ?: 'Invalid DateTimeInterface object';
94 }
95 }
96