{"id":36972168,"url":"https://github.com/jetcod/data-transport","last_synced_at":"2026-01-13T21:55:18.391Z","repository":{"id":81192420,"uuid":"605440013","full_name":"jetcod/data-transport","owner":"jetcod","description":"Efficient PHP Data Transport with DTOs","archived":false,"fork":false,"pushed_at":"2025-10-10T23:50:10.000Z","size":1052,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-11-18T07:04:52.755Z","etag":null,"topics":["data-structures","data-transfer-object","dto","object-mapping"],"latest_commit_sha":null,"homepage":"https://jetcod.github.io/data-transport/","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jetcod.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-02-23T06:43:03.000Z","updated_at":"2025-10-10T23:50:00.000Z","dependencies_parsed_at":"2024-10-24T20:32:53.497Z","dependency_job_id":"ab8a5ebb-9241-44b5-a82f-42d448e71d1e","html_url":"https://github.com/jetcod/data-transport","commit_stats":{"total_commits":12,"total_committers":1,"mean_commits":12.0,"dds":0.0,"last_synced_commit":"07d2586b7b66ca6e8d776c8e8c16afa26403b712"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/jetcod/data-transport","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jetcod%2Fdata-transport","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jetcod%2Fdata-transport/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jetcod%2Fdata-transport/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jetcod%2Fdata-transport/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jetcod","download_url":"https://codeload.github.com/jetcod/data-transport/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jetcod%2Fdata-transport/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28401953,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-13T14:36:09.778Z","status":"ssl_error","status_checked_at":"2026-01-13T14:35:19.697Z","response_time":56,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["data-structures","data-transfer-object","dto","object-mapping"],"created_at":"2026-01-13T21:55:18.316Z","updated_at":"2026-01-13T21:55:18.384Z","avatar_url":"https://github.com/jetcod.png","language":"PHP","readme":"# Data Transfer Object (DTO)\n\n[![Actions Status](https://github.com/jetcod/data-transport/actions/workflows/php.yml/badge.svg?style=for-the-badge\u0026label=%3Cb%3EBuild%3C/b%3E)](https://github.com/jetcod/data-transport/actions)\n\n\n[![Latest Stable Version](https://img.shields.io/packagist/v/jetcod/data-transport?label=Latest%20Stable%20Version)](https://packagist.org/packages/jetcod/data-transport)\n[![Total Downloads](https://img.shields.io/packagist/dt/jetcod/data-transport?label=Total%20Downloads)](https://packagist.org/packages/jetcod/data-transport)\n[![License](https://img.shields.io/github/license/jetcod/data-transport?label=License)](https://github.com/jetcod/eloquent-repository/blob/main/LICENSE)\n\n\n## Overview\n\n**Data Transport** is a PHP package that provides a simple and efficient way to transport data within your application. With Data Transport, you can easily define and manage your data structures, ensuring that your application's data is well-organized and easy to work with. It supports read-only objects, validates attributes upon assignment, and allows integration of custom validators through a schema definition.\n\nCheck out our [documentation](https://jetcod.github.io/data-transport) for more details.\n\n## Installation\n\nTo install `jetcod/data-transport`, you can use Composer, the dependency manager for PHP. Run the following command in your terminal:\n\n```sh\ncomposer require jetcod/data-transport\n```\n\n## Usage\n\nIt is straightforward to employ `jetcod/data-transport`. To begin, create a customized data object class that extends `Jetcod\\DataTransport\\AbstractDTO`.\n\n```php\n\u003c?php \n\nnamespace App\\DTO;\n\nuse Jetcod\\DataTransport\\AbstractDTO;\n\nclass Student extends AbstractDTO\n{\n}\n```\n\nNext, construct your class and inject your array of data into it.\n\n```php\n\u003c?php \n\n$data = [\n    'name' =\u003e 'John Doe',\n    'email' =\u003e 'john.doe@example.com',\n];\n\n$dto = new \\App\\DTO\\Student($data);\n```\n\nAlternatively, you can assign values to individual class attributes:\n\n```php\n\u003c?php \n\n$dto = new \\App\\DTO\\Student();\n\n$dto-\u003ename = \"John Doe\";\n$dto-\u003eemail = 'john.doe@example.com';\n```\n\nWhen assigning attributes dynamically, the values are validated against the defined schema if your DTO implements `TypedEntity`.\n\nIn addition to the traditional method of creating a class, you can also utilize the convenient make function to create instances of the class. The make function simplifies the process and provides an alternative way to initialize objects.\n\n```php\n\u003c?php \n\n$dto = \\App\\DTO\\Student::make($data);\n```\n\nBy utilizing the data transfer object, one can prevent the occurrence of exceptions when attempting to access an undefined class attribute. In such cases, the data transfer object will consistently return `null`.\n\n```php\n$dto = new \\App\\DTO\\Student();\n\nvar_dump($dto-\u003ename);   // Returns null\n```\nAdditionally, it offers the opportunity to specify custom data types.\n\n## Read-Only DTO\nDTOs can be constructed as read-only using the second argument of the constructor:\n\n```php\n$dto = new Student($data, readOnly: true);\n```\n\nOr later using:\n\n```php\n$dto = new Student($data);\n$dto-\u003ereadOnly();\n```\nAttempting to assign new values to a read-only object will throw an exception.\n\n## Custom Validator Support\n\nIf your DTO class implements `TypedEntity` and defines a `getSchema()` method, attributes will be validated using custom validators defined in your application. Each field in the schema should map to a validator alias or class.\n\n```php\nuse Jetcod\\DataTransport\\Contracts\\TypedEntity;\n\nclass Student extends AbstractDTO implements TypedEntity\n{\n    protected function getSchema(): array\n    {\n        return [\n            'email' =\u003e 'email',\n            'age'   =\u003e 'numeric',\n        ];\n    }\n}\n```\n\n## Supported Validator Aliases\n\nOut of the box, the following validator aliases are supported:\n\n| Alias | Description |\n|-------|-------------|\n| int | Validates that the value is an integer |\n| numeric | Validates that the value is a number |\n| float | Validates that the value is a float |\n| bool | Validates that the value is a boolean |\n| array | Validates that the value is an array |\n| string | Validates that the value is a string |\n| url | Validates that the value is a valid URL |\n| email | Validates that the value is a valid email address |\n\nYou can also create your own validators by implementing `Jetcod\\DataTransport\\Contracts\\ValidatorInterface`.\n\n## Initialization Hook\n\nStarting from version 1.1.2, an `init()` method can be defined within any derived class for additional initialization logic. This method, if defined, will be automatically called after the attributes are assigned in the constructor. You can use this method to perform any necessary setup or initialization tasks.\n\n```php\nclass Student extends AbstractDTO\n{\n    protected function init()\n    {\n        // Custom initialization logic\n    }\n}\n```\n\n## Functions\n\nThe following functions are available in the DTO class:\n\n### `__construct(?array $attributes = [])`\n\nThe constructor function creates a new instance of the DTO class and initializes it with the given data array.\n\n### `has(string $key): bool`\n\nThe has() function checks if the specified key exists in the DTO class data and returns a boolean value.\n\nIt works the same as `isset()` php built-in function. For example if you have object:\n\n```php\n$student = new Student([\n    'first_name' =\u003e 'John',\n    'last_name' =\u003e 'Doe',\n    'email' =\u003e 'john.doe@example.com'\n]);\n```\n\nthen both functions will produce an identical outcome:\n\n```php\nisset($student-\u003eemail);  // Returns true\nisset($student-\u003ephone);  // Returns false\n```\nor\n```php\n$student-\u003ehas('email');  // Returns true\n$student-\u003ehas('phone');  // Returns false\n```\n\n\n\n### `toArray()`\n\nThe toArray() function returns an array representation of the DTO class data.\n\n### `toJson(int $options = 0)`\n\nThe toJson() function returns a JSON representation of the DTO class data.\n\nOther than above functions, it has the following magic functions:\n\n| Function | Description |\n|----------|-------------|\n| `__set(string $key, $val)` | sets the value of the specified attribute |\n| `__get(string $key)` | returns the value of the specified attribute |\n| `__isset(string $key)` | determines if the attribute has been set |\n| `__unset(string $key)` | unsets the specified attribute |\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](./LICENSE) file for details.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjetcod%2Fdata-transport","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjetcod%2Fdata-transport","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjetcod%2Fdata-transport/lists"}