<?php
class AuthGuard
{
public static function guard()
{
return false;
}
}
class TrialGuard
{
public static function guard()
{
return true;
}
}
class Link
{
protected $guards = [];
protected $html;
public function __construct( $url, $text, $attrs = '' )
{
$attrs = $attrs == '' ? $attrs : " " . $attrs;
$this->html = "<a href=\"$url\"" . $attrs . ">$text</a>";
}
public function guard( array $guards = [])
{
$this->guards = $guards;
return $this;
}
function __toString()
{
foreach ( $this->guards as $guard )
{
if( !$guard::guard() )
{
return ''; // or throw an exception
}
}
return $this->html;
}
}
$link1 = new Link( 'https://google.com', 'Google' );
echo $link1->guard([
TrialGuard::class
]);
$link2 = new Link( 'https://yahoo.com', 'Yahoo', 'class="btn" style="color:#000000;"' );
echo $link2->guard([
TrialGuard::class
]);
$link3 = new Link( 'https://hotmail.com', 'Hotmail' );
echo $link3->guard([
TrialGuard::class, AuthGuard::class
]);
Last active
November 19, 2018 10:58
-
-
Save nezarfadle/2e205df94dc8855a66ac40118a7ba679 to your computer and use it in GitHub Desktop.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment