股票场内基金交易,没时间盯盘?
一句话介绍
Fractal output complex, flexible, ajax/restful data structures
Fractal 为复杂的数据输出提供了样式和转化层。
最简单的例子
正常情况下,下面的代码是分散在项目的各处的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
<?php use League\Fractal\Manager; use League\Fractal\Resource\Collection; // 创建一个 Manager 实例 $fractal = new Manager(); // 从某个地方获取到需要的数据,比如说 User::all() $books = [ [ 'id' => '1', 'title' => 'Hogfather', 'yr' => '1998', 'author_name' => 'Philip K Dick', 'author_email' => 'philip@example.org', ], [ 'id' => '2', 'title' => 'Game Of Kill Everyone', 'yr' => '2014', 'author_name' => 'George R. R. Satan', 'author_email' => 'george@example.org', ] ]; // 把得到的数据,传给一个 Collection。一起传入的还有一个 Transformer,可以是一个回调函数或者一个 Transformer 对象。 $resource = new Collection($books, function(array $book) { return [ 'id' => (int) $book['id'], 'title' => $book['title'], 'year' => (int) $book['yr'], 'author' => [ 'name' => $book['author_name'], 'email' => $book['author_email'], ], 'links' => [ [ 'rel' => 'self', 'uri' => '/books/'.$book['id'], ] ] ]; }); // 然后用 Manager 实例来转化数据 $array = $fractal->createData($resource)->toArray(); // Turn all of that into a JSON string echo $fractal->createData($resource)->toJson(); // 输出: {"data":[{"id":1,"title":"Hogfather","year":1998,"author":{"name":"Philip K Dick","email":"philip@example.org"}},{"id":2,"title":"Game Of Kill Everyone","year":2014,"author":{"name":"George R. R. Satan","email":"george@example.org"}}]} |
几个概念
Resources 资源
Resource 是一个对象,代表了数据,并且知道数据对应的“转化器”是什么。
有两种 Resources:
1. League\Fractal\Resource\Item – 单个资源
2. League\Fractal\Resource\Collection – 资源的集合
它们的构造函数接受实际操作的“数据”作为第一个参数,接受“转化器”作为第二个参数。
Serializers 序列化器
一个序列化器会以某种方式组织你的转化过的数据。Fractal 提供了 DataArraySerializer、ArraySerializer 和 JsonApiSerializer,也可以自己来写。
比较常用的是 DataArraySerializer,但是它会给数据增加一个 data 的命名空间,如果不喜欢,可以使用其他类型,或者自己实现一个。
Transformers 转化器
这个是最常用的一个内容。
转化器类
必须继承 League\Fractal\TransformerAbstract,并且包含一个 transform() 方法。这个方法相当于转化的格式,具体可以参考上面例子里面的回调函数。
Including Data 包含数据
比如我们的 Book 模型是有一些关系的,比如说 作者 Author,出版社 Press。
在模型里我们定义好相关的关系之后,在转化器类里面可以这样来包含相关的(转化好的)数据。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<?php namespace App\Transformer; use Acme\Model\Book; use League\Fractal\TransformerAbstract; class BookTransformer extends TransformerAbstract { protected $defaultIncludes = [ 'author' ]; protected $availableIncludes = [ 'author' ]; public function transform(Book $book) { return [ 'id' => (int) $book->id, 'title' => $book->title, 'year' => (int) $book->yr, 'links' => [ [ 'rel' => 'self', 'uri' => '/books/'.$book->id, ] ], ]; } public function includeAuthor(Book $book) { $author = $book->author; return $this->item($author, new AuthorTransformer); } } |
里面有两种包含,默认的总是会自动获取对应关系的数据,比如 user 的返回数据里面会给每一个 user 带上对应的 user_info。
可用的包含则必须要调用 parseIncludes 方法:
1 2 3 4 5 6 7 8 9 |
<?php use League\Fractal; $fractal = new Fractal\Manager(); if (isset($_GET['include'])) { $fractal->parseIncludes($_GET['include']); } |
否则不会生效。
想获得去掉 5 元限制的证券账户吗?

如果您想去掉最低交易佣金 5 元限制,使用微信扫描左边小程序二维码,访问微信小程序「优财助手」,点击底部菜单「福利」,阅读文章「通过优财开证券账户无最低交易佣金 5 元限制」,按照文章步骤操作即可获得免 5 元证券账户,股票基金交易手续费率万 2.5。
请注意,一定要按照文章描述严格操作,如错误开户是无法获得免 5 元证券账户的。
因为使用
code
这样的语法会生成换行的样式,所以代码和强调都用了加粗。