Skip to content

Commit

Permalink
Merge pull request #1 from phly/feat/result-set-property-access
Browse files Browse the repository at this point in the history
Allow access to Result instances via ResultSet property access
  • Loading branch information
weierophinney authored Nov 10, 2023
2 parents 13b1179 + 0ebf4f3 commit d7bfc57
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,9 @@ It defines the following methods:
```php
final class ResultSet implements IteratorAggregate
{
/** Returns the Result associated with $key, allowing property access to individual results */
public function __get(string $key): ?Result;

public function getIterator(): Traversable;

public function add(Result $result): void;
Expand All @@ -352,14 +355,19 @@ final class ResultSet implements IteratorAggregate
}
```

You can retrieve individual `Phly\RuleValidation\Result` instances using the `getResultForKey(string $key)` method.

> Internally, `RuleSet` calls `freeze()` on a `ResultSet` before returning it from `RuleSet::validate()`.
You can retrieve individual `Phly\RuleValidation\Result` instances using the `getResultForKey(string $key)` method, or via property access, using the key:

```php
$result = $results->getResultForKey('flag');
$result = $results->flag;
```

Access individual results when generating an HTML form:

```php
<?php $title = $results->getResultForKey('title'); // or $results['title'] ?>
<?php $title = $results->title; // or $results->getResultForKey('title') ?>
<input type="text" name="title" value="<?= $this->e($title->value) ?>">
<?php if (! $title->isValid): ?>
<p class="form-error"><?= $this->e($title->message) ?></p>
Expand Down
5 changes: 5 additions & 0 deletions src/ResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public function __construct(Result ...$results)
}
}

public function __get(string $key): ?Result
{
return array_key_exists($key, $this->results) ? $this->results[$key] : null;
}

/** @return Traversable<Result> */
public function getIterator(): Traversable
{
Expand Down
14 changes: 14 additions & 0 deletions test/ResultSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,18 @@ public function testAttemptingToAddAResultToAFrozenResultSetRaisesException(): v
$this->expectException(ResultSetFrozenException::class);
$resultSet->add(Result::forValidValue('flag', true));
}

public function testAllowsAccessOfIndividualResultsViaPropertyAccessViaKey(): void
{
$result1 = Result::forValidValue('first', 1);
$result2 = Result::forValidValue('second', 2);
$result3 = Result::forInvalidValue('third', 3, 'not a valid value');
$result4 = Result::forValidValue('fourth', 4);
$resultSet = new ResultSet($result1, $result2, $result3, $result4);

$this->assertSame($result1, $resultSet->first);
$this->assertSame($result2, $resultSet->second);
$this->assertSame($result3, $resultSet->third);
$this->assertSame($result4, $resultSet->fourth);
}
}

0 comments on commit d7bfc57

Please sign in to comment.