szurubooru/src/Dao/EntityConverter.php

35 lines
635 B
PHP
Raw Normal View History

2014-08-30 17:10:45 +02:00
<?php
namespace Szurubooru\Dao;
final class EntityConverter
{
protected $entityName;
public function __construct($entityName)
{
$this->entityName = $entityName;
}
public function toArray($entity)
{
$arrayEntity = (array) $entity;
if (isset($entity->id))
{
$arrayEntity['_id'] = $arrayEntity['id'];
unset($arrayEntity['id']);
}
return $arrayEntity;
}
public function toEntity($arrayEntity)
{
$entity = \Szurubooru\Helpers\TypeHelper::arrayToClass($arrayEntity, $this->entityName);
if (isset($entity->_id))
{
$entity->id = (string) $entity->_id;
unset($entity->_id);
}
return $entity;
}
}