PHP Classes

File: src/VarDumpable.php

Recommend this page to a friend!
  Classes of Rodolfo Berrios Arce   Var-Dump   src/VarDumpable.php   Download  
File: src/VarDumpable.php
Role: Class source
Content type: text/plain
Description: Class source
Class: Var-Dump
Show the value of a variable in colored way
Author: By
Last change:
Date: 1 month ago
Size: 1,527 bytes
 

Contents

Class file image Download
<?php

/*
 * This file is part of Chevere.
 *
 * (c) Rodolfo Berrios <[email protected]>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

declare(strict_types=1);

namespace
Chevere\VarDump;

use
Chevere\VarDump\Interfaces\VarDumpableInterface;
use
Chevere\VarDump\Interfaces\VarDumperInterface;
use
LogicException;
use function
Chevere\Message\message;
use function
Chevere\Parameter\getType;

final class
VarDumpable implements VarDumpableInterface
{
    private
string $type;

    private
string $processorName;

    public function
__construct(
        private
mixed $var
   
) {
       
$this->type = getType($this->var);
       
$this->assertSetProcessorName();
    }

    public function var():
mixed
   
{
        return
$this->var;
    }

    public function
type(): string
   
{
        return
$this->type;
    }

    public function
processorName(): string
   
{
        return
$this->processorName;
    }

   
/**
     * @codeCoverageIgnore
     * @infection-ignore-all
     */
   
private function assertSetProcessorName(): void
   
{
       
$processorName = VarDumperInterface::PROCESSORS[$this->type] ?? null;
        if (! isset(
$processorName)) {
            throw new
LogicException(
                (string)
message(
                   
'No processor for variable of type `%type%`',
                   
type: $this->type
               
)
            );
        }
       
$this->processorName = $processorName;
    }
}