Projectsphp-dataFramework Agnostic

PHP Data

Package

Lightweight, framework-agnostic Data Transfer Object (DTO) library with first-class Laravel integration.

Framework-agnostic usage

The core of this package has no hard dependency on Laravel. Data::from(), make(), toArray(), nested data, #[ArrayOf], Sometimes, Lazy and enum casting all work with only PHP installed.

Laravel-specific behaviour is detected at runtime through BradieTilley\Data\DataSystemContext, which caches capability checks so the hot path stays cheap:

use BradieTilley\Data\DataSystemContext;

DataSystemContext::hasCarbon();     // is nesbot/carbon installed?
DataSystemContext::hasCollection(); // is Illuminate\Support\Collection available?
DataSystemContext::hasModel();      // is Eloquent available?
DataSystemContext::hasConfig();     // is the Laravel config() helper available?

What changes without Laravel

FeatureWith LaravelWithout Laravel
Data::collect()Returns Illuminate\Support\CollectionReturns a plain array
Collection-typed propertiesHydrated to a CollectionTreated as an array
Carbon propertiesParsed/formatted (if Carbon installed)Left untouched unless Carbon is installed independently
Data::from($model)Reads Eloquent attributesNot applicable (no Eloquent)
#[WhenLoaded] / #[WhenCounted] / #[WhenAppended] / #[When]Conditional model hydration (see Laravel Integration)Ignored (attributes are Laravel-only; hydrate from arrays as usual)
Data::paginate(...)Wraps Laravel paginatorsThrows on unknown paginator input

Everything else — scalar/enum/nested-data hydration and serialization — is identical in both environments.

Standalone example

require 'vendor/autoload.php';

use BradieTilley\Data\Data;

class PointData extends Data
{
    public function __construct(
        public int $x,
        public int $y,
    ) {}
}

$point = PointData::from(['x' => 1, 'y' => 2]);
$point->toArray(); // ['x' => 1, 'y' => 2]