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

phpolar / model / 20685976260

04 Jan 2026 01:54AM UTC coverage: 89.667% (-1.8%) from 91.497%
20685976260

push

github

web-flow
Merge pull request #180 from phpolar:check-default-value

Check-default-value

0 of 8 new or added lines in 2 files covered. (0.0%)

269 of 300 relevant lines covered (89.67%)

3.63 hits per line

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

0.0
/src/ValidationTrait.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Phpolar\Model;
6

7
use PhpContrib\Validator\ValidatorInterface;
8
use ReflectionAttribute;
9
use ReflectionObject;
10
use ReflectionProperty;
11

12
/**
13
 * Use to add support for validating the properties of an object.
14
 */
15
trait ValidationTrait
16
{
17
    /**
18
     * Determines if the configured properties of an object
19
     * are valid.
20
     *
21
     * The configuration is used to determine validity.
22
     *
23
     * @api
24
     */
25
    public function isValid(): bool
26
    {
27
        return array_reduce(
×
28
            (new ReflectionObject($this))->getProperties(ReflectionProperty::IS_PUBLIC),
×
29
            $this->validateProperty(...),
×
30
            true
×
31
        );
×
32
    }
33

34
    /**
35
     * Uses validation attributes to determine if the
36
     * property is valid.
37
     */
38
    private function validateProperty(bool $prev, ReflectionProperty $prop): bool
39
    {
40
        return $prev && array_reduce(
×
41
            $this->getValidators($prop),
×
42
            static fn(bool $previousResult, ValidatorInterface $currentAttribute) =>
×
43
            $previousResult && $currentAttribute->isValid(),
×
44
            true
×
45
        );
×
46
    }
47

48
    /**
49
     * Provides a way of retrieving only the validator attributes of a property.
50
     *
51
     * Returns only validation attributes.
52
     *
53
     * @return ValidatorInterface[]
54
     */
55
    private function getValidators(ReflectionProperty $prop): array
56
    {
57
        return array_map(
×
58
            function (ValidatorInterface $instance) use ($prop): ValidatorInterface {
×
59
                if (property_exists($instance, "propVal") === true) {
×
NEW
60
                    if ($prop->isInitialized($this) === true) {
×
NEW
61
                        $instance->propVal = $prop->getValue($this);
×
62
                    }
NEW
63
                    if ($prop->hasDefaultValue() === true) {
×
NEW
64
                        $instance->propVal = $prop->getDefaultValue();
×
65
                    }
66
                }
67
                return $instance;
×
68
            },
×
69
            array_map(
×
70
                static fn(ReflectionAttribute $attr) => $attr->newInstance(),
×
71
                $prop->getAttributes(ValidatorInterface::class, ReflectionAttribute::IS_INSTANCEOF),
×
72
            ),
×
73
        );
×
74
    }
75
}
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

© 2026 Coveralls, Inc