connorvg / laravel-transform
Requires
- php: >=5.3.0
Suggests
- connorvg/laravel-permissions: A Laravel permissions package that uses keys rather than roles
- connorvg/laravel-wolframalpha: A Laravel wrapper for the Wolfram|Alpha API
- connorvg/php-slack: An Eloquent ORM styled PHP implementation for the Slack API w/ Laravel binds to help out Laravel lovers
- connorvg/php-wtf: A PHP implementation of my WholeTextFunctions library
This package is not auto-updated.
Last update: 2016-08-30 15:03:53 UTC
README
在composer.json文件的require键中添加以下内容:
"connorvg/laravel-transform": "dev-master"
运行Composer更新命令
$ composer update
或者
您可以在命令行界面运行composer require connorvg/laravel-transform命令。
Laravel
如果您正在使用laravel,请添加以下服务提供者
'ConnorVG\Transform\TransformServiceProvider'
此外,此外观
'Transform' => 'ConnorVG\Transform\TransformFacade'
用法
一旦您熟悉了Transform,它是一个非常简单的包,我将在这里尽可能详细地说明它
Transform实际上只有一个功能
::make( Array or something with ->toArray() as input, Array of definitions, Array of aliases *optional* );输入
输入可以是任何数组或具有返回数组的toArray()方法的对象。您还可以有元数组(多维数组),框架会为您处理这些。
例如
[
'this' => 'that',
'hey' => 'bye',
'more' => [
'go' => 'where?'
'to' => 'there!'
]
]
定义定义用于定义类型,这将在需要时将值转换为类型。
例如
输入$input = [
'some_number' => '18',
'a_bool' => '0',
'more_stuff' => [
'yeah' => '1'
]
]
定义定义用于将值定义为类型,这实际上设置了对象类型(例如,从'5'到5)。
$defs = [
'some_number' => 'int',
'a_bool' => 'bool',
'more_stuff' => [
'yeah' => 'bool'
]
]
这些,用作Transform::make($input, $defs);,将输出
[
'some_number' => 18,
'a_bool' => false,
'more_stuff' => [
'yeah' => true
]
]
您还可以为对象数组(数组)递归地定义,因此这
$input = [
[ 'id' => '1', 'active' => '1' ],
[ 'id' => '2', 'active' => '0' ],
[ 'id' => '3', 'active' => '1' ]
]
可以使用索引0来定义,这是用于找到递归定义的方法
$defs = [
[ 'id' => 'int', 'active' => 'bool' ]
]
这些,用作Transform::make($input, $defs);,将输出
[
[ 'id' => 1, 'active' => true ],
[ 'id' => 2, 'active' => false ],
[ 'id' => 3, 'active' => true ]
]
注意:这些类型的定义可以一起使用
别名别名是隐藏或重命名字段的方法,这对于API使用非常有用。要隐藏值,只需将其别名设置为null。
$input = [
[ 'id' => '1', 'active' => '1', 'password' => 'some_pass' ],
[ 'id' => '2', 'active' => '0', 'password' => 'some_pass' ],
[ 'id' => '3', 'active' => '1', 'password' => 'some_pass' ],
'count' => '3'
]
您可以这样别名这些
$aliases = [
[ 'active' => 'alive', 'password' => null ],
'count' => 'amount'
]
这些,用作Transform::make($input, [], $aliases);,将输出
[
[ 'id' => '1', 'alive' => '1' ],
[ 'id' => '2', 'alive' => '0' ],
[ 'id' => '3', 'alive' => '1' ],
'amount' => '3'
]
要重命名数组,很简单。要么作为一个变量做(如果您希望保留其内容),或者将其设置为[ NEW_NAME, [ CONTENTS_ALIASES ] ],如果NEW_NAME为null,则不会更改数组的名称。
[
'test' => [
'this' => 'that'
]
]
使用此别名数组
[
'test' => [ null, [
'this' => 'nope'
]]
]
将输出
[
'test' => [
'nope' => 'this'
]
]
不要害怕嵌套,因为每个数组都被视为根,所以可以完全嵌套。
示例// CALL //
User::with('account')->first();
// OUTPUT //
{
id: "1",
name: "Connor Parks",
url_name: "connor-parks",
created_at: "2014-04-30 09:50:08",
updated_at: "2014-04-30 09:50:08",
account: {
id: "2",
email: "Connor@ConnorVG.tv",
remember_token: "SOME_CODE",
confirmation_code: "",
confirmed: "1",
accountable_id: "1",
accountable_type: "User",
created_at: "2014-04-30 09:50:08",
updated_at: "2014-04-30 10:03:10"
}
}
// CALL //
Transform::make(User::with('account')->first(),
[
'id' => 'int',
'account' => [
'confirmed' => 'bool'
]
],
[
'created_at' => 'since',
'updated_at' => null,
'account' => [ null, [
'id' => null,
'remember_token' => null,
'confirmation_code' => null,
'accountable_id' => null,
'accountable_type' => 'type',
'created_at' => null,
'updated_at' => null
]]
]
);
// OUTPUT //
{
id: 1,
name: "Connor Parks",
url_name: "connor-parks",
since: "2014-04-30 09:50:08",
account: {
email: "Connor@ConnorVG.tv",
confirmed: true,
type: "User"
}
}