hyperf-view/builder/Traits/JsonBuilder.php

28 lines
643 B
PHP

<?php
declare(strict_types=1);
namespace Builder\Traits;
use Hyperf\Utils\Codec\Json;
class JsonBuilder
{
protected $hideAttrs = [];
public function jsonSerialize()
{
$data = [];
$hide = collect($this->hideAttrs)->push("hideAttrs")->toArray();
foreach ($this as $key => $val) {
if (!in_array($key, $hide) && $val !== null) {
$data[$key] = $val;
}
}
return $data;
}
public function __toString(): string
{
// TODO: Implement __toString() method.
return json_encode($this->jsonSerialize(), JSON_UNESCAPED_UNICODE);
}
}