mirror of
https://github.com/pestphp/pest.git
synced 2026-03-06 15:57:21 +01:00
32 lines
691 B
PHP
32 lines
691 B
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Pest\Concerns;
|
|
|
|
/**
|
|
* @internal
|
|
*/
|
|
trait Retrievable
|
|
{
|
|
/**
|
|
* @template TRetrievableValue
|
|
*
|
|
* Safely retrieve the value at the given key from an object or array.
|
|
* @template TRetrievableValue
|
|
*
|
|
* @param array<string, TRetrievableValue>|object $value
|
|
* @param TRetrievableValue|null $default
|
|
* @return TRetrievableValue|null
|
|
*/
|
|
private function retrieve(string $key, mixed $value, mixed $default = null): mixed
|
|
{
|
|
if (is_array($value)) {
|
|
return $value[$key] ?? $default;
|
|
}
|
|
|
|
// @phpstan-ignore-next-line
|
|
return $value->$key ?? $default;
|
|
}
|
|
}
|