Skip to content

Commit f35ce6d

Browse files
authored
Merge pull request #95 from kbond/json-cache
CS fixes, update docs, cache decoded json value
2 parents cd3cce3 + 157ce84 commit f35ce6d

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,8 @@ $browser
394394
Make assertions about json responses using [JMESPath expressions](https://jmespath.org/)
395395
See the [JMESPath Tutorials](https://jmespath.org/tutorial.html) to learn more.
396396

397-
**NOTE:** `mtdowling/jmespath.php` is required: `composer require --dev mtdowling/jmespath.php`.
397+
> **Note**
398+
> `mtdowling/jmespath.php` is required: `composer require --dev mtdowling/jmespath.php`.
398399
399400
```php
400401
/** @var \Zenstruck\Browser\KernelBrowser $browser **/
@@ -437,6 +438,10 @@ $json = $browser
437438
;
438439
```
439440

441+
> **Note**
442+
> See the [full `zenstruck/assert` expectation API documentation](https://github.com/zenstruck/assert#expectation-api)
443+
> to see all the methods available on `Zenstruck\Browser\Json`.
444+
440445
### PantherBrowser
441446

442447
*The `PantherBrowser` is experimental in 1.0 and may be subject to BC Breaks.*

src/Browser/Json.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Zenstruck\Assert;
66
use Zenstruck\Assert\Expectation;
7+
78
use function JmesPath\search;
89

910
/**
@@ -15,6 +16,9 @@ final class Json
1516
{
1617
private string $source;
1718

19+
/** @var mixed */
20+
private $decoded;
21+
1822
public function __construct(string $source)
1923
{
2024
$this->source = $source;
@@ -125,11 +129,7 @@ public function search(string $selector)
125129
*/
126130
public function decoded()
127131
{
128-
if (empty($this->source)) {
129-
return null;
130-
}
131-
132-
return \json_decode($this->source, true, 512, \JSON_THROW_ON_ERROR);
132+
return $this->decoded ??= empty($this->source) ? null : \json_decode($this->source, true, 512, \JSON_THROW_ON_ERROR);
133133
}
134134

135135
/**

tests/KernelBrowserAuthenticationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Zenstruck\Browser\Tests\Fixture\Kernel;
1212
use Zenstruck\Foundry\Configuration;
1313
use Zenstruck\Foundry\Factory;
14+
1415
use function Zenstruck\Foundry\factory;
1516

1617
/**

0 commit comments

Comments
 (0)