• 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

97.73
/src/FieldErrorMessageTrait.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Phpolar\Model;
6

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

12
/**
13
 * Provides support for displaying form field error messages.
14
 */
15
trait FieldErrorMessageTrait
16
{
17
    /**
18
     * Stores error messages.
19
     *
20
     * @var array<string,string>
21
     */
22
    private array $errorMessages;
23

24
    /**
25
     * Make sure the properties are
26
     * checked only once.
27
     *
28
     * Do not initialize this.
29
     * Otherwise, this property
30
     * will appear when iterating
31
     * the objects that use this
32
     * trait.
33
     */
34
    protected bool $checked;
35

36
    /**
37
     * Validation is usually only desired
38
     * when the user attempts to
39
     * create or edit a model.
40
     */
41
    protected bool $shouldValidate = false;
42

43
    /**
44
     * Provides an interface for
45
     * retrieving a fields error message.
46
     */
47
    public function getFieldErrorMessage(string $fieldName, string $stringToAppend = ""): string
48
    {
49
        $this->setErrorMsgsOnce();
6✔
50
        return $this->hasError($fieldName) === true ? ($this->errorMessages[$fieldName] . $stringToAppend) : "";
6✔
51
    }
52

53
    /**
54
     * Determines if a property is not valid.
55
     */
56
    public function hasError(string $fieldName): bool
57
    {
58
        if ($this->shouldValidate === false) {
12✔
59
            return false;
4✔
60
        }
61
        $this->setErrorMsgsOnce();
8✔
62
        return isset($this->errorMessages[$fieldName]);
8✔
63
    }
64

65
    /**
66
     * Changes the posted state of the model.
67
     */
68
    public function isPosted(): void
69
    {
70
        $this->shouldValidate = true;
8✔
71
    }
72

73
    /**
74
     * Selects one of the provided validation
75
     * strings based on the state of the model
76
     *
77
     * @param string $invalidAttr The HTML attribute that denotes invalid state
78
     * @param string $validAttr The HTML attribute that denotes valid state
79
     *
80
     * @return string The selected HTML attribute that corresponds with the state of the model
81
     */
82
    public function selectValAttr(string $propName, string $invalidAttr, string $validAttr): string
83
    {
84
        return $this->shouldValidate === false ? "" : ($this->hasError($propName) === true ? $invalidAttr : $validAttr);
2✔
85
    }
86

87

88
    private function setErrorMsgsOnce(): void
89
    {
90
        if (
91
            (new ReflectionProperty($this, "checked"))->isInitialized($this) === true &&
10✔
92
            $this->checked === true
10✔
93
        ) {
94
            return;
4✔
95
        }
96
        $this->checked = true;
10✔
97
        $props = (new ReflectionObject($this))->getProperties(ReflectionProperty::IS_PUBLIC);
10✔
98
        array_walk(
10✔
99
            $props,
10✔
100
            function (ReflectionProperty $prop): void {
10✔
101
                $errorMessages = self::getErrorsFromAttributes(
10✔
102
                    $this->getMessageGetters($prop),
10✔
103
                );
10✔
104
                array_walk(
10✔
105
                    $errorMessages,
10✔
106
                    function (string $err) use ($prop): void {
10✔
107
                        $this->errorMessages[$prop->getName()] = (string) $err;
6✔
108
                    }
10✔
109
                );
10✔
110
            }
10✔
111
        );
10✔
112
    }
113

114
    /**
115
     * Provides a way of retrieving only the validator attributes of a property.
116
     *
117
     * Returns only validation attributes.
118
     *
119
     * @return MessageGetterInterface[]
120
     */
121
    private function getMessageGetters(ReflectionProperty $prop): array
122
    {
123
        return array_map(
10✔
124
            function (MessageGetterInterface $instance) use ($prop): MessageGetterInterface {
10✔
125
                if (property_exists($instance, "propVal") === true) {
10✔
126
                    $instance->propVal = $prop->isInitialized($this) === true ? $prop->getValue($this) : $prop->getDefaultValue();
×
127
                }
128
                return $instance;
10✔
129
            },
10✔
130
            array_map(
10✔
131
                static fn (ReflectionAttribute $attr) => $attr->newInstance(),
10✔
132
                $prop->getAttributes(MessageGetterInterface::class, ReflectionAttribute::IS_INSTANCEOF),
10✔
133
            ),
10✔
134
        );
10✔
135
    }
136

137
    /**
138
     * Provides a way of retrieving errors from the invalid validation attributes of a property.
139
     *
140
     * Returns errors from invalid validators.
141
     *
142
     * @param MessageGetterInterface[] $attrs
143
     * @return string[]
144
     */
145
    private static function getErrorsFromAttributes(array $attrs): array
146
    {
147
        return array_filter(
10✔
148
            array_map(
10✔
149
                static fn (MessageGetterInterface $attr) => (string) $attr->getMessage(),
10✔
150
                $attrs
10✔
151
            )
10✔
152
        );
10✔
153
    }
154
}
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