PHPackages                             petrenkoanton/php-dto - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. petrenkoanton/php-dto

ActiveLibrary

petrenkoanton/php-dto
=====================

Custom dto implementation

v2.0.0(2y ago)1158MITPHPPHP ^8.1

Since Jan 22Pushed 2y ago1 watchersCompare

[ Source](https://github.com/PetrenkoAnton/php-dto)[ Packagist](https://packagist.org/packages/petrenkoanton/php-dto)[ RSS](/packages/petrenkoanton-php-dto/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (6)Dependencies (8)Versions (8)Used By (0)

php-dto
=======

[](#php-dto)

[![PHP Version](https://camo.githubusercontent.com/d22e32680cd029e0e2f18fedf9384b0801ea345ec9564ae4ddae39574df82570/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f70657472656e6b6f616e746f6e2f7068702d64746f)](https://packagist.org/packages/petrenkoanton/php-dto)[![Latest Version on Packagist](https://camo.githubusercontent.com/2558d6e0f1896a302dd5d3515a5259a3e34461e17461614acdb89e65d500c9fd/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f70657472656e6b6f616e746f6e2f7068702d64746f2e737667)](https://packagist.org/packages/petrenkoanton/php-dto)[![Total Downloads](https://camo.githubusercontent.com/2cdb5dda673a2f5d7b29b086ff4d99f2021b1c357de8f59186dd3ba7cab0f363/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f70657472656e6b6f616e746f6e2f7068702d64746f2e737667)](https://packagist.org/packages/petrenkoanton/php-dto)[![License](https://camo.githubusercontent.com/80afcc752946b0e81ffcba8e324507288a405f47e528012b86a9409d983d411b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f70657472656e6b6f616e746f6e2f7068702d64746f)](https://packagist.org/packages/petrenkoanton/php-dto)

[![PHP Composer](https://github.com/petrenkoanton/php-dto/actions/workflows/tests.yml/badge.svg)](https://github.com/petrenkoanton/php-dto/actions/workflows/tests.yml)[![Coverage Status](https://camo.githubusercontent.com/30e00eafeea1380efa3439018766a9397abaa105f19f841a457eb52a45d2a1fa/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f50657472656e6b6f416e746f6e2f7068702d64746f2f62616467652e7376673f6272616e63683d6d61696e)](https://coveralls.io/github/PetrenkoAnton/php-dto?branch=main)[![type-coverage](https://camo.githubusercontent.com/0a20db83d665a15083f8ab47b2fd0c5f24c5417652a95407c7dd06b6977ca214/68747470733a2f2f73686570686572642e6465762f6769746875622f70657472656e6b6f616e746f6e2f7068702d64746f2f636f7665726167652e737667)](https://shepherd.dev/github/petrenkoanton/php-dto)[![psalm-level](https://camo.githubusercontent.com/db0d3bf1977d0a471892c0c37031b1e8db19ef747667839af8a615fe26f7121f/68747470733a2f2f73686570686572642e6465762f6769746875622f70657472656e6b6f616e746f6e2f7068702d64746f2f6c6576656c2e737667)](https://shepherd.dev/github/petrenkoanton/php-dto)[![Build Status](https://github.com/petrenkoanton/php-dto/workflows/coding-style/badge.svg)](https://github.com/petrenkoanton/php-dto/actions)

[Installation](#installation) | [Functionality](#functionality) | [Usage](#usage) | [For developers](#for-developers)| [License](#license) | [Related projects](#related-projects)

Installation
------------

[](#installation)

### Requirements

[](#requirements)

- php 8.1 or higher

### Composer

[](#composer)

```
composer require petrenkoanton/php-dto
```

Functionality
-------------

[](#functionality)

### Public methods

[](#public-methods)

#### [Dto](./src/Dto.php)

[](#dto)

All getters are provided using the `__call()` magic method.

MethodException\_\_construct(array $data)DtoException\_\_call(string $name, array $arguments): mixedDtoExceptiontoArray(): array-#### [DtoCollection](./src/DtoCollection.php)

[](#dtocollection)

MethodException\_\_construct(Dto ...$items)DtoExceptionadd(Collectable $item): voidDtoException#### Parent [Collection](https://github.com/PetrenkoAnton/php-collection)

[](#parent-collection)

[github.com/PetrenkoAnton/php-collection](https://github.com/PetrenkoAnton/php-collection)

MethodExceptionfilter(callable $callback): Collection-getItems(): array-getItem(int $key): CollectableCollectionExceptionfirst(): CollectableCollectionExceptioncount(): int-### Exceptions

[](#exceptions)

Main library exception is [DtoException](./src/Exception/DtoException.php).

There are [3 groups of exceptions](./src/Exception/DtoException): InitDtoException, SetupDtoException and HandleDtoException

CodeMessage patternExceptionGroup101Dto: %s | Property: %s | Err: Missed property type declarationNoTypeDeclarationExceptionInitDtoException102Dto: %s | Property: %s | Err: Unsupported mixed property type declarationMixedDeclarationExceptionInitDtoException103Dto: %s | Property: %s | Err: Unsupported object property type declarationObjectDeclarationExceptionInitDtoException104Dto: %s | Property: %s | Err: Class must implement DtoInterfaceNotDtoClassDeclarationExceptionInitDtoException105Dto: %s | Property: %s | Err: No backing value for enumEnumNoBackingValueExceptionInitDtoException106DtoCollection: %s | Err: Invalid constructor declarationDtoCollectionConstructorExceptionInitDtoException201DtoCollection: %s | Expected Dto: %s | Given Dto: %sAddDtoExceptionSetupDtoException202Dto: %s | Property: %s | Err: No dataInputDataExceptionSetupDtoException203Dto: %s | Property: %s | Expected type: %s | Given type: %s | Value: %sSetValueExceptionSetupDtoException204Dto: %s | Property: %s | Enum: %s | Expected values: %s | Given type: %s | Value: %sSetValueEnumExceptionSetupDtoException301Dto: %s | %sGetValueExceptionHandleDtoExceptionUsage
-----

[](#usage)

### Initialization

[](#initialization)

- Your dto class must extends `Dto\Dto` abstract class.
- You need to declare available `protected` properties.

**Important!** Getter will be with the prefix `is*` if property is a `bool` type.

#### Simple DTO

[](#simple-dto)

```
