Created
February 1, 2015 02:05
-
-
Save bcho/da1fc4908f4d2e6f3980 to your computer and use it in GitHub Desktop.
PostPresenter.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php namespace Vtmer\Blog\Presenter; | |
| use ArrayAccess; | |
| use JsonSerializable; | |
| use Illuminate\Support\Contracts\JsonableInterface; | |
| use Illuminate\Support\Contracts\ArrayableInterface; | |
| use Vtmer\Blog\Model\PostModel; | |
| /** | |
| * Post Presenter | |
| * | |
| * 可以用在模板渲染中或者直接用作响应输出: | |
| * | |
| * $p = (new PostPresenter())->withPost($post); | |
| * | |
| * 1. 模板渲染: | |
| * | |
| * $p->renderPartialView(); // 调用某一个方法 | |
| * {{{ $p['title'] }}} // 获取某个属性 | |
| * | |
| * 2. 用作响应输出 | |
| * | |
| * return $p; // 输出 JSON 响应 | |
| */ | |
| class PostPresenter implements | |
| ArrayAccess, | |
| ArrayableInterface, | |
| JsonSerializable, | |
| JsonableInterface | |
| { | |
| /** | |
| * 模型资源实例 | |
| * | |
| * @var \Vtmer\Blog\Model\PostModel | |
| */ | |
| protected $resource; | |
| /** | |
| * 获取资源实例 | |
| * | |
| * @return \Vtmer\Blog\Model\PostModel | |
| */ | |
| public function getResource() | |
| { | |
| return $this->resource; | |
| } | |
| /** | |
| * 按下标来获取属性 | |
| * | |
| * 通过实现该方法,可以直接使用如下形式来访问资源属性: | |
| * | |
| * $p = (new PostPresenter())->withPost($post); | |
| * echo $p['title']; | |
| * | |
| * @param string $offset | |
| * @return mixed | |
| */ | |
| public function offsetGet($offset) | |
| { | |
| return $this->resource->offsetGet($offset); | |
| } | |
| /** | |
| * 按下标来设置属性 | |
| * | |
| * @param string $offset | |
| * @return mixed | |
| */ | |
| public function offsetSet($offset, $value) | |
| { | |
| return $this->resource->offsetGet($offset, $value); | |
| } | |
| /** | |
| * 删除下标属性 | |
| * | |
| * @param string $offset | |
| * @return mixed | |
| */ | |
| public function offsetUnset($offset) | |
| { | |
| return $this->resource->offsetUnset($offset); | |
| } | |
| /** | |
| * 对应下标属性是否存在? | |
| * | |
| * @param string $offset | |
| * @return bool | |
| */ | |
| public function offsetExists($offset) | |
| { | |
| return $this->resource->offsetExists($offset); | |
| } | |
| /** | |
| * 将不存在于 presenter 上的方法调用转发到资源上 | |
| * | |
| * 通过实现该方法,可以直接调用资源的方法: | |
| * | |
| * $p = (new PostPresenter())->withPost($post); | |
| * $p->DoSomethingOnlyPostModelCanDo(); | |
| * | |
| * @param string $name | |
| * @param array $arguments | |
| * @return mixed | |
| */ | |
| public function __call($name, $arguments) | |
| { | |
| return call_user_func_array([$this->resource, $name], $arguments); | |
| } | |
| /** | |
| * 将资源转换为数组输出 | |
| * | |
| * 用作序列化成 JSON 格式 | |
| * | |
| * @return array | |
| */ | |
| public function toArray() | |
| { | |
| $rv = $this->resource->toArray(); | |
| // 按需要添加字段到输出 | |
| $rv['some_other_cool_field'] = 'cool'; | |
| return $rv; | |
| } | |
| /** | |
| * 序列化成 JSON 格式 | |
| * | |
| * @param mixed $options | |
| * @return string | |
| */ | |
| public function toJson($options = 0) | |
| { | |
| return json_encode($this->toArray(), $options); | |
| } | |
| /** | |
| * 转换成可以被 JSON 序列化的形式 | |
| * | |
| * @return array | |
| */ | |
| public function jsonSerialize() | |
| { | |
| return $this->toArray(); | |
| } | |
| /** | |
| * 指定一个模型实例 | |
| * | |
| * @param \Vtmer\Blog\Model\PostModel $post | |
| * @return $this | |
| */ | |
| public function withPost(PostModel $post) | |
| { | |
| $this->resource = $post; | |
| return $this; | |
| } | |
| /** | |
| * 获取文章阅读量 | |
| * | |
| * @return int | |
| */ | |
| public function getPageViews() | |
| { | |
| return $this->model->page_views; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment