fas/exportable

快速简单的变量导出器

0.2.0 2021-07-09 06:41 UTC

This package is auto-updated.

Last update: 2024-09-09 13:19:15 UTC


README

Build Status Test Coverage

Latest Stable Version Latest Unstable Version License Total Downloads

安装

composer require fas/exportable

用法

闭包

<?php

require './vendor/autoload.php';

use Fas\Exportable\Exporter;

$exporter = new Exporter();

$data = [
    'somefunc' => static function () {
        return 'test';
    }
];

$output = $exporter->export($data);

print "$output\n";

输出

[
  'somefunc' => static function () {
        return 'test';
    }
]

自定义对象

<?php

require './vendor/autoload.php';

use Fas\Exportable\ExportableInterface;
use Fas\Exportable\Exporter;

class UpperCase implements ExportableInterface
{
    private string $str;

    public function __construct(string $str)
    {
        $this->str = $str;
    }

    public function exportable(Exporter $exporter, $level = 0): string
    {
        return var_export(strtoupper($this->str), true);
    }
}

$exporter = new Exporter();

$data = [
    'somekey' => new UpperCase('somevalue'),
];

$output = $exporter->export($data);

print "$output\n";

输出

[
  'somekey' => 'SOMEVALUE'
]