Zend Expressive Hal and Doctrine Entity - Need some help to Expose Doctrine Entity

zend-expressive-hal

#1

Hi to all i have some problem with doctrine and JSON HAL
ConfigProvider

<?php

declare(strict_types=1);

namespace App;

use Zend\Expressive\Hal\Metadata\MetadataMap;
use Zend\Expressive\Hal\Metadata\RouteBasedCollectionMetadata;
use Zend\Expressive\Hal\Metadata\RouteBasedResourceMetadata;
use Zend\Hydrator\ObjectProperty as ObjectPropertyHydrator;

/**
* The configuration provider for the Babook module
*
* @see https://docs.zendframework.com/zend-component-installer/
*/
class ConfigProvider
{
 /**
  * Returns the configuration array
  *
  * To add a bit of a structure, each section is defined in a separate
  * method which returns an array with its configuration.
  *
  */
 public function __invoke() : array
 {
     return [
         'dependencies' => $this->getDependencies(),
         MetadataMap::class => $this->getHalConfig(),
     ];
 }

 /**
  * Returns the container dependencies
  */
 public function getDependencies() : array
 {
     return [
         'invokables' => [

         ],
         'factories'  => [
             User\UsersCollectionHandler::class => User\UsersCollectionHandlerFactory::class,
             User\CreateUserHandler::class => User\CreateUserHandlerFactory::class,
         ],
     ];
 }

 public function getHalConfig() : array
 {
     return [
         [
             '__class__' => RouteBasedResourceMetadata::class,
             'resource_class' => User\UserEntity::class,
             'entity_identifier_name' => 'id',
             'route' => 'api.users',
             'extractor' => ObjectPropertyHydrator::class,
         ],
         [
             '__class__' => RouteBasedResourceMetadata::class,
             'resource_class' => Entity\User::class,
             'entity_identifier_name' => 'id',
             'route' => 'api.users',
             'extractor' => ObjectPropertyHydrator::class,
         ],
         [
             '__class__' => RouteBasedCollectionMetadata::class,
             'collection_class' => User\UserCollection::class,
             'collection_relation' => 'users',
             'route' => 'api.users',
         ]
     ];
 }
}

UserCollection class definition

declare(strict_types=1);

namespace App\User;

use ArrayIterator;

class UserCollection extends ArrayIterator
{

}

class UserRepository

<?php

namespace App\Repository;

use App\Exception;
use App\Entity\User;
use App\User\UserCollection;
use App\User\UserEntity;
use \Doctrine\ORM\EntityRepository;
use \Doctrine\ORM\Mapping\ClassMetadata;
use JsonSerializable;

/**
 * UserRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class UserRepository extends EntityRepository
{
   /** 
    * Select all users and cast them to an UserCollection
    * Retrun  a user collection
    * @return UserCollection
    */
    public function getAll(): UserCollection
    {
        $users = new UserCollection($this->findAll());
        return $users;
    }

    /**
     * Return a single User Entity if found
     * @throws Exception\NoResourceFoundException if invalid entity id provided
     * @param integer $id
     * @return UserEntity
     */
    public function getUser(int $id): UserEntity
    {
        $result = new UserEntity($this->findBy(['id'=>$id]));

        if (null == $result) {
            throw Exception\NoResourceFoundException::create('User not found');
        }
        return $result;
    }

    /**
     * Persist to databse a full user entity (User + Profile)
     * Return UserID
     * @todo Add logic to persisti profile Entity
     * @param array $data
     * @param UserInputFilter $inputFilter class that ma
     * @return integer
     * @throws Exception\InvalidParameterException if the data is not valid.
     * @throws Exception\RuntimeException if an error occurs during insert.
     */
    public function addUser(array $data, UserInputFilter $inputFilter): int
    {
        $inputFilter->setData($data);
        if (! $inputFilter->isValid()) {
            throw Exception\InvalidParameterException::create(
                'Invalid parameter',
                $inputFilter->getMessages()
            );
        }

        $user = new User();
        $user->setEmail($data['email']);
        $user->setPassword($data['password']);
        $this->_em->persist($user);
        $this->_em->flush($user);

        $user_id = $user->getId();
        if (! is_int($user_id)) {
            throw Exception\RuntimeException::create(

                'Oops, something went wrong. Please contact the administrator'
            );
        }
        return (int) $user_id;
    }
}


How i can cast a doctrine entity to a UserCollection class without adding this to ConfigProvider

 [  '__class__' => RouteBasedResourceMetadata::class,
    'resource_class' => Entity\User::class,
    'entity_identifier_name' => 'id',
    'route' => 'api.users',
     'extractor' => ObjectPropertyHydrator::class,
],

#2

You can use markdown to make the code readable with a codeblock instead of quoting code: http://commonmark.org/help/

For php code you could use this:

    ```php
    /* Your code here */
    ```

#3

Added markdown to format code