• 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

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

3
declare(strict_types=1);
4

5
namespace Phpolar\Model;
6

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