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

phpolar / model / 5449748827

pending completion
5449748827

Pull #12

github

web-flow
Merge 068a00ea6 into 190d9e203
Pull Request #12: refactor: use renamed core library

263 of 287 relevant lines covered (91.64%)

7.79 hits per line

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

100.0
/src/FormControlTypeDetectionTrait.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Phpolar\Model;
6

7
use DateTime;
8
use DateTimeImmutable;
9
use DateTimeInterface;
10
use ReflectionNamedType;
11
use ReflectionProperty;
12
use ReflectionUnionType;
13

14
/**
15
 * Adds support for detecting the form control type based on the type declaration of the property.
16
 *
17
 * If the property type is not declared, the type of the value of the property will be used.
18
 */
19
trait FormControlTypeDetectionTrait
20
{
21
    /**
22
     * Uses the type declaration of the property to determine the form control type of the field.
23
     *
24
     * @api
25
     */
26
    public function getFormControlType(string $propName): FormControlTypes
27
    {
28
        $property = new ReflectionProperty($this, $propName);
2✔
29
        $propertyType = $property->getType();
2✔
30
        $propTypeNotDeclared = $propertyType === null;
2✔
31
        if ($propTypeNotDeclared === true) {
2✔
32
            return $property->isInitialized($this) === true ? match (gettype($property->getValue($this))) {
2✔
33
                "string",
2✔
34
                "integer",
2✔
35
                "double",
2✔
36
                "boolean" => FormControlTypes::Input,
2✔
37
                "object" => match (get_class($property->getValue($this))) {
2✔
38
                    "DateTimeInterface", "DateTimeImmutable", "DateTime" => FormControlTypes::Input,
2✔
39
                    default => FormControlTypes::Invalid,
2✔
40
                },
2✔
41
                "array" => FormControlTypes::Select,
2✔
42
                default => FormControlTypes::Invalid,
2✔
43
            } : FormControlTypes::Invalid;
2✔
44
        }
45
        if ($propertyType instanceof ReflectionNamedType) {
2✔
46
            return match ($property->getName()) {
2✔
47
                "string",
2✔
48
                "int",
2✔
49
                "float",
2✔
50
                "bool",
2✔
51
                DateTimeInterface::class,
2✔
52
                DateTimeImmutable::class,
2✔
53
                DateTime::class =>
2✔
54
                FormControlTypes::Input,
2✔
55
                "array" => FormControlTypes::Select,
2✔
56
                default => FormControlTypes::Invalid,
2✔
57
            };
2✔
58
        }
59
        if ($propertyType instanceof ReflectionUnionType) {
2✔
60
            return in_array("string", $propertyType->getTypes()) === true &&
2✔
61
            in_array("array", $propertyType->getTypes()) === false ? FormControlTypes::Input : FormControlTypes::Invalid;
2✔
62
        }
63
        return FormControlTypes::Invalid;
2✔
64
    }
65
}
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