PHP code example of awoyotoyin / zfe-base

1. Go to this page and download the library: Download awoyotoyin/zfe-base library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

awoyotoyin / zfe-base example snippets

bash
> 
> use Zfe\Common\ConfigProvider as CommonConfigProvider;
> 
> $aggregator = new ConfigAggregator([
>     ...
>     CommonConfigProvider::class,
>     ...
> ], $cacheConfig['config_cache_path']);
> 
bash


namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Zfe\Common\Entity\AbstractEntity;

/**
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks
 * @ORM\Table(name="blog_post")
 */
class Post extends AbstractEntity
{
    /**
     * @ORM\Column(name="title", type="string", length=32)
     * @var string
     */
    private $title;

    /**
     * Get the value of title
     *
     * @return  string
     */
    public function getTitle(): string
    {
        return $this->title;
    }

    /**
     * Set the value of title
     *
     * @param  string  $title
     * @return self
     */
    public function setTitle(string $title): self
    {
        $this->title = $title;
        return $this;
    }
}

Example:
$data = [
    'title' => 'Some Title'
];

$post = new Post();
$post->exchangeArray($data);
$post->getTitle(); // Some Title
bash
public function fetchAll(): \Doctrine\ORM\QueryBuilder;

public function selectAll(array $filters = [], array $orderBy = [], array $groupBy = []): \Doctrine\ORM\QueryBuilder;

public function selectAllPaginate(
        $first = 0,
        $max = 20,
        array $filters = [],
        array $orderBy = [],
        array $groupBy = [],
        $fetchJoinCollection = true
    ): Doctrine\ORM\Tools\Pagination\Paginator;

public function selectJoin(
        $first = 0,
        $max = 20,
        array $filters = [],
        array $joins = [],
        array $orderBy = [],
        array $groupBy = []
    ): \Doctrine\ORM\QueryBuilder
bash


/**
 * Description of PostProvider
 *
 * @author: Awoyo Oluwatoyin Stephen alias awoyotoyin <[email protected]>
 */

namespace App\Provider;

use Zfe\Common\Provider\AbstractProvider;

class PostProvider extends AbstractProvider
{
    protected $entityClass = 'App\Entity\Post';

    protected $entity_event_prefix = 'blog_post';
}
bash


/**
 * Description of PostService
 *
 * @author: Awoyo Oluwatoyin Stephen alias awoyotoyin <[email protected]>
 */
namespace App\Service;

use Zfe\Common\Service\AbstractService;

class PostService extends AbstractService
{

}
bash

/**
 * Description of PostObserver
 *
 * @author: Awoyo Oluwatoyin Stephen alias awoyotoyin <[email protected]>
 */
namespace App\Observer;

use Interop\Container\ContainerInterface;
use Zend\EventManager\Event;
use Zend\Log\Logger;
use Zend\Log\Processor\PsrPlaceholder;
use Zend\Log\Writer;

class PostObserver
{
    public function __invoke(ContainerInterface $container)
    {
        // Grab some dependencies from the $container
        // And return self
        return new self();
    }

    public function onPostBeforeSaveHandled(Event $event)
    {
        // Do something with the $event here
        $name = $event->getName();
        $target = get_class($event->getTarget());
        $entity = $event->getParam('entity');

        /** Modify the Entity */
        if ($entity instanceof \Zfe\Common\Entity\EntityInterface) {
            $entity->setTitle('Title Changed');
        } elseif (is_array($entity)) {
            $entity['title'] = 'Title Changed';
        }

        /** Push changes back to the trigger */
        $event->setParam('entity', $entity);

        $logger = new Logger;
        $logger->addProcessor(new PsrPlaceholder);

        $writer = new Writer\Stream('data/log/events.log');
        $logger->addWriter($writer);

        $logger->notice('{event} was called on {target} with entity {entity}', [
            'event' => $event,
            'target' => $target,
            'entity' => json_encode($entity)
        ]);
    }
}