Skip to content

Commit 60bbb2f

Browse files
authored
Add Result::getPropertyErrorMessagesByPath() method (#774)
1 parent 2c97207 commit 60bbb2f

File tree

7 files changed

+93
-3
lines changed

7 files changed

+93
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 2.4.1 under development
44

5-
- no changes in this release.
5+
- New #774: Add `Result::getPropertyErrorMessagesByPath()` method (@vjik)
66

77
## 2.4.0 July 07, 2025
88

docs/guide/en/result.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ The output for example above:
8686
];
8787
```
8888

89+
#### Filtering by a specific path
90+
91+
This list of error messages can be filtered by a specific path to property.
92+
93+
```php
94+
$result->getPropertyErrorMessagesByPath(['person', 'first_name']);
95+
```
96+
8997
### Error messages indexed by property
9098

9199
To group error messages by property, use the following `Result` API call:

docs/guide/ru/result.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ $result->getPropertyErrorMessages('email');
9595
];
9696
```
9797

98+
#### Filtering by a specific path
99+
100+
This list of error messages can be filtered by a specific path to property.
101+
102+
```php
103+
$result->getPropertyErrorMessagesByPath(['person', 'first_name']);
104+
```
105+
98106
### Сообщения об ошибках, сгруппированные по свойству
99107

100108
Для группировки сообщений об ошибках по свойству, используйте следующий

docs/po/result.md/result.md.pot

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
msgid ""
88
msgstr ""
99
"Project-Id-Version: PACKAGE VERSION\n"
10-
"POT-Creation-Date: 2025-04-06 07:24+0000\n"
10+
"POT-Creation-Date: 2025-07-18 14:58+0000\n"
1111
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1212
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1313
"Language-Team: LANGUAGE <LL@li.org>\n"
@@ -192,6 +192,24 @@ msgid ""
192192
"];\n"
193193
msgstr ""
194194

195+
#. type: Title ####
196+
#: guide/en/result.md
197+
#, no-wrap
198+
msgid "Filtering by a specific path"
199+
msgstr ""
200+
201+
#. type: Plain text
202+
#: guide/en/result.md
203+
msgid ""
204+
"This list of error messages can be filtered by a specific path to property."
205+
msgstr ""
206+
207+
#. type: Fenced code block (php)
208+
#: guide/en/result.md
209+
#, no-wrap
210+
msgid "$result->getPropertyErrorMessagesByPath(['person', 'first_name']);\n"
211+
msgstr ""
212+
195213
#. type: Title ###
196214
#: guide/en/result.md
197215
#, no-wrap

docs/po/result.md/ru/result.md.ru.po

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
msgid ""
77
msgstr ""
88
"Project-Id-Version: \n"
9-
"POT-Creation-Date: 2025-04-06 07:24+0000\n"
9+
"POT-Creation-Date: 2025-07-18 14:58+0000\n"
1010
"PO-Revision-Date: 2025-04-12 21:30+0500\n"
1111
"Last-Translator: Automatically generated\n"
1212
"Language-Team: none\n"
@@ -198,6 +198,27 @@ msgstr ""
198198
" 'This value is not a valid email address.',\n"
199199
"];\n"
200200

201+
#. type: Title ####
202+
#: guide/en/result.md
203+
#, fuzzy, no-wrap
204+
#| msgid "Filtering by a specific property"
205+
msgid "Filtering by a specific path"
206+
msgstr "Фильтрация по определенному свойству"
207+
208+
#. type: Plain text
209+
#: guide/en/result.md
210+
#, fuzzy
211+
#| msgid "Error messages not bound to a specific property"
212+
msgid "This list of error messages can be filtered by a specific path to property."
213+
msgstr "Сообщения об ошибках, не привязанные к определенному свойству"
214+
215+
#. type: Fenced code block (php)
216+
#: guide/en/result.md
217+
#, fuzzy, no-wrap
218+
#| msgid "$result->getPropertyErrorMessages('email');\n"
219+
msgid "$result->getPropertyErrorMessagesByPath(['person', 'first_name']);\n"
220+
msgstr "$result->getPropertyErrorMessages('email');\n"
221+
201222
#. type: Title ###
202223
#: guide/en/result.md
203224
#, no-wrap

src/Result.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Stringable;
99

1010
use function array_slice;
11+
use function count;
1112
use function implode;
1213
use function is_string;
1314

@@ -202,6 +203,23 @@ public function getPropertyErrorMessages(string $property): array
202203
return $errors;
203204
}
204205

206+
/**
207+
* Get an array of error messages for the path specified.
208+
*
209+
* @psalm-param list<string> $path
210+
* @psalm-return list<string>
211+
*/
212+
public function getPropertyErrorMessagesByPath(array $path): array
213+
{
214+
$errors = [];
215+
foreach ($this->errors as $error) {
216+
if ($path === array_slice($error->getValuePath(), 0, count($path))) {
217+
$errors[] = $error->getMessage();
218+
}
219+
}
220+
return $errors;
221+
}
222+
205223
/**
206224
* Get arrays of error messages for the property specified indexed by property path.
207225
* Each key is a dot-separated property path.

tests/ResultTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,23 @@ public function testGetAttributeErrorMessages(): void
189189
$this->assertEquals(['error4.1', 'error4.2'], $result->getPropertyErrorMessages('attribute4'));
190190
}
191191

192+
public function testGetPropertyErrorMessagesByPath(): void
193+
{
194+
$result = (new Result())
195+
->addError('e1', valuePath: ['age'])
196+
->addError('e2', valuePath: ['person'])
197+
->addError('e3', valuePath: ['person', 'first_name'])
198+
->addError('e4', valuePath: ['person', 'first_name'])
199+
->addError('e5', valuePath: ['person', 'last_name'])
200+
->addError('e6');
201+
202+
$this->assertSame(['e1', 'e2', 'e3', 'e4', 'e5', 'e6'], $result->getPropertyErrorMessagesByPath([]));
203+
$this->assertSame([], $result->getPropertyErrorMessagesByPath(['non-exists']));
204+
$this->assertSame(['e1'], $result->getPropertyErrorMessagesByPath(['age']));
205+
$this->assertSame(['e2', 'e3', 'e4', 'e5'], $result->getPropertyErrorMessagesByPath(['person']));
206+
$this->assertSame(['e3', 'e4'], $result->getPropertyErrorMessagesByPath(['person', 'first_name']));
207+
}
208+
192209
public function testGetAttributeErrorMessagesIndexedByPath(): void
193210
{
194211
$result = $this->createAttributeErrorResult();

0 commit comments

Comments
 (0)