chocofamilyme / restapi-helpers
RestAPI项目的辅助函数和类
2.0.1
2019-10-25 07:31 UTC
Requires
- php: >= 7.0.0
- ext-phalcon: >= 3.0.0
Requires (Dev)
- phpunit/phpunit: ^4.0|^5.0
This package is auto-updated.
Last update: 2024-09-25 19:10:21 UTC
README
注意:从版本2.x开始,“全局”函数“collect”已更改为“pcollect”
需求
- Phalcon > 3.0.0
- RestAPI
集合
集合接受数组作为参数。
创建
有两种方式可以创建集合
- 通过helper pcollect,示例
$collection = pcollect([1, 2 , 3, 4]); return $collection->first(); //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 或 falsevalues()- 函数 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' ]; }