%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /var/www/html/bbw/farmaci/kritik-portale/module/Basis/src/Basis/Handler/
Upload File :
Create Path :
Current File : /var/www/html/bbw/farmaci/kritik-portale/module/Basis/src/Basis/Handler/BaseHandler.php

<?php
/**
 * Created by PhpStorm.
 * User: juliangilles
 * Date: 17.03.14
 * Time: 15:49
 */

namespace Basis\Handler;


use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;

abstract class BaseHandler implements HandlerInterface
{

    /**
     * @var null
     */
    protected $_databaseAdapter = null;

    /**
     * @var string
     */
    protected $_entity;

    /**
     * @var string
     */
    protected $_table;


    /**
     * @var \Zend\Db\ResultSet\ResultSet
     */
    protected $_resultset;

    /**
     * @var array
     */
    protected $_events = array();

    public function __construct()
    {

    }

    /**
     * @param string $eventName
     * @param string $callBack
     */
    protected function addEvent($eventName, $callBack)
    {
        $this->_events[$eventName] = $callBack;
    }

    /**
     * @param \Zend\Db\Adapter\Adapter $databaseAdapter
     */
    public function setDatabaseAdapter($databaseAdapter)
    {
        $this->_databaseAdapter = $databaseAdapter;
    }

    /**
     * @param string $entityType
     */
    protected function setTable($table)
    {
        $this->_table = $table;
    }

    /**
     * @param string $entityType
     */
    protected function setEntityType($entityType)
    {
        $this->_entity = $entityType;

        $class = $entityType;
        $this->_resultSet = new ResultSet(ResultSet::TYPE_ARRAYOBJECT, new $class());
    }

    /**
     * @param string $fieldName
     * @param string $value
     * @param string $objectId
     */
    protected function setValue($fieldName, $value, $objectId)
    {
        if (empty($this->_entity)) {
            throw new \Exception("BaseHandler setValue(...): No Entity Type is set. (no entity is set)");
        }

        $table = new TableGateway($this->_table, $this->_databaseAdapter, null, $this->_resultSet);
        $entity = $table->select(array("entityid" => $objectId));

        if ($entity->count() === 0) {
            $entity = new $this->_entity();
            $entity->__set("entityid", $objectId);
            $entity->__set($fieldName, $value);
            $table->insert($entity->getArrayCopy());
        }
        else
        {
            $updateEntity = $entity->current();
            $updateEntity->__set($fieldName, $value);
            $table->update($updateEntity->getArrayCopy(), array("entityid" => $objectId));
        }
    }

    /**
     * @param array $data
     * @param string $objectId
     */
    protected function setValues($data, $objectId)
    {
        $table = new TableGateway($this->_table, $this->_databaseAdapter, null, $this->_resultSet);
        $entity = $table->select(array("entityid" => $objectId));

        if (is_null($entity)) {
            $entity = new $this->_entity();
            $entity->__set("entityid", $objectId);

            foreach ($data as $key => $value) {
                $entity->__set($key, $value);
            }
            $table->insert($entity->getArrayCopy());
        }
        else
        {
            $updateEntity = $entity->current();
            foreach ($data as $key => $value) {
                $updateEntity->__set($key, $value);
            }
            $table->update($updateEntity->getArrayCopy(), array("entityid" => $objectId));
        }
    }

    /**
     * @param string $objectId
     */
    protected function delete($objectId)
    {
        $table = new TableGateway($this->_table, $this->_databaseAdapter, null, $this->_resultSet);
        $table->delete(array("entityid" => $objectId));
    }

    /**
     * @param array $events
     * @param null $object
     */
    public function handle($events)
    {
        foreach ($events as $event) {
            /* @var $event \Basis\Event\Event */
            if (array_key_exists($event->getName(), $this->_events)) {
                $callback = $this->_events[$event->getName()];
                $this->$callback($event);
            }
        }
    }
} 

Zerion Mini Shell 1.0