Skip to content

Instantly share code, notes, and snippets.

View rizqidjamaluddin's full-sized avatar

Rizqi Djamaluddin rizqidjamaluddin

  • Jakarta, Indonesia
View GitHub Profile
<?php namespace A\Parser;
use A\Parser\ParserDriver;
use Mews\Purifier\Purifier;
use Parsedown;
class ParsedownPurifierParserDriver implements ParserDriver
{
protected $trusted_settings = [
@rizqidjamaluddin
rizqidjamaluddin / ArticleRepository.php
Last active December 21, 2015 13:12
Generic Eloquent repository example
<?php
class ArticleRepository {
/**
* Because of how Eloquent works, an instance of a model is also a starting point to
* execute queries on it. This class should be instantiated by Laravel's IoC (e.g.
* constructor injection on a higher class, or App::make) so you never actually should
* need to do "new ArticleRepository(new Article)".
*
<?php
/**
* @test
*/
public function users_can_post_links_in_their_profile()
{
$mario = $this->registerAndLoginAsMario();
$set = $this->callJson('PUT', "/api/users/{$mario->hash}/profile", ['bio' => "A URL: http://www.google.com"]);
$fetch = $this->callJson('GET', "/api/users/{$mario->hash}/profile");
<?php
// you can do this from any code, anywhere in your codebase
$service = new CakeService;
$service->makeOrder("Bob", "Chocolate");
<?php
class CakeController {
public function __construct (CakeService $cakeService) {
$this->cakeService = $cakeService;
}
public function createOrder() {
$customer = Input::get('customer');
$flavor = Input::get('flavor');
return $this->cakeService->makeOrder($customer, $flavor);
<?php
class CakeService {
public function makeOrder($customer, $flavor) {
$cake = new Cake();
$cake->isFor($customer);
$cake->addFlavor($flavor);
$cake->bake();
$cake->save();
return $cake;
<?php
class CakeController {
public function createOrder() {
$cake = new Cake();
$cake->isFor(Input::get('customer'));
$cake->addFlavor(Input::get('flavor'));
$cake->bake();
$cake->save();
return $cake;
<?php
class NewsAuthorizer {
public function canPublish(Actor $user, NewsPost $post) {
// using different classes to the Actor interface?
return $user instanceof Editor;
// got more complex rules, like the user can only post news within their area of expertise?
<?php
class BlogPostCommentAuthorizer {
public function can ($user, $blogPost, $comment) {
$judge = new Judge;
// blog post owner is obviously allowed to comment for sure
$policy = new EqualityPolicy();
$judge->consider($policy->enforce($user->id, $blogPost->getOwner()->id));
// ACL option. the list would generally be put in a separate file/config
@rizqidjamaluddin
rizqidjamaluddin / BlogCommentAuthorizer.php
Last active August 29, 2015 14:07
New Gatekeeper package setup, now with bonus policies and per-model authorizer classes
<?php
class BlogCommentAuthorizer extends GatekeeperAuthorizer {
public function register () {
// if you own it, you can read and edit it
$this->enforce([new OwnershipPolicy, new ActionPolicy(['read', 'edit'])]);
// members can read, admins can do anything
$this->enforce(new AclPolicy($this->getAcl()));