chocofamilyme/restapi-helpers

RestAPI项目的辅助函数和类

2.0.1 2019-10-25 07:31 UTC

This package is auto-updated.

Last update: 2024-09-25 19:10:21 UTC


README

注意:从版本2.x开始,“全局”函数“collect”已更改为“pcollect”

需求

- Phalcon > 3.0.0
- RestAPI

集合

集合接受数组作为参数。

创建

有两种方式可以创建集合

  1. 通过helper pcollect,示例
$collection = pcollect([1, 2 , 3, 4]);
return $collection->first(); //1
  1. 通过类
use Chocofamily\Collection\Collection;
$collection = new Collection([1,2,3,4]);
return $collection->last();//4
方法

目前集合可用的方法列表如下

  • first() - 返回集合的第一个属性
  • last() - 返回集合的最后一个属性
  • key() - 返回当前属性集合的索引。
  • next() - 返回当前属性集合之后的下一个属性
  • current() - 返回当前属性集合
  • all() - 以数组的形式返回集合中的所有属性
  • map() - 对array_map函数的封装
  • mapWithKeys() - 与Laravel中的mapwithkeys方法类似
  • filter() - 对array_filter函数的封装
  • reduce() - 对array_reduce函数的封装
  • diff() - 对array_diff函数的封装
  • diffUsing() - 对array_udiff函数的封装
  • diffAssoc() - 对array_diff_assoc函数的封装
  • diffAssocUsing() - 对array_diff_assoc的封装 -diffKeys() - 对array_diff_key函数的封装
  • diffKeysUsing() - 对array_diff_ukey函数的封装
  • each() - 与Laravel中的each方法类似
  • when() - 与Laravel中的when方法类似
  • flip() - 对array_flip函数的封装
  • splice() - 与Laravel中的splice方法类似
  • merge() - 对array_merge函数的封装
  • combine() - 对array_combine函数的封装
  • partition() - 与Laravel中的partition方法类似
  • reverse() - 对array_reverse函数的封装
  • intersect() - 函数 array_intersect 的包装
  • intersectByKeys() - 函数 array_intersect_key 的包装
  • pad() - 函数 array_pad 的包装
  • slice() - 函数 array_slice 的包装
  • chunk() - Laravel 的 chunk 方法的类似函数
  • exists() - 接受一个回调函数作为参数,根据条件返回 true 或 false
  • values() - 函数 array_values 的包装
  • keys() - 函数 array_keys 的包装
  • add() - 向集合中添加新的属性
  • remove() - 根据键删除属性
  • push() - Laravel 的 push 方法的类似函数
  • sort() - Laravel 的 sort 方法的类似函数

模型

在模型中可以访问所有集合方法。

目前,在模型中可以指定以下属性:

  • 属性 fillable - 用于批量填充的元素列表
  • 属性 required - 必须填充的元素列表(如果缺少 required 列表中的元素,将抛出 MissingRequiredException 异常)
示例
<?php

use Chocofamily\Collection\Model;

class ModelStub extends Model
{
    protected $fillable = [
        'first_name', 'last_name', 'age', 'sex', 'active'
    ];

    protected $required = [
        'first_name'
    ];
}