Polyscope - The agent-first dev environment for Laravel

Bag

Bag stats

Downloads
1.2K
Stars
76
Open Issues
4
Forks
7

View on GitHub →

A comprehensive immutable value objects implementation

Bag

Coverage Quality Gate Status

Bag

Immutable Value Objects for PHP 8.3+ inspired by spatie/laravel-data, created by Davey Shafik.

Introduction

Bag helps you create immutable value objects. It's a great way to encapsulate data within your application.

Bag prioritizes immutability and type safety with built-in validation and data casting.

When to use Value Objects

Value objects should be used in place of regular arrays, allowing you enforce type safety and immutability.

Features

  • Immutable & Strongly typed
  • Value casting — both input and output
  • Collection support
  • Composable — nest Bag value objects and collections
  • Built-in validation

[!NOTE] Bag is framework-agnostic, but it works great with Laravel. Bag uses standard Laravel Collections and Validation. In addition, it will automatically inject Bag\Bag value objects into your controllers with validation.

Requirements

Bag requires PHP 8.3+, and supports Laravel 11.x.

Installation

You can install the package via composer:

composer require dshafik/bag

Usage

Creating a Value Object

To create a basic Value Object, extend the Bag\Bag class and define your properties in the constructor:

use Bag\Bag;
 
readonly class MyValue extends Bag {
public function __construct(
public string $name,
public int $age,
) {
}
}

Instantiating a Value Object

To create a new instance of your Value Object, call the ::from() method:

$value = MyValue::from([
'name' => 'Davey Shafik',
'age' => 40,
]);

Documentation

Full documentation can be found here.

dshafik photo

Davey Shafik is a full-time developer with 25 years of experience as a technical polyglot with a focus on PHP and related technologies.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Dshafik Bag Related Articles

Immutable Value Objects in PHP and Laravel With the Bags Package image

Immutable Value Objects in PHP and Laravel With the Bags Package

Read article
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
The Certification of Competence for Laravel logo

The Certification of Competence for Laravel

A community-driven, proctored assessment across 4 levels designed to validate real-world Laravel knowledge, from Junior to mastery-level Artisan. Official Vue.js, Official Nuxt, Angular, React, JS certifications also available.

The Certification of Competence for Laravel
LoadForge logo

LoadForge

Scalable load testing for web apps & APIs. Simulate real-world traffic and identify breaking points and performance limits with powerful, scalable load tests designed for Laravel.

LoadForge
Honeybadger logo

Honeybadger

Simple developer-focused application monitoring for Laravel. Error tracking, log management, uptime monitoring, status pages, and more!

Honeybadger
Securing Laravel logo

Securing Laravel

The essential security resource for Laravel devs, covering everything you need to keep your apps secure. Sign up to receive weekly security tips and monthly in depth articles, diving deep into security concepts you need to know!

Securing Laravel