[PHP-NOTES] note 130358 added to function.echo

[email protected] ("[email protected]")
Newsgroups php.notes
Message-ID <[email protected]>
echo "hello";

<?php
namespace entidad;

use DateTime;
use Exception;
use JsonSerializable;
use ReflectionProperty;

class Libro implements JsonSerializable{
    public const FECHA_HORA_MYSQL = "Y-m-d H:i:s";
    public const FECHA_HORA_USUARIO = "d/m/Y H:i:s";
    public const FECHA_HORA_ORACLE = "d-m-y H:i:s";
    protected int $id;
    protected string $titulo;
    protected string $autor;
    protected string $genero;
    protected string $isbn;
    protected string $precio;
    protected DateTime $fecha_publicacion;
    protected string $editorial;
    protected string $paginas;
    protected bool $disponible;

    public function __construct(array $datos){
        $this->id = $datos["id"];
        $this->titulo = $datos["titulo"];
        $this->autor = $datos["autor"];
        $this->genero = $datos["genero"];
        $this->isbn = $datos["isbn"];
        $this->precio = $datos["precio"];
        $this->fecha_publicacion = new DateTime($datos["fecha_publicacion"]);
        $this->editorial = $datos["editorial"];
        $this->paginas = $datos["paginas"];
        $this->disponible = $datos["disponible"];
    }

    public function __get($propiedad){
        if(!property_exists($this,$propiedad)){
            throw new Exception("No existe la propiedad en el objeto");
        }
        return $this->$propiedad;
    }

    public function __set($propiedad,$valor){
        if(!property_exists($this,$propiedad)){
            throw new Exception("No existe la propiedad en el objeto");
        }
        $this->$propiedad = $valor; 
    }

    public function toArray(): array {
        return get_object_vars($this);
    }
    
    public function __serialize(): array {
        return $this->toArray();
    }

    public function __sleep(): array {
        // Accedemos a las propiedades
        foreach( $this as $propiedad => $valor ) {
            $propiedades[] = $propiedad;
        }

        return $propiedades;
    }

    public function jsonSerialize(): mixed {
        $objeto_json = [];
        
        foreach($this as $propiedad => $valor ) {
            $reflexion_propiedad = new ReflectionProperty($this, $propiedad);
            $tipo_datos = $reflexion_propiedad->getType()->getName();
            if ( $tipo_datos === DateTime::class && $valor ) {
                $objeto_json[$propiedad] = $valor->format(self::FECHA_HORA_USUARIO);
            }
            else {
                $objeto_json[$propiedad] = $valor;
            }          
        }
        return  $objeto_json;
    }
           

    public function JSON(): string{
        $datos = [
            "id" => $this->id,
            "titulo" => $this->titulo,
            "autor" => $this->autor,
            "genero" => $this->genero,
            "isbn" => $this->isbn,
            "precio" => $this->precio,
            "fecha publicacion" => $this->fecha_publicacion,
            "editorial" => $this->editorial,
            "paginas" => $this->paginas,
            "disponible" => $this->disponible
        ];
        return json_encode($datos);
    }
}
?>
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.137 (proxied: 2a0c:5a83:cc09:8c00:8c79:8374:c878:e62)
----
Manual Page -- https://php.net/manual/en/function.echo.php
Edit        -- https://main.php.net/note/edit/130358
Del: integrated  -- https://main.php.net/note/delete/130358/integrated
Del: useless     -- https://main.php.net/note/delete/130358/useless
Del: bad code    -- https://main.php.net/note/delete/130358/bad+code
Del: spam        -- https://main.php.net/note/delete/130358/spam
Del: non-english -- https://main.php.net/note/delete/130358/non-english
Del: in docs     -- https://main.php.net/note/delete/130358/in+docs
Del: other reasons-- https://main.php.net/note/delete/130358
Reject      -- https://main.php.net/note/reject/130358
Search      -- https://main.php.net/manage/user-notes.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.