Skip to content

Commit

Permalink
Simplify wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Steveb-p committed Nov 18, 2024
1 parent ccb944e commit 84ee00f
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 9 deletions.
6 changes: 2 additions & 4 deletions src/contracts/Validation/StructWrapperValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,11 @@ public function validate($value, $constraints = null, $groups = null): Constrain

$unwrappedErrors = new ConstraintViolationList();

// Skip $ from argument name
$prefix = ltrim($value->getStructName(), '$') . '.';
foreach ($result as $error) {
$path = $error->getPropertyPath();
$root = $error->getRoot();
if (str_starts_with($path, $prefix)) {
$path = substr($path, strlen($prefix));
if (str_starts_with($path, 'struct.')) {
$path = substr($path, strlen('struct.'));
$root = $value->getStruct();
}

Expand Down
6 changes: 4 additions & 2 deletions src/contracts/Validation/ValidationStructWrapperInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@

namespace Ibexa\Contracts\Core\Validation;

use Symfony\Component\Validator\Constraints as Assert;

/**
* @template T of object
*/
interface ValidationStructWrapperInterface
{
public function getStructName(): string;

/**
* @phpstan-return T
*
* @Assert\Valid()
*/
public function getStruct(): object;
}
3 changes: 0 additions & 3 deletions tests/lib/Validation/StructWrapperValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,6 @@ public function testAssertValidStructWithInvalidWrapperStruct(): void
$initialErrors = $this->createExampleConstraintViolationList($initialError);

$wrapper = $this->createMock(ValidationStructWrapperInterface::class);
$wrapper->expects(self::once())
->method('getStructName')
->willReturn('$struct');

$struct = new stdClass();
$wrapper->expects(self::once())
Expand Down

0 comments on commit 84ee00f

Please sign in to comment.