• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nextras / orm / 41909495460

04 Nov 2026 08:44PM UTC coverage: 92.015% (-0.002%) from 92.017%
41909495460

push

github

web-flow
Merge pull request #842 from nextras/js/dresscode

Setup DressCode

288 of 347 new or added lines in 66 files covered. (83.0%)

14 existing lines in 9 files now uncovered.

4402 of 4784 relevant lines covered (92.02%)

6.29 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.0
/src/Entity/PropertyWrapper/DateTimeWrapper.php
1
<?php declare(strict_types = 1);
2

3
namespace Nextras\Orm\Entity\PropertyWrapper;
4

5

6
use Nextras\Dbal\Utils\DateTimeImmutable;
7
use Nextras\Orm\Entity\ImmutableValuePropertyWrapper;
8
use Nextras\Orm\Entity\PropertyComparator;
9
use Nextras\Orm\Exception\InvalidPropertyValueException;
10
use Nextras\Orm\Exception\NullValueException;
11

12

13
/**
14
 * \DateTimeImmutable property wrapper.
15
 *
16
 * Handles auto-conversion from string, int (unix timestamp) and \DateTimeInterface
17
 * instances to a \DateTimeImmutable (sub-)type.
18
 */
19
class DateTimeWrapper extends ImmutableValuePropertyWrapper implements PropertyComparator
20
{
21
        public function convertToRawValue(mixed $value): mixed
22
        {
23
                /** @var class-string<covariant DateTimeImmutable> $rawType */
24
                $rawType = array_key_first($this->propertyMetadata->types);
7 ✔
25

26
                if ($value instanceof $rawType) {
7 ✔
27
                        return $value;
7 ✔
28
                } elseif ($value instanceof \DateTimeInterface) {
7 ✔
29
                        return new $rawType($value->format('c'));
7 ✔
30
                } elseif ($value === null) {
7 ✔
31
                        return null;
7 ✔
32
                } elseif (is_string($value)) {
7 ✔
33
                        if ($value === '') throw new InvalidPropertyValueException($this->propertyMetadata);
7 ✔
34

35
                        $tmp = new $rawType($value);
7 ✔
36
                        return $tmp->setTimezone(new \DateTimeZone(date_default_timezone_get()));
7 ✔
UNCOV
37
                } elseif (ctype_digit((string) $value)) {
×
38
                        return new $rawType("@{$value}");
×
39
                } else {
UNCOV
40
                        throw new InvalidPropertyValueException($this->propertyMetadata);
×
41
                }
42
        }
43

44

45
        public function convertFromRawValue($value)
46
        {
47
                // The string conversion from raw values is used when using {default} modifier in property definition.
48
                // This string value is considered to be a raw value.
49
                if (is_string($value)) {
7 ✔
50
                        if ($value === '') throw new InvalidPropertyValueException($this->propertyMetadata);
7 ✔
51

52
                        /** @var class-string<covariant DateTimeImmutable> $rawType */
53
                        $rawType = array_key_first($this->propertyMetadata->types);
7 ✔
54
                        $tmp = new $rawType($value);
7 ✔
55
                        return $tmp->setTimezone(new \DateTimeZone(date_default_timezone_get()));
7 ✔
56
                }
57

58
                return $value;
7 ✔
59
        }
60

61

62
        public function setInjectedValue($value): bool
63
        {
64
                if ($value === null && !$this->propertyMetadata->isNullable) {
7 ✔
65
                        throw new NullValueException($this->propertyMetadata);
×
66
                }
67
                return parent::setInjectedValue($this->convertToRawValue($value));
7 ✔
68
        }
69

70

71
        public function equals(mixed $a, mixed $b): bool
72
        {
73
                assert($a === null || $a instanceof \DateTimeImmutable);
74
                assert($b === null || $b instanceof \DateTimeImmutable);
75
                return $a?->getTimestamp() === $b?->getTimestamp();
7 ✔
76
        }
77

78

79
        public function compare(mixed $a, mixed $b): int
80
        {
81
                assert($a === null || $a instanceof \DateTimeImmutable);
82
                assert($b === null || $b instanceof \DateTimeImmutable);
83
                return $a?->getTimestamp() <=> $b?->getTimestamp();
7 ✔
84
        }
85
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc