• 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

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

3
namespace Nextras\Orm\Entity\PropertyWrapper;
4

5

6
use Nextras\Orm\Entity\IEntity;
7
use Nextras\Orm\Entity\IEntityAwareProperty;
8
use Nextras\Orm\Entity\IPropertyContainer;
9
use Nextras\Orm\Entity\PropertyComparator;
10
use Nextras\Orm\Entity\Reflection\EntityMetadata;
11
use Nextras\Orm\Entity\Reflection\PropertyMetadata;
12
use Nextras\Orm\Exception\InvalidArgumentException;
13
use Nextras\Orm\Model\MetadataStorage;
14

15

16
/**
17
 * @implements IEntityAwareProperty<IEntity>
18
 */
19
class PrimaryProxyWrapper implements IPropertyContainer, IEntityAwareProperty, PropertyComparator
20
{
21
        private IEntity $entity;
22
        private EntityMetadata $metadata;
23

24

25
        public function __construct(
5✔
26
                private readonly PropertyMetadata $propertyMetadata, // @phpstan-ignore constructor.unusedParameter
27
        )
28
        {
29
        }
5✔
30

31

32
        public function onEntityAttach(IEntity $entity): void
33
        {
34
                $this->entity = $entity;
5✔
35
                $this->metadata = $entity->getMetadata();
5✔
36
        }
5✔
37

38

39
        public function onEntityRepositoryAttach(IEntity $entity): void
40
        {
41
        }
5✔
42

43

44
        public function setInjectedValue($value): bool
45
        {
46
                $this->setRawValue($value);
5✔
47
                return true;
5✔
48
        }
49

50

51
        public function &getInjectedValue()
52
        {
53
                $value = $this->getRawValue();
5✔
54
                return $value;
5✔
55
        }
56

57

58
        public function hasInjectedValue(): bool
59
        {
60
                $value = $this->getRawValue();
5✔
61
                return isset($value);
5✔
62
        }
63

64

65
        public function convertToRawValue($value)
66
        {
67
                return $value;
5✔
68
        }
69

70

71
        public function setRawValue($value): void
72
        {
73
                if ($value === null) return;
5✔
74

75
                $keys = $this->metadata->getPrimaryKey();
5✔
76

77
                if (count($keys) === 1) {
5✔
78
                        $value = [$value];
5✔
79
                } elseif (!is_array($value)) {
5✔
80
                        $class = get_class($this->entity);
5✔
81
                        throw new InvalidArgumentException("Value for $class::\$id has to be passed as array.");
5✔
82
                }
83

84
                if (count($keys) !== count($value)) {
5✔
85
                        $class = get_class($this->entity);
5✔
86
                        throw new InvalidArgumentException("Value for $class::\$id has insufficient number of parameters.");
5✔
87
                }
88

89
                foreach ($keys as $key) {
5✔
90
                        $this->entity->setRawValue($key, array_shift($value));
5✔
91
                }
92
        }
5✔
93

94

95
        public function getRawValue()
96
        {
97
                if ($this->entity->isPersisted()) {
5✔
98
                        return $this->entity->getPersistedId();
5✔
99
                }
100

101
                $id = [];
5✔
102
                $keys = $this->metadata->getPrimaryKey();
5✔
103
                foreach ($keys as $key) {
5✔
104
                        $id[] = $this->entity->getRawValue($key);
5✔
105
                }
106
                if (count($keys) === 1) {
5✔
107
                        return $id[0];
5✔
108
                } else {
109
                        return $id;
5✔
110
                }
111
        }
112

113

114
        public function equals(mixed $a, mixed $b): bool
115
        {
116
                // equals() is called on a wrapper's prototype during filtering, and therefore
117
                // it is not connected (yet) to an entity instance;
118
                $metadata = MetadataStorage::get($this->propertyMetadata->containerClassname);
5✔
119

120
                foreach ($metadata->getPrimaryKey() as $key) {
5✔
121
                        $property = $metadata->getProperty($key);
5✔
122
                        $comparator = $property->getPropertyComparator();
5✔
123
                        if ($comparator !== null) {
5✔
124
                                if (!$comparator->equals($a, $b)) return false;
5✔
125
                        } else {
126
                                return $a === $b;
5✔
127
                        }
128
                }
129
                return true;
5✔
130
        }
131

132

133
        public function compare(mixed $a, mixed $b): int
134
        {
135
                // compare() is called on wrapper's prototype during filtering, and therefore
136
                // it is not connected (yet) to an entity instance;
NEW
137
                $metadata = MetadataStorage::get($this->propertyMetadata->containerClassname);
×
138

NEW
139
                $keys = $metadata->getPrimaryKey();
×
NEW
140
                if (count($keys) !== 1) {
×
NEW
141
                        throw new InvalidArgumentException("The compare() method may be called only for single property proxied primary key.");
×
142
                }
143

NEW
144
                $property = $metadata->getProperty($keys[0]);
×
NEW
145
                $comparator = $property->getPropertyComparator();
×
NEW
146
                if ($comparator !== null) {
×
NEW
147
                        return $comparator->compare($a, $b);
×
148
                } else {
NEW
149
                        return $a <=> $b;
×
150
                }
151
        }
152
}
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