Casters for spatie/data-transfer-object

Overview

A package containing DTO casters

Latest Version on Packagist GitHub Tests Action Status Code style Total Downloads

Installation

You can install the package via composer:

composer require morningtrain/data-transfer-object-casters

Usage

To use the provided casters, add an attribute CastWith, to the property you want to cast.

class DTO extends DataTransferObject
{
    #[CastWith(BoolCaster::class)]
    public bool $bool;
}

Bool

The Boolean caster is used to cast the provided value to a boolean.

It uses FILTER_VALIDATE_BOOL, see the php docs for more information

Date

The DateCaster will attempt to cast the provided value into a Carbon instance, using the provided format.

If no format is specified 'd.m.Y H:i:s' will be used.

use Morningtrain\DataTransferObjectCasters\Casters\DateCaster;

class DTO extends DataTransferObject
{
    #[CastWith(DateCaster::class, format: 'Y-m-d')]
    public Carbon $date;
}

Int

The IntCaster casts the provided value to an integer using (int) cast.

Trim

The TrimCaster will trim the provided value, so no whitespace remains around the value.

UppercaseFirst

The UppercaseFirstCaster will uppercase the first letter of the provided value, similar to php's ucfirst function.

Optionally, the caster can lowercase the rest of the string, like so.

use Morningtrain\DataTransferObjectCasters\Casters\UppercaseFirstCaster;

class DTO extends DataTransferObject
{
    #[CastWith(UppercaseFirstCaster::class, lower: true)]
    public string $ucFirstString;
}

If the provided value is not a string, it will return the provided value.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

You might also like...
the examples of head first object oriented analysis & design - in PHP

Head First object oriented analysis & design in (PHP) after cloning the repository, you have to install the project's dependancies by running the foll

Supper quick use Aliyun OSS or Tencent COS or Qiniu Koa to get、put、delete Object.

An SDK integrating Alibaba cloud, Tencent cloud and qiniu cloud object storage

Allows generate class files parse from json and map json to php object, including multi-level and complex objects;

nixihz/php-object Allows generate class files parse from json and map json to php object, including multi-level and complex objects; Installation You

PHP library that helps to map any input into a strongly-typed value object structure.

Valinor • PHP object mapper with strong type support Valinor is a PHP library that helps to map any input into a strongly-typed value object structure

Object-Oriented API for PHP streams

Streamer Streamer is an Object-Oriented API for PHP streams. Why should I use Streams? A stream is a flow of bytes from one container to the other. Yo

Immutable value object for IPv4 and IPv6 addresses, including helper methods and Doctrine support.

IP is an immutable value object for (both version 4 and 6) IP addresses. Several helper methods are provided for ranges, broadcast and network address

Decimal handling as value object instead of plain strings.

Decimal Object Decimal value object for PHP. Background When working with monetary values, normal data types like int or float are not suitable for ex

Yet another Value Object Library (YAVOL)

Yet Another DDD Library Value object This library is a foundation in order to implement the Value Object pattern. It helps you to introduce some DDD s

FUGIO: Automatic Exploit Generation for PHP Object Injection Vulnerabilities

FUGIO FUGIO is the first automatic exploit generation (AEG) tool for PHP object injection (POI) vulnerabilities. When exploiting a POI vulnerability,

Releases(1.1.0)
  • 1.1.0(Feb 16, 2022)

    What's Changed

    • Allow Laravel 9 support by @peterbrinck in https://github.com/Morning-Train/data-transfer-object-casters/pull/3

    Full Changelog: https://github.com/Morning-Train/data-transfer-object-casters/compare/1.0.0...1.1.0

    Source code(tar.gz)
    Source code(zip)
Owner
Morningtrain
Here we will release open source projects. Stay tuned for great stuff!
Morningtrain
Creating data transfer objects with the power of php objects. No php attributes, no reflection api, and no other under the hook work.

Super Simple DTO Creating data transfer objects with the power of php objects. No php attributes, no reflection api, and no other under the hook work.

Mohammed Manssour 8 Jun 8, 2023
An article about alternative solution for convert object into a JSON Object for your api.

Do we really need a serializer for our JSON API? The last years I did build a lot of JSON APIs but personally was never happy about the magic of using

Alexander Schranz 1 Feb 1, 2022
Your alter ego object. Takes the best of object and array worlds.

Supporting Opensource formapro\values is an MIT-licensed open source project with its ongoing development made possible entirely by the support of com

FormaPro 31 Jun 25, 2021
Configure Magento 2 to send email using Google App, Gmail, Amazon Simple Email Service (SES), Microsoft Office365 and many other SMTP (Simple Mail Transfer Protocol) servers

Magento 2 SMTP Extension - Gmail, G Suite, Amazon SES, Office 365, Mailgun, SendGrid, Mandrill and other SMTP servers. For Magento 2.0.x, 2.1.x, 2.2.x

MagePal :: Magento Extensions 303 Oct 7, 2022
Melek Berita Backend is a service for crawling data from various websites and processing the data to be used for news data needs.

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

Chacha Nurholis 2 Oct 9, 2022
provides a nested object property based user interface for accessing this configuration data within application code

laminas-config This package is considered feature-complete, and is now in security-only maintenance mode, following a decision by the Technical Steeri

Laminas Project 43 Dec 26, 2022
Data visualization for NASA's DSNNow public data

DSN Monitor Data visualization for NASA's DSNNow public data. A live version of the project can be accessed at http://dsnmonitor.ddns.net. Description

Vinz 2 Sep 18, 2022
Import data from and export data to a range of different file formats and media

Ddeboer Data Import library This library has been renamed to PortPHP and will be deprecated. Please use PortPHP instead. Introduction This PHP library

David de Boer 570 Dec 27, 2022
An object graph visualizer

print_o An object graph visualizer for PHP What is object graph ? Object-oriented applications contain complex webs of interrelated objects. Objects a

Akihito Koriyama 139 Jan 1, 2023
Allows reflection of object attributes, including inherited and non-public ones

sebastian/object-reflector Allows reflection of object attributes, including inherited and non-public ones. Installation You can add this library as a

Sebastian Bergmann 6k Jan 4, 2023