germix / fields-formatter
格式化对象字段
dev-master
2020-12-21 11:58 UTC
This package is not auto-updated.
Last update: 2024-09-29 06:14:22 UTC
README
安装
composer require germix/fields-formatter
如何使用
假设我们有两个类叫做 Post 和 Comment,它们实现了 FieldsFormatterEntity
Post 类
class Post implements FieldsFormatterEntity
{
private $id;
private $user;
private $title;
private $status;
private $content;
private $comments;
public function __construct()
{
$this->id = 1;
$this->user = "John";
$this->title = 'My post';
$this->status = 'publish';
$this->content = 'My post content';
$this->comments = [
new Comment(1, 'Peter', 'Comment 1'),
new Comment(2, 'Louis', 'Comment 2'),
new Comment(3, 'Richard', 'Comment 3'),
];
}
public function getId()
{
return $this->id;
}
public function getUser()
{
return $this->user;
}
public function getTitle()
{
return $this->title;
}
public function getStatus()
{
return $this->status;
}
public function getContent()
{
return $this->content;
}
public function getComments()
{
return $this->comments;
}
public function formatField($formatter, $field, $subfields, &$valid, $data)
{
switch($field)
{
case 'id': return $this->getId();
case 'user': return $this->getUser();
case 'title': return $this->getTitle();
case 'status': return $this->getStatus();
case 'content': return $this->getContent();
case 'comments': return $formatter->format($this->getComments(), $subfields);
}
$valid = false; return null;
}
public function defaultFormatterFields()
{
return
[ 'id'
, 'user'
, 'title'
, 'status'
, 'content'
, 'comments'
];
}
}
Comment 类
class Comment implements FieldsFormatterEntity
{
private $id;
private $user;
private $message;
public function __construct($id, $user, $message)
{
$this->id = $id;
$this->user = $user;
$this->message = $message;
}
public function getId()
{
return $this->id;
}
public function getUser()
{
return $this->user;
}
public function getMessage()
{
return $this->message;
}
public function formatField($formatter, $field, $subfields, &$valid, $data)
{
switch($field)
{
case 'id': return $this->getId();
case 'user': return $this->getUser();
case 'message': return $this->getMessage();
}
$valid = false; return null;
}
public function defaultFormatterFields()
{
return
[ 'id'
, 'user'
, 'message'
];
}
}
假设我们有一个名为 Post 的对象 $post,我们可以这样格式化它
$post = new Post();
$formatter = new FieldsFormatted();
$result = $formatter->formatter($post, null);
var_dump($result);
在这种情况下,$post 将使用默认字段进行格式化,我们将得到以下结果
array(6) {
["id"]=>
int(1)
["user"]=>
string(4) "John"
["title"]=>
string(7) "My post"
["status"]=>
string(7) "publish"
["content"]=>
string(15) "My post content"
["comments"]=>
array(3) {
[0]=>
array(3) {
["id"]=>
int(1)
["user"]=>
string(5) "Peter"
["message"]=>
string(9) "Comment 1"
}
[1]=>
array(3) {
["id"]=>
int(2)
["user"]=>
string(5) "Louis"
["message"]=>
string(9) "Comment 2"
}
[2]=>
array(3) {
["id"]=>
int(3)
["user"]=>
string(7) "Richard"
["message"]=>
string(9) "Comment 3"
}
}
}
如果我们想格式化某些特定的字段,我们可以使用逗号分隔的字段字符串
$result = $formatter->formatter($post, 'id,title,comments{message}');
或者使用字段数组...
$result = $formatter->formatter($post, ['id', 'title', [ 'comments', [ 'message'] ]]);
在上述两种情况下,我们将得到以下结果
array(3) {
["id"]=>
int(1)
["title"]=>
string(7) "My post"
["comments"]=>
array(3) {
[0]=>
array(1) {
["message"]=>
string(9) "Comment 1"
}
[1]=>
array(1) {
["message"]=>
string(9) "Comment 2"
}
[2]=>
array(1) {
["message"]=>
string(9) "Comment 3"
}
}
}
最后,我们可以将结果编码为JSON。
echo json_encode($result, JSON_PRETTY_PRINT);
我们将得到类似以下的内容
{
"id": 1,
"user": "John",
"title": "My post",
"status": "publish",
"content": "My post content",
"comments": [
{
"id": 1,
"user": "Peter",
"message": "Comment 1"
},
{
"id": 2,
"user": "Louis",
"message": "Comment 2"
},
{
"id": 3,
"user": "Richard",
"message": "Comment 3"
}
]
}