0 follower

Final Class Yiisoft\Yii\DataView\ValuePresenter\SimpleValuePresenter

InheritanceYiisoft\Yii\DataView\ValuePresenter\SimpleValuePresenter
ImplementsYiisoft\Yii\DataView\ValuePresenter\ValuePresenterInterface

Presents scalars and Stringable objects as strings.

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( string $null '', string $true 'True', string $false 'False', string $dateTimeFormat 'Y-m-d H:i:s' )
$null string

Label to use for null.

$true string

Label to use for true.

$false string

Label to use for false.

$dateTimeFormat string

The format to use for DateTimeInterface objects.

                public function __construct(
    private readonly string $null = '',
    private readonly string $true = 'True',
    private readonly string $false = 'False',
    private readonly string $dateTimeFormat = 'Y-m-d H:i:s',
) {}

            
present() public method

public string|\Stringable present ( mixed $value )
$value mixed
throws InvalidArgumentException

                public function present(mixed $value): string|Stringable
{
    return match (gettype($value)) {
        'NULL' => $this->null,
        'boolean' => $value ? $this->true : $this->false,
        'string' => $value,
        'integer', 'double' => (string) $value,
        'object' => match (true) {
            $value instanceof Stringable => $value,
            $value instanceof DateTimeInterface => $value->format($this->dateTimeFormat),
            $value instanceof UnitEnum => $value->name,
            default => $this->throwUnsupportedType($value),
        },
        default => $this->throwUnsupportedType($value),
    };
}