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

phpolar / model / 6328972008

27 Sep 2023 04:47PM UTC coverage: 91.815%. First build
6328972008

Pull #42

github

web-flow
Merge ea05db7cb into c9b7f1c25
Pull Request #42: build(deps-dev): bump phpmd/phpmd from 2.13.0 to 2.14.0

258 of 281 relevant lines covered (91.81%)

7.74 hits per line

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

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

3
declare(strict_types=1);
4

5
namespace Phpolar\Model;
6

7
use DateTimeInterface;
8
use ReflectionNamedType;
9
use ReflectionProperty;
10
use ReflectionUnionType;
11

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