|
<?php |
|
|
|
class UserViewModel { |
|
private $displayName; |
|
|
|
public function __construct($displayname) { |
|
$this->displayName = $displayname; |
|
} |
|
|
|
public function getDisplayName() { |
|
return $this->displayName; |
|
} |
|
} |
|
|
|
class GetUserViewService { |
|
public static function query($id) { |
|
return new UserViewModel('Eric Banner'); |
|
} |
|
} |
|
|
|
interface ViewInterface { |
|
public function render(); |
|
} |
|
|
|
class BaseView { |
|
private $data; |
|
|
|
public function __construct() { |
|
$this->action(); |
|
$this->render($this->data); |
|
} |
|
} |
|
|
|
class UserPageView extends BaseView implements ViewInterface { |
|
public function __construct($id) { |
|
$this->data = GetUserViewService::query($id); |
|
$this->render(); |
|
} |
|
public function render() { |
|
?> |
|
<?php echo $this->data->getDisplayName(); ?> |
|
<?php |
|
new SocialIconView(); |
|
} |
|
} |
|
|
|
class SocialIconsViewModel { |
|
private $icons = [ |
|
'Facebook', |
|
'Twitter', |
|
'Instagram' |
|
]; |
|
|
|
public function __construct() { |
|
|
|
} |
|
|
|
public function getIcons() { |
|
return $this->icons; |
|
} |
|
} |
|
|
|
class SocialIconView extends BaseView implements ViewInterface { |
|
public function __contruct() { |
|
parent::__construct(); |
|
} |
|
public function action() { |
|
$this->data = new SocialIconsViewModel(); |
|
} |
|
public function render() { |
|
?> |
|
<ul> |
|
<?php foreach ($this->data->getIcons() as $data) { ?> |
|
<li><?php echo $data; ?></li> |
|
<?php } ?> |
|
</ul> |
|
<?php |
|
} |
|
} |
|
|
|
class CardViewModel { |
|
private $title; |
|
private $excerpt; |
|
|
|
public function __construct($title, $excerpt) { |
|
$this->title = $title; |
|
$this->excerpt = $excerpt; |
|
} |
|
|
|
public function getTitle() { |
|
return $this->title; |
|
} |
|
|
|
public function getExcerpt() { |
|
return $this->excerpt; |
|
} |
|
} |
|
|
|
class GetCardViewService { |
|
public static function query($id) { |
|
// Pretend to get data based on $id |
|
return new CardViewModel('Hello World','This is your first post.'); |
|
} |
|
} |
|
|
|
class CardView extends BaseView implements ViewInterface { |
|
public function __construct($post_id) { |
|
$this->data = GetCardViewService::query($post_id); |
|
$this->render(); |
|
} |
|
|
|
public function render() { |
|
?> |
|
<h2><?php echo $this->data->getTitle(); ?></h2> |
|
<p><?php echo $this->data->getExcerpt(); ?></p> |
|
<?php |
|
} |
|
} |
|
|
|
class CardLayoutViewModel { |
|
private $cards = []; |
|
|
|
public function __construct() {} |
|
|
|
public function addCard($card) { |
|
$this->cards[] = $card; |
|
} |
|
|
|
public function getCards() { |
|
return $this->cards; |
|
} |
|
} |
|
|
|
class GetCardLayoutService { |
|
public static function query($post_type) { |
|
// Pretend you're using parameters to get data |
|
$cardlayout = new CardLayoutViewModel(); |
|
$cardlayout->addCard(new CardViewModel('Hello World','This is your first post.')); |
|
$cardlayout->addCard(new CardViewModel('Super Man Is Here','This is your second post.')); |
|
return $cardlayout; |
|
} |
|
} |
|
|
|
class CardLayoutView extends BaseView implements ViewInterface { |
|
public function __construct($post_type) { |
|
$this->data = GetCardLayoutService::query($post_type); |
|
$this->render(); |
|
} |
|
|
|
public function render() { |
|
?> |
|
<table> |
|
<?php foreach ($this->data->getCards() as $card) { ?> |
|
<tr> |
|
<td><?php echo $card->getTitle(); ?></td> |
|
<td><?php echo $card->getExcerpt(); ?></td> |
|
</tr> |
|
<?php } ?> |
|
</table> |
|
|
|
<?php |
|
} |
|
} |
|
|
|
// page-user.php |
|
new UserPageView(111); |
|
new CardView(222); |
|
new CardLayoutView('posts'); |
|
|
|
?> |