Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zero-to-prod/data-model-helper
Helpers for a DataModel.
https://github.com/zero-to-prod/data-model-helper
dto helper helpers php
Last synced: 3 months ago
JSON representation
Helpers for a DataModel.
- Host: GitHub
- URL: https://github.com/zero-to-prod/data-model-helper
- Owner: zero-to-prod
- License: mit
- Created: 2024-10-14T12:38:53.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2024-10-17T02:22:25.000Z (3 months ago)
- Last Synced: 2024-10-17T02:25:46.111Z (3 months ago)
- Topics: dto, helper, helpers, php
- Language: PHP
- Homepage: https://zero-to-prod.github.io/data-model-helper/
- Size: 28.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
- Code of conduct: CODE_OF_CONDUCT.md
- Security: SECURITY.md
Awesome Lists containing this project
README
# `Zerotoprod\DataModelHelper`
[![Repo](https://img.shields.io/badge/github-gray?logo=github)](https://github.com/zero-to-prod/data-model-helper)
[![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/zero-to-prod/data-model-helper/test.yml?label=tests)](https://github.com/zero-to-prod/data-model-helper/actions)
[![Packagist Downloads](https://img.shields.io/packagist/dt/zero-to-prod/data-model-helper?color=blue)](https://packagist.org/packages/zero-to-prod/data-model-helper/stats)
[![Packagist Version](https://img.shields.io/packagist/v/zero-to-prod/data-model-helper?color=f28d1a)](https://packagist.org/packages/zero-to-prod/data-model-helper)
[![GitHub repo size](https://img.shields.io/github/repo-size/zero-to-prod/data-model-helper)](https://github.com/zero-to-prod/data-model-helper)
[![License](https://img.shields.io/packagist/l/zero-to-prod/data-model-helper?color=red)](https://github.com/zero-to-prod/data-model-helper/blob/main/LICENSE.md)Utilities for casting values using the [DataModel](https://github.com/zero-to-prod/data-model) package.
## Installation
Install the package via Composer:
```bash
composer require zero-to-prod/data-model-helper
```### Additional Packages
- [DataModel](https://github.com/zero-to-prod/data-model): Transform data into a class.
- [DataModelFactory](https://github.com/zero-to-prod/data-model-factory): A factory helper to set the value of your `DataModel`.
- [Transformable](https://github.com/zero-to-prod/transformable): Transform a `DataModel` into different types.## Quick Start
Here’s how to use the `mapOf` helper with all its arguments:
```php
class User
{
use \Zerotoprod\DataModel\DataModel;
use \Zerotoprod\DataModelHelper\DataModelHelper;
/** @var Collection $Aliases */
#[Describe([
'cast' => [self::class, 'mapOf'], // Casting method to use
'type' => Alias::class, // Target type for each item
'coerce' => true, // Coerce single elements into an array
'using' => [self::class, 'map'], // Custom mapping function
'map_via' => 'mapper', // Custom mapping method (defaults to 'map')
])]
public Collection $Aliases;
}
```## Usage
### Including the Trait
Include the `DataModelHelper` trait in your class to access helper methods:
```php
class DataModelHelper
{
use \Zerotoprod\DataModelHelper\DataModelHelper;
}
```### mapOf()
The `mapOf()` method returns an array of `Alias` instances.
```php
class User
{
use \Zerotoprod\DataModel\DataModel;
use \Zerotoprod\DataModelHelper\DataModelHelper;
/** @var Alias[] $Aliases */
#[Describe([
'cast' => [self::class, 'mapOf'], // Use the mapOf helper method
'type' => Alias::class, // Target type for each item
])]
public array $Aliases;
}class Alias
{
use \Zerotoprod\DataModel\DataModel;
public string $name;
}$User = User::from([
'Aliases' => [
['name' => 'John Doe'],
['name' => 'John Smith'],
]
]);echo $User->Aliases[0]->name; // Outputs: John Doe
echo $User->Aliases[1]->name; // Outputs: John Smith
```#### Laravel Collection Example
The `mapOf` helper is designed to work will with the `\Illuminate\Support\Collection` class.
```php
class User
{
use \Zerotoprod\DataModel\DataModel;
use \Zerotoprod\DataModelHelper\DataModelHelper;
/** @var Collection $Aliases */
#[Describe([
'cast' => [self::class, 'mapOf'],
'type' => Alias::class,
])]
public \Illuminate\Support\Collection $Aliases;
}class Alias
{
use \Zerotoprod\DataModel\DataModel;
public string $name;
}$User = User::from([
'Aliases' => [
['name' => 'John Doe'],
['name' => 'John Smith'],
]
]);echo $User->Aliases->first()->name; // Outputs: John Doe
```#### Coercing
Sometimes, an attribute may contain either a single element or an array of elements. By setting `'coerce' => true`, you can ensure that single
elements
are coerced into an array.```php
class User
{
use \Zerotoprod\DataModel\DataModel;
use \Zerotoprod\DataModelHelper\DataModelHelper;
/** @var Alias[] $Aliases */
#[Describe([
'cast' => [self::class, 'mapOf'],
'type' => Alias::class,
'coerce' => true, // Coerce single elements into an array
])]
public array $Aliases;
}class Alias
{
use \Zerotoprod\DataModel\DataModel;
public string $name;
}$User = User::from([
'Aliases' => ['name' => 'John Doe'], // Single element instead of an array
]);echo $User->Aliases[0]->name; // Outputs: John Doe
```#### Using a Custom Mapping Function
Specify your mapping function by setting the `using` option.
```php
class User
{
use \Zerotoprod\DataModel\DataModel;
use \Zerotoprod\DataModelHelper\DataModelHelper;
/** @var Collection $Aliases */
#[Describe([
'cast' => [self::class, 'mapOf'],
'type' => Alias::class,
'using' => [self::class, 'map'], // Use custom mapping function
])]
public Collection $Aliases;public static function map(array $values): Collection
{
// Map each value to an Alias instance
$items = array_map(fn($value) => Alias::from($value), $values);// Return as a Collection
return new Collection($items);
}
}class Alias
{
use \Zerotoprod\DataModel\DataModel;
public string $name;
}class Collection
{
public array $items;public function __construct(array $items = [])
{
$this->items = $items;
}
}$User = User::from([
'Aliases' => [
['name' => 'John Doe'],
],
]);echo $User->Aliases->items[0]->name; // Outputs: John Doe
```#### Specifying a Custom Mapping Method
By default, the map method is used to map over elements. You can specify a different method using the `map_via` option.
```php
class User
{
use \Zerotoprod\DataModel\DataModel;
use \Zerotoprod\DataModelHelper\DataModelHelper;
/** @var Collection $Aliases */
#[Describe([
'cast' => [self::class, 'mapOf'],
'type' => Alias::class,
'map_via' => 'mapper', // Use custom mapping method for the `Collection` class.
])]
public Collection $Aliases;
}class Alias
{
use \Zerotoprod\DataModel\DataModel;public string $name;
}class Collection
{
public array $items;public function __construct(array $values)
{
$this->items = $values;
}public function mapper(callable $callable): Collection
{
$this->items = array_map($callable, $this->items);
return $this;
}
}$User = User::from([
'Aliases' => [
['name' => 'John Doe'],
],
]);echo $User->Aliases->items[0]->name; // Outputs: John Doe
```