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

nextras / orm / 13354993605

16 Feb 2025 12:29PM UTC coverage: 91.73% (-0.3%) from 92.021%
13354993605

push

github

web-flow
Merge pull request #732 from nextras/datetime-property-wrapper

Datetime property wrapper

94 of 112 new or added lines in 15 files covered. (83.93%)

1 existing line in 1 file now uncovered.

4115 of 4486 relevant lines covered (91.73%)

4.57 hits per line

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

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

24
                if ($value instanceof $rawType) {
5✔
25
                        return $value;
5✔
26

27
                } elseif ($value instanceof \DateTimeInterface) {
5✔
28
                        return new $rawType($value->format('c'));
5✔
29

30
                } elseif ($value === null) {
5✔
31
                        return null;
5✔
32

33
                } elseif (is_string($value)) {
5✔
34
                        if ($value === '') throw new InvalidPropertyValueException($this->propertyMetadata);
5✔
35

36
                        $tmp = new $rawType($value);
5✔
37
                        return $tmp->setTimezone(new \DateTimeZone(date_default_timezone_get()));
5✔
38

NEW
39
                } elseif (ctype_digit((string) $value)) {
×
NEW
40
                        return new $rawType("@{$value}");
×
41

42
                } else {
NEW
43
                        throw new InvalidPropertyValueException($this->propertyMetadata);
×
44
                }
45
        }
46

47

48
        public function convertFromRawValue($value)
49
        {
50
                if ($value === null && !$this->propertyMetadata->isNullable) {
5✔
NEW
51
                        throw new NullValueException($this->propertyMetadata);
×
52
                }
53

54
                // The string conversion from raw values is used when using {default} modifier in property definition.
55
                // This string value is considered to be a raw value.
56
                if (is_string($value)) {
5✔
57
                        if ($value === '') throw new InvalidPropertyValueException($this->propertyMetadata);
5✔
58

59
                        /** @var class-string<covariant DateTimeImmutable> $rawType */
60
                        $rawType = array_key_first($this->propertyMetadata->types);
5✔
61
                        $tmp = new $rawType($value);
5✔
62
                        return $tmp->setTimezone(new \DateTimeZone(date_default_timezone_get()));
5✔
63
                }
64

65
                return $value;
5✔
66
        }
67

68

69
        public function setInjectedValue($value): bool
70
        {
71
                if ($value === null && !$this->propertyMetadata->isNullable) {
5✔
NEW
72
                        throw new NullValueException($this->propertyMetadata);
×
73
                }
74
                return parent::setInjectedValue($this->convertToRawValue($value));
5✔
75
        }
76

77

78
        public function equals(mixed $a, mixed $b): bool
79
        {
80
                assert($a === null || $a instanceof \DateTimeImmutable);
81
                assert($b === null || $b instanceof \DateTimeImmutable);
82
                return $a?->getTimestamp() === $b?->getTimestamp();
5✔
83
        }
84

85

86
        public function compare(mixed $a, mixed $b): int
87
        {
88
                assert($a === null || $a instanceof \DateTimeImmutable);
89
                assert($b === null || $b instanceof \DateTimeImmutable);
90
                return $a?->getTimestamp() <=> $b?->getTimestamp();
5✔
91
        }
92
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc