• 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

95.16
/src/Entity/Reflection/PropertyMetadata.php
1
<?php declare(strict_types = 1);
2

3
namespace Nextras\Orm\Entity\Reflection;
4

5

6
use Nette\SmartObject;
7
use Nextras\Orm\Entity\IProperty;
8
use Nextras\Orm\Entity\PropertyComparator;
9
use Nextras\Orm\Exception\InvalidStateException;
10
use function is_subclass_of;
11

12

13
class PropertyMetadata
14
{
15
        use SmartObject;
16

17
        public string $name = '';
18
        public string $containerClassname = '';
19
        public string|null $wrapper = null;
20

21
        public string|null $hasGetter = null;
22
        public string|null $hasSetter = null;
23

24
        /** @var non-empty-array<string, bool> of allowed types defined as keys */
25
        public array $types;
26

27
        public bool $isPrimary = false;
28
        public bool $isNullable = false;
29
        public bool $isReadonly = false;
30
        public bool $isVirtual = false;
31
        public mixed $defaultValue = null;
32
        public PropertyRelationshipMetadata|null $relationship = null;
33

34
        /** @var array<string, mixed>|null */
35
        public array|null $args = null;
36

37
        /** @var array<mixed>|null array of allowed values */
38
        public array|null $enum = null;
39

40
        private IProperty|null $wrapperPrototype = null;
41

42

43
        public function __construct()
44
        {
45
        }
7 ✔
46

47

48
        public function getWrapperPrototype(): IProperty
49
        {
50
                if ($this->wrapperPrototype === null) {
7 ✔
51
                        $class = $this->wrapper;
7 ✔
52
                        if ($class === null || !is_subclass_of($class, IProperty::class)) {
7 ✔
53
                                throw new InvalidStateException('Wrapper class has to implement ' . IProperty::class . ' interface.');
×
54
                        }
55
                        $this->wrapperPrototype = new $class($this);
7 ✔
56
                }
57
                return $this->wrapperPrototype;
7 ✔
58
        }
59

60

61
        public function getPropertyComparator(): PropertyComparator|null
62
        {
63
                if ($this->wrapper === null) return null;
7 ✔
64
                $wrapper = $this->getWrapperPrototype();
7 ✔
65
                if (!$wrapper instanceof PropertyComparator) return null;
7 ✔
66
                return $wrapper;
7 ✔
67
        }
68

69

70
        public function __sleep()
71
        {
72
                // we skip wrapperPrototype which may not be serializable and is created lazily
73
                return [
74
                        'name',
7 ✔
75
                        'containerClassname',
76
                        'wrapper',
77
                        'hasGetter',
78
                        'hasSetter',
79
                        'types',
80
                        'isPrimary',
81
                        'isNullable',
82
                        'isReadonly',
83
                        'isVirtual',
84
                        'defaultValue',
85
                        'relationship',
86
                        'args',
87
                        'enum',
88
                ];
89
        }
90

91

92
        /**
93
         * @param mixed $value
94
         */
95
        public function isValid(&$value): bool
96
        {
97
                if ($value === null && $this->isNullable) {
7 ✔
98
                        return true;
7 ✔
99
                }
100

101
                if ($this->enum !== null) {
7 ✔
102
                        return in_array($value, $this->enum, true);
7 ✔
103
                }
104

105
                foreach ($this->types as $rawType => $_) {
7 ✔
106
                        $type = strtolower($rawType);
7 ✔
107
                        if ($type === 'string') {
7 ✔
108
                                if (is_string($value)) {
7 ✔
109
                                        return true;
7 ✔
110
                                }
111
                                if (is_int($value) || (is_object($value) && method_exists($value, '__toString'))) {
7 ✔
112
                                        $value = (string) $value;
7 ✔
113
                                        return true;
7 ✔
114
                                }
115
                        } elseif ($type === 'float') {
7 ✔
116
                                if (is_float($value)) {
7 ✔
117
                                        return true;
7 ✔
118
                                }
119
                                if (is_numeric($value)) {
7 ✔
120
                                        $value = (float) $value;
7 ✔
121
                                        return true;
7 ✔
122
                                } elseif (is_string($value)) {
7 ✔
123
                                        $value = (float) str_replace([' ', ','], ['', '.'], $value);
7 ✔
124
                                        return true;
7 ✔
125
                                }
126
                        } elseif ($type === 'int') {
7 ✔
127
                                if (is_int($value)) {
7 ✔
128
                                        return true;
7 ✔
129
                                }
130
                                if (is_numeric($value)) {
7 ✔
131
                                        $value = (int) $value;
7 ✔
132
                                        return true;
7 ✔
133
                                } elseif (is_string($value)) {
7 ✔
134
                                        $value = (int) str_replace([' ', ','], ['', '.'], $value);
7 ✔
135
                                        return true;
7 ✔
136
                                }
137
                        } elseif ($type === 'bool') {
7 ✔
138
                                if (is_bool($value)) {
7 ✔
139
                                        return true;
7 ✔
140
                                }
141
                                if (in_array($value, [0, 0.0, '0', 1, 1.0, '1'], true)) {
7 ✔
142
                                        $value = (bool) $value;
7 ✔
143
                                        return true;
7 ✔
144
                                }
145
                        } elseif ($type === 'array') {
7 ✔
146
                                if (is_array($value)) {
7 ✔
147
                                        return true;
7 ✔
148
                                }
149
                        } elseif ($type === 'object') {
7 ✔
150
                                if (is_object($value)) {
7 ✔
151
                                        return true;
7 ✔
152
                                }
153
                        } elseif ($type === 'scalar') {
7 ✔
UNCOV
154
                                if (is_scalar($value)) {
×
155
                                        return true;
×
156
                                }
157
                        } elseif ($type === 'mixed') {
7 ✔
158
                                return true;
7 ✔
159
                        } else {
160
                                if ($value instanceof $type) {
7 ✔
161
                                        return true;
7 ✔
162
                                }
163
                        }
164
                }
165

166
                return false;
7 ✔
167
        }
168
}
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