PHPackages                             hedgehoglab-engineering/php-declared-data - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. hedgehoglab-engineering/php-declared-data

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

hedgehoglab-engineering/php-declared-data
=========================================

A set of data tools for PHP.

v1.0.0(1y ago)2545MITPHPPHP ^8.2

Since Oct 4Pushed 1y agoCompare

[ Source](https://github.com/hedgehoglab-engineering/php-declared-data)[ Packagist](https://packagist.org/packages/hedgehoglab-engineering/php-declared-data)[ RSS](/packages/hedgehoglab-engineering-php-declared-data/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (4)Versions (2)Used By (0)

PHP Declared Data
=================

[](#php-declared-data)

This package provides attributes, contracts, and behaviors that simplify the creation and management of data classes in PHP applications.

In particular, it enables the use of sparse objects that can be hydrated from optional data submitted via PATCH requests in a RESTful API, allowing missing or nullable fields to be handled appropriately.

Table of Contents
-----------------

[](#table-of-contents)

- [Installation](#installation)
- [Usage](#usage)

    - [Defining Data Classes](#defining-data-classes)
    - [Interfaces](#interfaces)
        - [ResolvableData](#resolvabledata)
        - [LenientData](#lenientdata)
        - [SparseData](#sparsedata)
    - [Traits](#traits)
        - [ArraysData](#arraysdata)
        - [CollectsData](#collectsdata)
        - [CreatesData](#createsdata)
        - [DeclaresData](#declaresdata)
    - [Attributes](#attributes)
        - [CollectionOf](#collectionof)
        - [DateTimeFromFormat](#datetimefromformat)
        - [JsonDecode](#jsondecode)
        - [MapArgumentName](#mapargumentname)
- [Examples](#examples)

    - [Using Attributes in Data Classes](#using-attributes-in-data-classes)
- [Testing](#testing)
- [Code Formatting](#code-formatting)
- [Code Analysing](#code-analysing)
- [Contributing](#contributing)

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

[](#installation)

Install the package via Composer:

```
composer require hedgehoglab-engineering/php-declared-data
```

Usage
-----

[](#usage)

### Defining Data Classes

[](#defining-data-classes)

The easiest way to start to define a declared data object is to extend the `AbstractDeclaredData` class. Alternatively, you can compose your own using the provided contracts and traits. Then just define your properties in the constructor.

```
use HedgehoglabEngineering\DeclaredData\AbstractDeclaredData;

class UserData extends AbstractDeclaredData
{
    public function __construct(
        public string $name,
        public string $email,
        public int $age,
    ) {
        //
    }
}
```

### Interfaces

[](#interfaces)

Interfaces define behaviors that can be implemented by data classes for different functionality.

#### ResolvableData

[](#resolvabledata)

Implementing the `ResolvableData` interface allows a class to resolve its properties using the specified property type or a PHP 8 attribute.

```
use HedgehoglabEngineering\DeclaredData\Contracts\ResolvableData;

class UserData extends AbstractDeclaredData implements ResolvableData
{
    // ...
}
```

#### LenientData

[](#lenientdata)

The `LenientData` interface allows the data object to ignore extra properties that are not defined in the class.

```
use HedgehoglabEngineering\DeclaredData\Contracts\LenientData;

class UserData extends AbstractDeclaredData implements LenientData
{
    // ...
}
```

#### SparseData

[](#sparsedata)

The `SparseData` interface allows the data object to be instantiated without all required properties. The primary use case for this is transforming PATCH request data where missing properties without defaults may remain unset - i.e.: indicating that the field's value is not being modified.

```
use HedgehoglabEngineering\DeclaredData\Contracts\SparseData;

class UserData extends AbstractDeclaredData implements SparseData
{
    // ...
}
```

### Traits

[](#traits)

Traits provide reusable functionality that can be applied to your data classes.

#### ArraysData

[](#arraysdata)

The `ArraysData` trait provides a `toArray` method to recursively convert a data instance into an array.

#### CollectsData

[](#collectsdata)

The `CollectsData` trait provides a static `collect` method which can convert an iterable value into a `Illuminate\Support\Collection` of instances of the defined class.

#### CreatesData

[](#createsdata)

The `CreatesData` trait provides a static `create` method which can convert data into an instance of the defined class.

#### DeclaresData

[](#declaresdata)

The `DeclaresData` trait provides `has`, `missing`, `only` and `except` methods, which are useful for handling instances of declared data.

### Attributes

[](#attributes)

The use of PHP attributes provides a mechanism for hinting how properties should be resolved.

#### CollectionOf

[](#collectionof)

Transforms an array into an instance of `Illuminate\Support\Collection` containing instances of the specified class.

```
use HedgehoglabEngineering\DeclaredData\Attributes\CollectionOf;

#[CollectionOf(class: PostData::class)]
public readonly Collection $posts;
```

#### DateTimeFromFormat

[](#datetimefromformat)

Parses a date string into a `DateTime` object using a specified format and/or timezone.

```
use HedgehoglabEngineering\DeclaredData\Attributes\DateTimeFromFormat;

#[DateTimeFromFormat(format: 'Y-m-d', timezone: 'UTC', toTimezone: 'America/New_York')]
public readonly DateTimeInterface $publishedAt;
```

#### JsonDecode

[](#jsondecode)

Decodes a JSON string into a PHP array or object.

```
use HedgehoglabEngineering\DeclaredData\Attributes\JsonDecode;

#[JsonDecode(associative: true)]
public readonly array $settings;
```

#### MapArgumentName

[](#mapargumentname)

Maps an input field name to a different property name in the data object.

```
use HedgehoglabEngineering\DeclaredData\Attributes\MapArgumentName;

#[MapArgumentName(name: 'first_name')]
public string $firstName;
```

Examples
--------

[](#examples)

### Using Attributes in Data Classes

[](#using-attributes-in-data-classes)

Here's an example of a data class using various attributes:

```
use HedgehoglabEngineering\DeclaredData\AbstractDeclaredData;
use HedgehoglabEngineering\DeclaredData\Contracts\ResolvableData;
use HedgehoglabEngineering\DeclaredData\Attributes\CollectionOf;
use HedgehoglabEngineering\DeclaredData\Attributes\DateTimeFromFormat;
use HedgehoglabEngineering\DeclaredData\Attributes\JsonDecode;
use HedgehoglabEngineering\DeclaredData\Attributes\MapArgumentName;
use Illuminate\Support\Collection;

class PostData extends AbstractDeclaredData implements ResolvableData
{
    public function __construct(
        #[MapArgumentName(name: 'post_title')]
        public string $title,

        #[DateTimeFromFormat('Y-m-d H:i:s')]
        public DateTimeInterface $createdAt,

        #[JsonDecode(associative: true)]
        public array $metadata,

        #[CollectionOf(class: CommentData::class)]
        public Collection $comments,
    ) {
        //
    }
}

$inputData = [
    'post_title' => 'Test Post',
    'createdAt' => '2023-10-01 12:00:00',
    'metadata' => '{"views": 100, "likes": 10}',
    'comments' => [
        ['content' => 'Great post!'],
    ],
];

$resolvedPostData = PostData::create($inputData);
```

Testing
-------

[](#testing)

```
composer test
```

Code Formatting
---------------

[](#code-formatting)

```
composer format
```

Code Analysing
--------------

[](#code-analysing)

```
composer analyse
```

Contributing
------------

[](#contributing)

Contributions are welcome! Please submit a pull request or open an issue to discuss your ideas.

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance36

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 81.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

592d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/624c4f6bab3e31a7f022c26be6166a2c362a2d769675f585eda44c07e1693a43?d=identicon)[tomchkk](/maintainers/tomchkk)

---

Top Contributors

[![jawur](https://avatars.githubusercontent.com/u/54275259?v=4)](https://github.com/jawur "jawur (13 commits)")[![tomchkk](https://avatars.githubusercontent.com/u/6698306?v=4)](https://github.com/tomchkk "tomchkk (3 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/hedgehoglab-engineering-php-declared-data/health.svg)

```
[![Health](https://phpackages.com/badges/hedgehoglab-engineering-php-declared-data/health.svg)](https://phpackages.com/packages/hedgehoglab-engineering-php-declared-data)
```

###  Alternatives

[barryvdh/laravel-ide-helper

Laravel IDE Helper, generates correct PHPDocs for all Facade classes, to improve auto-completion.

14.9k123.0M687](/packages/barryvdh-laravel-ide-helper)[orchestra/canvas

Code Generators for Laravel Applications and Packages

20917.2M158](/packages/orchestra-canvas)[illuminate/pipeline

The Illuminate Pipeline package.

9346.6M213](/packages/illuminate-pipeline)[illuminate/pagination

The Illuminate Pagination package.

10532.5M862](/packages/illuminate-pagination)[spatie/laravel-pjax

A pjax middleware for Laravel 5

513371.8k11](/packages/spatie-laravel-pjax)[spatie/laravel-mix-preload

Add preload and prefetch links based your Mix manifest

169176.0k2](/packages/spatie-laravel-mix-preload)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
