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

phpolar / model / 14025007690

24 Mar 2025 01:06AM UTC coverage: 90.182%. First build
14025007690

Pull #97

github

web-flow
Merge 0b573916d into 84527ee9f
Pull Request #97: test(InputTypesTest): remove covered exception

248 of 275 relevant lines covered (90.18%)

3.32 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);
1✔
27
        if (count($property->getAttributes(Hidden::class)) > 0) {
1✔
28
            return InputTypes::Hidden;
1✔
29
        }
30
        $propertyType = $property->getType();
1✔
31
        $propTypeNotDeclared = $propertyType === null;
1✔
32
        if ($propTypeNotDeclared === true) {
1✔
33
            return $property->isInitialized($this) === true ? match (gettype($property->getValue($this))) {
1✔
34
                "string" => InputTypes::Text,
1✔
35
                "integer", "double" => InputTypes::Number,
1✔
36
                "boolean" => InputTypes::Checkbox,
1✔
37
                "object" => $property->getValue($this) instanceof DateTimeInterface ? InputTypes::Date : InputTypes::Invalid,
1✔
38
                default => InputTypes::Invalid,
1✔
39
            } : InputTypes::Invalid;
1✔
40
        }
41
        if ($propertyType instanceof ReflectionNamedType) {
1✔
42
            return match ($propertyType->getName()) {
1✔
43
                "string" => InputTypes::Text,
1✔
44
                "int", "float" => InputTypes::Number,
1✔
45
                "bool" => InputTypes::Checkbox,
1✔
46
                "DateTimeInterface", "DateTimeImmutable", "DateTime" =>
1✔
47
                InputTypes::Date,
1✔
48
                default => InputTypes::Invalid,
1✔
49
            };
1✔
50
        }
51
        if ($propertyType instanceof ReflectionUnionType) {
1✔
52
            return in_array("string", $propertyType->getTypes()) === true &&
1✔
53
                in_array("array", $propertyType->getTypes()) === false ? InputTypes::Text : InputTypes::Invalid;
1✔
54
        }
55
        return InputTypes::Invalid;
1✔
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