承接 wp-grogu/laravel-fluent-plus 相关项目开发

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

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

wp-grogu/laravel-fluent-plus

最新稳定版本:v3.0.0

Composer 安装命令:

composer require wp-grogu/laravel-fluent-plus

包简介

A fork from janpantel/laravel-fluent-plus

README 文档

README

This package extends Laravel's Fluent class to provide a supercharged experience like type casting and recursive Fluent access to sub-assocs.

Installation

Use composer to grep the package :

composer require wp-grogu/laravel-fluent-plus

Basic usage

Instead of extending Illuminate\Support\Fluent you extend Grogu\FluentPlus\FluentPlus instead. As this class is a superset of Fluent it can be treated as a drop in replacement.

FluentPlus implements a $casts property that defines how the attributes of your assoc should be cast.

For the following example assume NamesContainer, Price and StockLevel to be classes extending Fluent or FluentPlus as well.

class MyContainer extends FluentPlus
{
    protected $casts = [
        // Use the contents of the `names` attribute
        // to initialize a new `NamesContainer` instance.
        'names' => NamesContainer::class,

        // Assumes that the `price` attribute contains
        // an array and initializes a new `Price` instance
        // for every item. 
        'prices' => [Price::class],

        // The same as the array syntax but using
        // Laravel's `Collection` class instead.
        'stock' => [
            \Illuminate\Support\Collection::class,
            StockLevel::class
        ],
    ];
}

The resulting object now offers convenient property access:

$container = new MyContainer($sourceAssoc);

var_dump($container->names->english);
var_dump($container->prices[0]->amount);
var_dump($container->stock->get('store_1')->quantity);

The problem this package solves

If you work with assocs a lot and like using property access over assoc access Laravel's Fluent class is a convenient way to do so.

The use case that made me write this package is working with responses fetched from 3rd party APIs. Since I want to use @property PHP docs to have "type safety" while not manually assigning properties.

Advanced usage

Recursive array casts

FluentPlus casts sub-assocs that do not have a cast defined into other FluentPlus for infinite dimension property access.

$instance = new Fluent(['foo' => ['bar' => 'baz']]);
$instancePlus = new FluentPlus(['foo' => ['bar' => 'baz']]);

// breaks :(
var_dump($instance->foo->bar);
// works :)
var_dump($instancePlus->foo->bar);

You can turn this feature off by setting the $recursive property in your derived FluentPlus to false or using the NonRecursiveFluentPlus utility class if you want to inline it instead.

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-04-19