bfg/object

用于处理对象和数组

维护者

详细信息

github.com/bfg-s/object

源代码

问题

安装数: 17,177

依赖项: 5

建议者: 0

安全: 0

星标: 1

关注者: 2

分支: 1

公开问题: 0

类型:bfg-app

0.0.4 2023-05-22 14:25 UTC

This package is auto-updated.

Last update: 2024-09-22 17:29:38 UTC


README

用于处理对象和数组。

GA Object

GA Object对象设计用于递归数据填充和从可自动填充的分区构建电路及其子电路。标准GA Object连接了app/Gags文件夹中的所有类,所有类都会自动解析并存储在GA Object中。

默认情况下,已经有一个收集到的GA Object Bfg\Object\Gag,您可以根据需要全局使用。还有创建自己的GA Object集合的机会。

考虑标准GA Object填充的情况

\Bfg\Object\Gag::register('gag_name', MyComponent::class);
\Bfg\Object\Gag::register('gag_next', MyNextComponent::class);

实现方法

public function gag(\Bfg\Object\Gag $gag)
{
    $gag->gag_name(...$construct_arguments)
        ->before(function (...$construct_arguments) {}) 
        // "before" To call an event before initialization.
        ->then(function (MyComponent $component, ...$construct_arguments) {})
        // "then" To call an event after initialization.
        ->gag_next();

    return $gag;
}

初始化器将进入它

\Bfg\Object\Gag::instance(MyComponent::class, function (
    MyComponent $component, array $child, ...$construct_arguments
) {
    return $component->applyChilds($child);
});

如果您需要包装已经准备好的GA组件,且无法重载或替换它,则可以为这种情况添加一个包装器,priment helper view就是通过这样的包装器组织的

<?php
use Bfg\Object\GagCore;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;

/**
 * Class ViewComponent
 * @package Bfg\Layout\BodyComponents
 */
class ViewComponent
{
    /**
     * The name of the component in the Gag storage.
     * 
     * @var string
     */
    static string $name = "view";

    /**
     * To save in the designer of the object template.
     * 
     * @var Application|Factory|View
     */
    public Application|Factory|View $view;

    /**
     * ViewComponent constructor.
     *
     * @param  string  $name
     * @param  array  $data
     * @param  array  $mergeData
     */
    public function __construct(string $name, array $data = [], array $mergeData = [])
    {
        $this->view = view($name, $data, $mergeData);
    }
    
    /**
     * To call an event before initialization.
     * 
     * @param ...$construct_arguments
     */
    public function gagBefore(...$construct_arguments){
        //
    }
    
    /**
     * To call an event after initialization.
     * 
     * @param ...$construct_arguments
     */
    public function gagThen(...$construct_arguments){
        //
    }

    /**
     * For the use of GAG object to sequence.
     * 
     * @param  GagCore  $core
     * @return string
     */
    public function gagApply(GagCore $core): string
    {
        return $this->view->with('content', implode('', $core->child))->render();
    }
}

因此,如果您需要创建自己的GA Object,可以采取以下方法

<?php

use Bfg\Object\GagCore;

/**
 * @mixin \MyGags
 */
class MyGag extends GagCore
{
    /**
     * Storage of components
     * @var array
     */
    #[
        StaticClassStorage('MyComponents'),
        StaticClassStorage('app/Components', false),
        DocMethods([Body::class, 'static'], '{key}({value_construct})', 'Storage body gag {key} component'),
        DocClassName('{class}Gags')
    ]
    static array $storage = [];

    /**
     * Gag instances for subject injection with child
     * @var array
     */
    protected static array $instances = [];
}

静态类存储

这是一个允许您扫描包含类的文件夹并为其创建静态属性列表的属性。

注意!属性必须是静态公共数组!

#[StaticClassStorage('Components')]
static array $classes = [];

集合

向集合添加一个paginate方法,方便从集合创建分页器。

collect([])->paginate($perPage = 15, $pageName = 'page', $page = null);

辅助工具

管道

管道的组织方式与Laravel中间件实现相同。

pipeline($send, array $pipes);

is_call

当调用一个项目时。

is_call(mixed $subject);

is_assoc

检查数组是否为关联数组。

is_assoc(array $arr);

array_merge_recursive_distinct

array_merge_recursive确实合并数组,但它将具有重复键的值转换为数组,而不是用第二个数组中的重复值覆盖第一个数组中的值,如array_merge所做的那样。即,使用array_merge_recursive,将发生这种情况(已记录的行为)

array_merge_recursive(array('key' => 'org value'), array('key' => 'new value'));
     => array('key' => array('org value', 'new value'));

array_merge_recursive_distinct不改变数组中值的类型。第二个数组中匹配键的值覆盖第一个数组中的值,如array_merge所做的那样,即。

array_merge_recursive_distinct(array('key' => 'org value'), array('key' => 'new value'));
     => array('key' => array('new value'));

参数通过引用传递,尽管只是为了性能原因。它们不会被此函数更改。

array_dots_uncollapse

展开折叠为点数组的数组。

array_dots_uncollapse(array $array);

multi_dot_call

使用点路径方法访问对象或数组的属性。

multi_dot_call($obj, string $dot_path, bool $locale = true);