承接 graze/formatter 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

graze/formatter

最新稳定版本:v0.2.0

Composer 安装命令:

composer require graze/formatter

包简介

Convert objects into arrays of data by applying processors, filters, and sorters.

README 文档

README

Convert objects into arrays of data by applying processors, filters, and sorters.

Read more about why we made this library in our blog post.

Installation

We recommend installing this library with Composer.

~$ composer require graze/formatter

Usage

// Create a formatter ...
class CountableFormatter extends \Graze\Formatter\AbstractFormatter
{
    protected function convert($object)
    {
        if (! $object instanceof Countable) {
            throw new \InvalidArgumentException(sprintf('`$object` must be an instance of %s.', Countable::class));
        }

        return [
            'count' => $object->count(),
        ];
    }
}

// ... processor ...
$processor = function (array $data, Countable $object) {
    // Let's add the square of count.
    $data['square'] = $data['count'] ** 2;

    return $data;
};

// ... filter ...
$filter = function (array $data) {
    // Remove elements with a square of 9.
    return $data['square'] !== 9;
};

// ... sorter ...
$sorter = function (array $data) {
    // Sort by count in descending order.
    return $data['count'] * -1;
};

// ... and something we can format.
class ExampleCountable implements Countable
{
    private $count = 0;

    public function count()
    {
        return $this->count += 1;
    }
}

$countable = new ExampleCountable();

// Create a new instance of the formatter, and register all the callables.
$formatter = new CountableFormatter();
$formatter->addProcessor($processor);
$formatter->addFilter($filter);
$formatter->addSorter($sorter);

// Format a single object.
$result = $formatter->format($countable);

print_r($result);

// Format several objects.
$result = $formatter->formatMany([$countable, $countable, $countable]);

print_r($result);

The above example will output:

Array
(
    [count] => 1
    [square] => 1
)
Array
(
    [0] => Array
        (
            [count] => 4
            [square] => 16
        )

    [1] => Array
        (
            [count] => 2
            [square] => 4
        )

)

Find more documentation under docs/.

统计信息

  • 总下载量: 24.62k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 11
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-12-09