Skip to content

Commit

Permalink
Refactor and rename InvalidAttributeException to `UndefinedProperty…
Browse files Browse the repository at this point in the history
…Exception` (#270)
  • Loading branch information
vjik committed Nov 17, 2023
1 parent bdfe924 commit dad3a5a
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 49 deletions.
26 changes: 7 additions & 19 deletions src/FormModel.php
Expand Up @@ -166,7 +166,7 @@ public function hasAttribute(string $attribute): bool
{
try {
$this->readAttributeValue($attribute);
} catch (InvalidAttributeException) {
} catch (UndefinedPropertyException) {
return false;
}
return true;
Expand All @@ -178,7 +178,7 @@ public function isValid(): bool
}

/**
* @throws InvalidAttributeException
* @throws UndefinedPropertyException If the property is not in the set.
*/
private function readAttributeValue(string $attribute): mixed
{
Expand All @@ -191,35 +191,31 @@ private function readAttributeValue(string $attribute): mixed

if (is_array($value)) {
if (array_key_exists($key, $value)) {
/** @var mixed $value */
$value = $value[$key];
continue;
}
throw $this->createNotFoundException($keys);
throw UndefinedPropertyException::forUndefinedProperty($this->makePropertyPathString($keys));
}

if (is_object($value)) {
$class = new ReflectionClass($value);
try {
$property = $class->getProperty($key);
} catch (ReflectionException) {
throw $this->createNotFoundException($keys);
throw UndefinedPropertyException::forUndefinedProperty($this->makePropertyPathString($keys));
}
if ($property->isStatic()) {
throw $this->createNotFoundException($keys);
throw UndefinedPropertyException::forUndefinedProperty($this->makePropertyPathString($keys));
}
if (PHP_VERSION_ID < 80100) {
$property->setAccessible(true);
}
/** @var mixed $value */
$value = $property->getValue($value);
continue;
}

array_pop($keys);
throw new InvalidAttributeException(
sprintf('Attribute "%s" is not a nested attribute.', $this->makePathString($keys))
);
throw UndefinedPropertyException::forNotNestedProperty($this->makePropertyPathString($keys));
}

return $value;
Expand Down Expand Up @@ -305,15 +301,7 @@ private function normalizePath(string $attribute): array
/**
* @psalm-param array<array-key, array{0:int|string, 1:mixed}> $keys
*/
private function createNotFoundException(array $keys): InvalidArgumentException
{
return new InvalidAttributeException('Undefined property: "' . $this->makePathString($keys) . '".');
}

/**
* @psalm-param array<array-key, array{0:int|string, 1:mixed}> $keys
*/
private function makePathString(array $keys): string
private function makePropertyPathString(array $keys): string
{
$path = '';
foreach ($keys as $key) {
Expand Down
11 changes: 0 additions & 11 deletions src/InvalidAttributeException.php

This file was deleted.

25 changes: 25 additions & 0 deletions src/UndefinedPropertyException.php
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Form;

use InvalidArgumentException;

final class UndefinedPropertyException extends InvalidArgumentException
{
private function __construct(string $message)
{
parent::__construct($message);
}

public static function forUndefinedProperty(string $property): self
{
return new self('Undefined property: "' . $property . '".');
}

public static function forNotNestedProperty(string $property): self
{
return new self('Property "' . $property . '" is not a nested attribute.');
}
}
29 changes: 15 additions & 14 deletions tests/FormModelTest.php
Expand Up @@ -13,9 +13,10 @@
use Yiisoft\Form\Tests\Support\TestHelper;
use Yiisoft\Form\Tests\TestSupport\Form\CustomFormNameForm;
use Yiisoft\Form\Tests\TestSupport\Form\DefaultFormNameForm;
use Yiisoft\Form\Tests\TestSupport\Form\FormWithNestedAttribute;
use Yiisoft\Form\Tests\TestSupport\Form\FormWithNestedProperty;
use Yiisoft\Form\Tests\TestSupport\Form\LoginForm;
use Yiisoft\Form\Tests\TestSupport\TestTrait;
use Yiisoft\Form\UndefinedPropertyException;

require __DIR__ . '/TestSupport/Form/NonNamespacedForm.php';

Expand Down Expand Up @@ -113,34 +114,34 @@ public function testGetAttributesLabels(): void
$this->assertSame($expected, $form->getAttributeLabels());
}

public function testGetNestedAttributeException(): void
public function testGetNestedPropertyException(): void
{
$form = new FormWithNestedAttribute();
$form = new FormWithNestedProperty();

$this->expectException(InvalidArgumentException::class);
$this->expectException(UndefinedPropertyException::class);
$this->expectExceptionMessage(
'Attribute "' . FormWithNestedAttribute::class . '::id" is not a nested attribute.'
'Property "' . FormWithNestedProperty::class . '::id" is not a nested attribute.'
);
$form->getAttributeValue('id.profile');
}

public function testGetNestedAttributeHint(): void
{
$form = new FormWithNestedAttribute();
$form = new FormWithNestedProperty();

$this->assertSame('Write your id or email.', $form->getAttributeHint('user.login'));
}

public function testGetNestedAttributeLabel(): void
{
$form = new FormWithNestedAttribute();
$form = new FormWithNestedProperty();

$this->assertSame('Login:', $form->getAttributeLabel('user.login'));
}

public function testGetNestedAttributePlaceHolder(): void
{
$form = new FormWithNestedAttribute();
$form = new FormWithNestedProperty();

$this->assertSame('Type Username or Email.', $form->getAttributePlaceHolder('user.login'));
}
Expand Down Expand Up @@ -181,7 +182,7 @@ public function testGetAttributeValueException(): void

public function testGetAttributeValueWithNestedAttribute(): void
{
$form = new FormWithNestedAttribute();
$form = new FormWithNestedProperty();

$form->setUserLogin('admin');
$this->assertSame('admin', $form->getAttributeValue('user.login'));
Expand All @@ -200,7 +201,7 @@ public function testHasAttribute(): void

public function testHasNestedAttribute(): void
{
$form = new FormWithNestedAttribute();
$form = new FormWithNestedProperty();

$this->assertTrue($form->hasAttribute('user.login'));
$this->assertTrue($form->hasAttribute('user.password'));
Expand All @@ -210,7 +211,7 @@ public function testHasNestedAttribute(): void

public function testHasNestedAttributeException(): void
{
$form = new FormWithNestedAttribute();
$form = new FormWithNestedProperty();

$this->assertFalse($form->hasAttribute('user.noexist'));
}
Expand Down Expand Up @@ -289,12 +290,12 @@ public function testLoadWithEmptyScope(): void
$this->assertSame('555', $form->getAttributeValue('string'));
}

public function testLoadWithNestedAttribute(): void
public function testLoadWithNestedProperty(): void
{
$form = new FormWithNestedAttribute();
$form = new FormWithNestedProperty();

$data = [
'FormWithNestedAttribute' => [
'FormWithNestedProperty' => [
'user.login' => 'admin',
],
];
Expand Down
8 changes: 4 additions & 4 deletions tests/Helper/HtmlFormErrorsTest.php
Expand Up @@ -8,7 +8,7 @@
use Yiisoft\Form\Helper\HtmlFormErrors;
use Yiisoft\Form\Tests\Support\TestHelper;
use Yiisoft\Form\Tests\TestSupport\Form\LoginForm;
use Yiisoft\Form\Tests\TestSupport\Form\FormWithNestedAttribute;
use Yiisoft\Form\Tests\TestSupport\Form\FormWithNestedProperty;
use Yiisoft\Form\Tests\TestSupport\TestTrait;
use Yiisoft\Validator\Validator;

Expand Down Expand Up @@ -131,13 +131,13 @@ public function testGetErrorSummaryOnlyAttributes(): void
);
}

public function testGetErrorNestedAttribute(): void
public function testGetErrorNestedProperty(): void
{
$formModel = new FormWithNestedAttribute();
$formModel = new FormWithNestedProperty();
$validator = new Validator();
$populateResult = TestHelper::createFormHydrator()->populate(
$formModel,
['FormWithNestedAttribute' => ['user.login' => 'ad']]
['FormWithNestedProperty' => ['user.login' => 'ad']]
);

$this->assertTrue($populateResult);
Expand Down
Expand Up @@ -9,7 +9,7 @@
use Yiisoft\Validator\Rule\Required;
use Yiisoft\Validator\RulesProviderInterface;

final class FormWithNestedAttribute extends FormModel implements RulesProviderInterface
final class FormWithNestedProperty extends FormModel implements RulesProviderInterface
{
private ?int $id = null;
private LoginForm $user;
Expand Down

0 comments on commit dad3a5a

Please sign in to comment.