-
Notifications
You must be signed in to change notification settings - Fork 1
ja Mastodon class
USAMI Kenta edited this page May 5, 2017
·
5 revisions
なんでもできるクラス(神)
このクラスの目的は、個別の責務に分割されたClient
, SessionStorage
とRequester
手続きを結びつけることにある。
このクラスの実装はひどく短い。実際のコードはリポジトリ内の最新版を見てほしいが、何かドラスティックな構造変更の提案がない限りは(DocComment以外は)そうそう変更されないだろう。
/**
* Mastodon Service
*
* @author USAMI Kenta <[email protected]>
* @copyright 2017 Baguette HQ
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL-3.0
* @method Entity\Account fetchAccount(int $id)
* @method Entity\Account getAccountCurrentUser()
* @method Entity\Account updateAccount(array $update_data)
* @method Entity\Account[] getAccountFollowers(int $account_id)
* @method Entity\Account[] getBlocks(array $options = [])
* @method Entity\Status[] getFavourites(array $options = [])
* @method Entity\Account postStatus(Service\Toot $toot)
*/
final class Mastodon
{
use \Teto\Object\PrivateGetter;
use \Teto\Object\ReadOnly;
/** @var Client */
private $client;
/** @var SessionStorage */
private $session;
public function __construct(Client $client, SessionStorage $session)
{
$this->client = $client;
$this->session = $session;
}
/**
* @return Entity\Entity
*/
public function __call($name, array $args)
{
return call_user_func_array(
[Requester::class, $name],
array_merge([$this->client, $this->session], $args)
);
}
}
マジックメソッド__call()
は定義すると、存在しないメソッドが呼ばれたときに呼び出される不思議なメソッド。ちょうべんり機能でRubyにおけるObject#method_missing
と概ね同じもの。ちなみにPHP用語ではメソッドのオーバーロードと呼ばれる。
そんでもって、@method
を書いてやると、PhpStormみたいなIDEはこれを見てメソッド定義に存在しないメソッドを補完候補にしてくれるわけですね。べんりー。