venne/data-transfer

Data Transfer Object

dev-master / 1.0.x-dev 2014-11-15 08:52 UTC

This package is auto-updated.

Last update: 2024-11-29 04:57:34 UTC


README

Use data transfer object pattern in templates.

Benefits:

  • Simple read-only objects in templates.
  • Cacheable objects and collections with all features from Nette/Caching.
  • Serializable objects nad collections.
  • It works right out of the box with Kdyby/Doctrine.

Installation

The best way to install Venne/DataTransfer is using Composer:

composer require venne/data-transfer:@dev

Activation

extensions:
	dataTransfer: Venne\DataTransfer\DI\DataTransferExtension

Configuration

dataTransfer:
	driver: Venne\Bridges\Kdyby\Doctrine\DataTransfer\EntityDriver
	cache:
		namespace: dataTransfer

Usage

Define DTO

/**
 * @property-read integer $id
 * @property-read string $name
 */
class ArticleDTO extends \Venne\DataTransfer\DataTransferObject {}

Basic use

$article = new ArticleDTO(array(
	'id' => 1,
	'name' => 'fooName',
));

// Lazy mode
$article = new ArticleDTO(function () {
	return array(
		'id' => 1,
		'name' => 'fooName',
	);
});

$article = unserialize(serialize($article)); // it works

echo $article->id;   // 1
echo $article->name; // fooName
echo $article->foo; // throw exception

Iterator

$articles = DataTransferObjectIterator(ArticleDTO::class, array(
	array(
		'id' => 1,
		'name' => 'fooName',
	),
	array(
		'id' => 2,
		'name' => 'barName',
	),
));

// Lazy mode
$articles = DataTransferObjectIterator(ArticleDTO::class, function () {
	return array(
				array(
					'id' => 1,
					'name' => 'fooName',
				),
				array(
					'id' => 2,
					'name' => 'barName',
				),
	);
});

$articles = unserialize(serialize($articles)); // it works

echo count($articles);

foreach ($articles as $article) {
	echo $article->id;
	echo $article->name;
}

DataTransferManager

$dataTransferManager = $container->getByType('Venne\DataTransfer\DataTransferManager');
// $dataTransferManager = new DataTransferManager($driver, $cacheStorage);
$article = $dataTransferManager
	->createQuery(ArticleDTO::class, function () {
		return $this->articleRepository->find($this->id);
	})
	->enableCache($key, $dependencies)
	->fetch();
$articles = $dataTransferManager
	->createQuery(ArticleDTO::class, function () {
		return $this->articleRepository->findBy(array(
			'parent' => $this->id,
		));
	})
	->enableCache($key, $dependencies)
	->fetchAll();

Cooperation with kdyby/doctrine

Installation

extensions:
	...
	kdybyDataTransfer: Venne\Bridges\Kdyby\Doctrine\DataTransfer\DI\DataTransferExtension

Usage

$this->template->article = $dataTransferManager
	->createQuery(ArticleDTO::class, function () {
		return $this->articleRepository->find($this->id);
	})
	->enableCache($key, $dependencies)
	->fetch();