PHP Classes

File: example/index.php

Recommend this page to a friend!
  Packages of Aleksandar Zivanovic   PHP Decorator Pattern   example/index.php   Download  
File: example/index.php
Role: Example script
Content type: text/plain
Description: Example script
Class: PHP Decorator Pattern
Implement the decorator pattern using a trait
Author: By
Last change: Decorating with class now expects object instead of class name, and accepts non static methods
Date: 7 years ago
Size: 928 bytes
 

 

Contents

Class file image Download
<?php
/**
 * Author: Aleksandar Zivanovic
 */

use App\ClassDecorators\CRUDDecorator;
use App\ClassDecorators\EntityHydrator;
use App\Entities\CommentEntity;
use App\Entities\PostEntity;
use PHPDecorator\Decorator;

require_once __DIR__ . '/../vendor/autoload.php';

/** @var PostEntity $post */
$post = Decorator::decorateWithClasses(new PostEntity(), [
    new CRUDDecorator(), new EntityHydrator('post')
])->hydrate(['id' => 1, 'title' => 'My first post!', 'content' => 'Hello, World!']);

echo "Title: {$post->getTitle()}\n";
echo "Content: {$post->getContent()}\n";

/** @var CommentEntity $comment */
$comment = Decorator::decorateWithClasses(new CommentEntity(), [
    new CRUDDecorator(), new EntityHydrator('comment')
]);

$comment->hydrate([
    'postId' => $post->getId(),
    'comment' => 'and here we have my first comment :)'
]);

echo "Post: {$comment->getPostId()}\n";
echo "Comment: {$comment->getComment()}";