### Installing Bag with Composer (Bash) Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.3/install.md This command installs the Bag library using Composer, the PHP dependency manager. It adds `dshafik/bag` to your project's `composer.json` and downloads the necessary files. ```Bash composer require dshafik/bag ``` -------------------------------- ### Installing Bag with Composer Source: https://github.com/dshafik/bag/blob/main/docs/install.md This command installs the Bag library into your PHP project using Composer, the dependency manager for PHP. It adds `dshafik/bag` to your project's `composer.json` and downloads the necessary files. ```Bash composer require dshafik/bag ``` -------------------------------- ### Installing Bag Library with Composer (Bash) Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.1/install.md This command installs the Bag library using Composer, the PHP dependency manager. It adds `dshafik/bag` to your project's dependencies, making the library available for use. Ensure Composer is installed and configured on your system. ```Bash composer require dshafik/bag ``` -------------------------------- ### Installing Bag with Composer (bash) Source: https://github.com/dshafik/bag/blob/main/docs/versions/1.x/install.md This command installs the Bag library using Composer, the PHP dependency manager. It adds `dshafik/bag` to your project's dependencies, making the library available for use. ```bash composer require dshafik/bag ``` -------------------------------- ### Installing Bag with Composer (Bash) Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.4/install.md This command installs the Bag library using Composer, the PHP dependency manager. It adds `dshafik/bag` to your project's dependencies and downloads the necessary files. This is the standard way to include PHP libraries in a project. ```Bash composer require dshafik/bag ``` -------------------------------- ### Installing Bag with Composer - Bash Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.0/install.md This command installs the Bag library into your PHP project using Composer, the dependency manager for PHP. It adds `dshafik/bag` to your project's `composer.json` and downloads the necessary files, making the library available for use. ```Bash composer require dshafik/bag ``` -------------------------------- ### Installing Bag with Composer (Bash) Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.2/install.md This command installs the Bag library into your PHP project using Composer, the PHP dependency manager. It adds `dshafik/bag` to your project's `composer.json` and downloads the necessary files, making the library available for use in your application. ```bash composer require dshafik/bag ``` -------------------------------- ### Installing Bag with Composer Source: https://github.com/dshafik/bag/blob/main/docs/index.md This command uses Composer, the PHP dependency manager, to install the 'dshafik/bag' library into your project. Executing this command will add Bag as a required dependency, allowing you to start using its immutable value object features. ```Shell composer require dshafik/bag ``` -------------------------------- ### Installing Project Dependencies - Composer Shell Source: https://github.com/dshafik/bag/blob/main/CONTRIBUTING.md Executes Composer to install all required PHP dependencies for the Bag project and sets up Git hooks for development workflow. ```shell composer install ``` -------------------------------- ### Installing Bag PHP Library Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.4/index.md This command installs the Bag PHP library using Composer, the dependency manager for PHP. It adds `dshafik/bag` to your project's `composer.json` and downloads the necessary files, making the library available for use in your PHP application. ```Bash composer require dshafik/bag ``` -------------------------------- ### Installing Bag with Composer - Bash Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.1/index.md This command installs the `dshafik/bag` package into your PHP project using Composer, the PHP dependency manager. Executing this command adds the package as a dependency in your `composer.json` file and downloads its files into the `vendor` directory, making the library available for use in your application. ```bash composer require dshafik/bag ``` -------------------------------- ### Installing Bag PHP Library via Composer Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.0/index.md This command installs the Bag PHP library into your project using Composer, the PHP dependency manager. It adds `dshafik/bag` to your project's `composer.json` and downloads the necessary files. ```Shell composer require dshafik/bag ``` -------------------------------- ### Installing Bag Package via Composer Source: https://github.com/dshafik/bag/blob/main/README.md This `bash` command installs the `dshafik/bag` package using Composer, the PHP dependency manager. It adds the package to your project's `composer.json` and downloads its dependencies, making the Bag library available for use in your PHP application. ```bash composer require dshafik/bag ``` -------------------------------- ### Installing Bag PHP Library Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.3/index.md This shell command uses Composer, the PHP dependency manager, to install the `dshafik/bag` package. Executing this command adds Bag as a dependency to your project, allowing you to utilize its immutable value object features. ```Shell composer require dshafik/bag ``` -------------------------------- ### Example Bag Constructor with Properties Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.4/laravel-artisan-make-bag-command.md This PHP code provides an example of a `MyBag` class constructor with various typed properties, including scalar types, objects, and collections, demonstrating how properties are defined for automatic factory generation. ```php public function __construct( public string $name, public int $age, #[Cast(DateTime::class, 'y-m-d')] public CarbonImmutable $birthday, public Money $money, public AnotherBag $test, #[Cast(CollectionOf::class, AnotherBag::class)] public Collection $collection, ) { } ``` -------------------------------- ### Installing Bag via Composer Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.2/index.md This command installs the Bag library into your PHP project using Composer, the PHP dependency manager. It adds `dshafik/bag` as a dependency, making the immutable value object features available for use in your application. ```Shell composer require dshafik/bag ``` -------------------------------- ### Installing Bag via Composer Source: https://github.com/dshafik/bag/blob/main/docs/versions/1.x/index.md This command installs the Bag library into your PHP project using Composer, the PHP dependency manager. It adds `dshafik/bag` to your project's dependencies, making the immutable value object functionality available for use. ```Shell composer require dshafik/bag ``` -------------------------------- ### Instantiating Bag Value Object with Named Arguments in PHP Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.3/basic-usage.md This example shows how to create an instance of a `Bag` Value Object using named arguments, providing clear readability and flexibility in argument order. ```php $value = MyValue::from( name: 'Davey Shafik', age: => 40, ); ``` -------------------------------- ### Example Bag Class Constructor with Properties Source: https://github.com/dshafik/bag/blob/main/docs/laravel-artisan-make-bag-command.md This PHP snippet provides an example of a `MyBag` constructor with various typed properties, including scalar types, objects, and collections. This structure is used by the factory generator to infer the `definition()` method. ```php public function __construct( public string $name, public int $age, #[Cast(DateTime::class, 'y-m-d')] public CarbonImmutable $birthday, public Money $money, public AnotherBag $test, #[Cast(CollectionOf::class, AnotherBag::class)] public Collection $collection, ) { } ``` -------------------------------- ### Creating Bag Object Instance with Factory (PHP) Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.2/testing.md This example demonstrates the simplest way to create an instance of a Bag object using its associated factory. The `::factory()->make()` method instantiates the object based on the factory's default definition. ```PHP $bag = MyValue::factory()->make(); ``` -------------------------------- ### Cloning the Bag Repository - Shell Source: https://github.com/dshafik/bag/blob/main/CONTRIBUTING.md This command clones the Bag project repository from GitHub to your local machine, allowing you to start development. ```shell git clone https://github.com/dshafik/bag.git ``` -------------------------------- ### Example Bag Constructor for Factory Generation (PHP) Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.0/laravel-artisan-make-bag-command.md This PHP snippet provides an example of a `MyBag` constructor with various typed properties, including scalar types, objects, and collections. This structure influences how the `make:bag` command generates the factory's `definition()` method. ```php public function __construct( public string $name, public int $age, #[Cast(DateTime::class, 'y-m-d')] public CarbonImmutable $birthday, public Money $money, public AnotherBag $test, #[Cast(CollectionOf::class, AnotherBag::class)] public Collection $collection, ) { } ``` -------------------------------- ### Instantiating Bag Value Object with Positional Arguments in PHP Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.0/basic-usage.md This example demonstrates creating a `MyValue` instance using positional arguments. It's crucial that the order of arguments matches the order of properties defined in the constructor. ```php $value = MyValue::from('Davey Shafik', 40); ``` -------------------------------- ### Instantiating Bag Value Object with Named Arguments in PHP Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.0/basic-usage.md This example shows how to create an instance of a `MyValue` object using named arguments. This method provides clarity by explicitly mapping input values to constructor properties by name. ```php $value = MyValue::from( name: 'Davey Shafik', age: => 40, ); ``` -------------------------------- ### Instantiating Bag Value Object with Collection Arguments in PHP Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.3/basic-usage.md This example demonstrates creating a `Bag` Value Object instance from an `Illuminate\Support\Collection`, similar to using an associative array. ```php $value = MyValue::from(collect([ 'name' => 'Davey Shafik', 'age' => 40, ])); ``` -------------------------------- ### Instantiating Bag Value Object with Named Arguments in PHP Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.4/basic-usage.md This example shows how to create an instance of a `Bag` Value Object using named arguments. This method provides clarity by explicitly mapping input values to constructor properties by name. ```PHP $value = MyValue::from( name: 'Davey Shafik', age: => 40, ); ``` -------------------------------- ### Demonstrating JSON-Specific Output Wrapping for Bag (PHP) Source: https://github.com/dshafik/bag/blob/main/docs/wrapping.md This example instantiates `MyValue` (defined with `#[WrapJson('data')]`) and illustrates the different behaviors of `->toArray()` and `->toJson()`. `->toArray()` produces unwrapped output, while `->toJson()` wraps the data under a 'data' key. ```php $myValue = MyValue::from(['name' => 'Davey Shafik', 'age' => 40]); $myValue->toArray(); // ['name' => 'Davey Shafik', 'age' => 40] $myValue->toJson(); // {"data":{"name":"Davey Shafik","age":40}} ``` -------------------------------- ### Example Usage of JSON-Wrapped Bag (PHP) Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.0/wrapping.md This example demonstrates the behavior of a `Bag` class configured with `#[WrapJson('data')]`. While `->toJson()` wraps the output under a 'data' key, `->toArray()` returns the unwrapped data, highlighting the attribute's JSON-specific application. ```php $myValue = MyValue::from(['name' => 'Davey Shafik', 'age' => 40]); $myValue->toArray(); // ['name' => 'Davey Shafik', 'age' => 40] $myValue->toJson(); // {"data":{"name":"Davey Shafik","age":40}} ``` -------------------------------- ### Example Bag Constructor for Factory Update (PHP) Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.2/laravel-artisan-make-bag-command.md This PHP code provides an example `MyBag` constructor with various typed properties, including those with casting attributes. This constructor serves as the input for the `make:bag --update` command to demonstrate how the factory's `definition()` method is automatically generated. ```php public function __construct( public string $name, public int $age, #[Cast(DateTime::class, 'y-m-d')] public CarbonImmutable $birthday, public Money $money, public AnotherBag $test, #[Cast(CollectionOf::class, AnotherBag::class)] public Collection $collection, ) { } ``` -------------------------------- ### Converting Class Instance to Bag Object Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.4/object-to-bag.md This example illustrates how to create an instance of `MyClass` (which uses the `HasBag` trait) and then convert it into a Bag object by calling the `->toBag()` method. This method leverages the configuration defined by the `Bag` attribute on the class. ```php $myClass = new MyClass(); $bag = $myClass->toBag(); ``` -------------------------------- ### Demonstrating Universal Output Wrapping for Bag (PHP) Source: https://github.com/dshafik/bag/blob/main/docs/wrapping.md This example instantiates `MyValue` (defined with `#[Wrap('data')]`) and shows how `->toArray()` and `->toJson()` methods produce wrapped output. Both methods will enclose the Bag's data within a 'data' key, as specified by the `Wrap` attribute. ```php $myValue = MyValue::from(['name' => 'Davey Shafik', 'age' => 40]); $myValue->toArray(); // ['data' => ['name' => 'Davey Shafik', 'age' => 40]] $myValue->toJson(); // {"data":{"name":"Davey Shafik","age":40}} ``` -------------------------------- ### Instantiating a Bag Value Object with a Collection in PHP Source: https://github.com/dshafik/bag/blob/main/docs/versions/1.x/basic-usage.md This example demonstrates creating a Bag Value Object instance from an `Illuminate\Support\Collection`. The `::from()` method can directly consume a collection, making it convenient when working with Laravel's data structures. ```php $value = MyValue::from(collect([ 'name' => 'Davey Shafik', 'age' => 40, ])); ``` -------------------------------- ### Instantiating Bag with SnakeCase Input (PHP) Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.1/mapping.md This example demonstrates how to instantiate the `MyValue` class (defined with class-level `SnakeCase` mapping) using `snake_case` input keys. Bag automatically maps these keys to the `camelCase` properties defined in the class. ```php $value = MyValue::from([ 'my_value' => 'value', 'my_other_value' => 'other value' ]); ``` -------------------------------- ### Instantiating Bag Value Object with Collection in PHP Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.4/basic-usage.md This example demonstrates creating a `Bag` Value Object instance from an `Illuminate\Support\Collection` (or `Bag\Collection`). This is useful when working with Laravel's collection API, providing a consistent way to pass data. ```PHP $value = MyValue::from(collect([ 'name' => 'Davey Shafik', 'age' => 40, ])); ``` -------------------------------- ### Transforming Eloquent Model to Array in Bag (PHP) Source: https://github.com/dshafik/bag/blob/main/docs/versions/1.x/transformers.md This example shows how to create a transformer to convert an Eloquent `User` model instance into an array. The `#[Transforms(User::class)]` attribute specifies that this method should be used when a `User` object is provided as input. The `toArray()` method is called on the model to get its array representation. ```php use App\Models\User; use Bag\Bag; class MyValue extends Bag { #[Transforms(User::class)] protected static function fromUser(User $user): array { return $user->toArray(); } } ``` -------------------------------- ### Creating Collections of Bag Objects (PHP) Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.2/testing.md This example shows how to generate a collection of multiple Bag objects using the `->count()` method. It creates a `Bag\Collection` containing the specified number of identical Bag object instances. ```PHP $values = MyValue::factory()->count(10)->make(); ``` -------------------------------- ### Example Usage of Hidden Property in PHP Bag Source: https://github.com/dshafik/bag/blob/main/docs/versions/1.x/hidden.md This example illustrates the effect of a `#[Hidden]` property during serialization. When an instance of `MyValue` is converted to an array or JSON, the `hiddenProperty` is omitted from the output, while `visibleProperty` remains visible. ```PHP $value = MyValue::from([ 'hiddenProperty' => 'hidden', 'visibleProperty' => 'visible' ]); $value->toArray(); // ['visibleProperty' => 'visible'] json_encode($value, JSON_THROW_ON_ERROR); // {"visibleProperty": "visible"} ``` -------------------------------- ### Running Unit Tests - Composer Shell Source: https://github.com/dshafik/bag/blob/main/CONTRIBUTING.md Initiates the test suite for the Bag project using Composer, ensuring all existing functionalities work as expected. ```shell composer run test ``` -------------------------------- ### Example Usage of Automatic Variadic Casting (PHP) Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.4/variadics.md Provides an example of instantiating the `MyValue` class with various boolean and integer inputs, demonstrating how Bag automatically casts them to boolean values as defined by the constructor's type hint. ```php $bag = new MyValue(true, false, 0, 1); // $bag->values = [true, false, false, true] ``` -------------------------------- ### Creating Collections with Bag::collect() in PHP Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.3/collections.md This snippet demonstrates how to create a collection of Value objects using the `Bag::collect()` method. It shows an example of populating a collection with associative arrays that will be converted into `MyValue` objects, resulting in a `\Bag\Collection` instance. ```php $values = MyValue::collect([ [ 'name' => 'Davey Shafik', 'age' => 40, ], [ 'name' => 'Taylor Otwell', 'age' => 40, ], ]); ``` -------------------------------- ### Instantiating a Bag Value Object with Named Arguments in PHP Source: https://github.com/dshafik/bag/blob/main/docs/versions/1.x/basic-usage.md This example shows how to create an instance of a Bag Value Object using named arguments with the static `::from()` method. This approach provides clear readability by explicitly mapping values to their corresponding property names. ```php $value = MyValue::from( name: 'Davey Shafik', age: => 40, ); ``` -------------------------------- ### Example Usage of JSON-Only Hidden Property in PHP Bag Source: https://github.com/dshafik/bag/blob/main/docs/versions/1.x/hidden.md This example clarifies the behavior of a `#[HiddenFromJson]` property. When `MyValue` is converted to an array, `hiddenProperty` is present. However, when serialized to JSON, `hiddenProperty` is omitted, showcasing its JSON-specific hiding. ```PHP $value = MyValue::from([ 'hiddenProperty' => 'hidden', 'visibleProperty' => 'visible' ]); $value->toArray(); // ['hiddenProperty' => 'hidden', visibleProperty' => 'visible'] json_encode($value, JSON_THROW_ON_ERROR); // {"visibleProperty": "visible"} ``` -------------------------------- ### Instantiating a Bag Value Object with an Array in PHP Source: https://github.com/dshafik/bag/blob/main/docs/basic-usage.md This example shows how to instantiate a Bag Value Object using an associative array. The keys of the array should correspond to the property names defined in the Value Object's constructor. ```PHP $value = MyValue::from([ 'name' => 'Davey Shafik', 'age' => 40, ]); ``` -------------------------------- ### Bag 2.x Nullable Property Handling Example Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.0/upgrading.md This PHP example illustrates the changed behavior in Bag 2.x where nullable properties without default values are automatically filled with `null` when instantiating a Bag from an empty array, preventing `MissingPropertiesException`. ```php readonly class MyBag extends Bag {\n public function __construct(\n public ?string $name\n }\n}\n\n// Bag 1.4.0 (and older)\nMyBag::from([]); // throws MissingPropertiesException\n\n// Bag 2.0.0+\nMyBag::from([]); // MyBag { $name = null } ``` -------------------------------- ### Demonstrating Universal Output Wrapping for Collection (PHP) Source: https://github.com/dshafik/bag/blob/main/docs/wrapping.md This example shows how `MyCollection` (defined with `#[Wrap('data')]`) behaves when converted to an array or JSON. Both `->toArray()` and `->toJson()` will output the collection's items nested within a 'data' key, consistent with the `Wrap` attribute. ```php $collection = MyValue::factory()->count(2)->make(); $collection->toArray(); // ['data' => [["name" => "Davey Shafik", "age" => 40], ...]] $collection->toJson(); // {"data":[{"name":"Davey Shafik","age":40}, ...]} ``` -------------------------------- ### Artisan make:bag Command Help Output Source: https://github.com/dshafik/bag/blob/main/docs/laravel-artisan-make-bag-command.md This snippet displays the help output for the `php artisan make:bag` command. It outlines the command's description, usage syntax, arguments, and a comprehensive list of available options. Each option's purpose, default value (if applicable), and short/long forms are detailed, providing a complete reference for command line usage. ```bash Description: Create a new Bag value class, with optional factory and collection. Usage: make:bag [options] [--] Arguments: name Options: -F, --force Force overwriting all files -E, --force-except-bag Force overwriting Factory/Collection files -u, --update Update Bag class to add factory/collection -f, --factory[=FACTORY] Create a Factory for the Bag [default: "interactive"] -c, --collection[=COLLECTION] Create a Collection for the Bag [default: "interactive"] -d, --docs Add Bag::from() docs to the Bag for IDE auto-complete -N, --namespace[=NAMESPACE] Specify the namespace for the Bag --pretend Dump the file contents instead of writing to disk -h, --help Display help for the given command. When no command is given display help for the list command -q, --quiet Do not output any message -V, --version Display this application version --ansi|--no-ansi Force (or disable --no-ansi) ANSI output -n, --no-interaction Do not ask any interactive question --env[=ENV] The environment the command should run under -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug ``` -------------------------------- ### Converting a PHP Object to a Bag Source: https://github.com/dshafik/bag/blob/main/docs/object-to-bag.md This example shows how to instantiate a `MyClass` object, which has the `HasBag` trait, and then convert it into a Bag object by simply calling the `->toBag()` method on the instance. This creates a new Bag object populated with the class's properties. ```php $myClass = new MyClass(); $bag = $myClass->toBag(); ``` -------------------------------- ### Example Bag Constructor with Properties and Casts Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.1/laravel-artisan-make-bag-command.md This PHP code demonstrates an updated `MyBag` constructor with various public properties, including scalar types, objects, and collections, some with `Cast` attributes. This structure influences the auto-generation of the factory's `definition()` method. ```php public function __construct( public string $name, public int $age, #[Cast(DateTime::class, 'y-m-d')] public CarbonImmutable $birthday, public Money $money, public AnotherBag $test, #[Cast(CollectionOf::class, AnotherBag::class)] public Collection $collection, ) { } ``` -------------------------------- ### Creating Bag Factory and Updating Docs (Bash) Source: https://github.com/dshafik/bag/blob/main/docs/versions/1.x/laravel-artisan-make-bag-command.md This command creates a factory for the `MyBag` class, generates documentation for the Bag's `from()` method, and updates any existing documentation. This is typically run after the Bag class has been customized to ensure the factory and docs reflect the latest structure. ```bash php artisan make:bag MyBag --factory --docs --update ``` -------------------------------- ### Example Usage of Wrapped Bag (PHP) Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.0/wrapping.md This example shows the effect of applying the `#[Wrap('data')]` attribute to a `Bag` instance. Both `->toArray()` and `->toJson()` calls result in the Bag's data being nested under a 'data' key, confirming the wrapping behavior for both output formats. ```php $myValue = MyValue::from(['name' => 'Davey Shafik', 'age' => 40]); $myValue->toArray(); // ['data' => ['name' => 'Davey Shafik', 'age' => 40]] $myValue->toJson(); // {"data":{"name":"Davey Shafik","age":40}} ``` -------------------------------- ### make:bag Command Options and Usage Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.3/laravel-artisan-make-bag-command.md This snippet provides the full help output for the `make:bag` Artisan command, detailing its description, usage, arguments, and all available options. It serves as a comprehensive reference for configuring Bag, factory, and collection generation and documentation, including force overwrite options. ```bash Description:\n Create a new Bag value class, with optional factory and collection.\n\nUsage:\n make:bag [options] [--] \n\nArguments:\n name \n\nOptions:\n -F, --force Force overwriting all files\n -E, --force-except-bag Force overwriting Factory/Collection files\n -u, --update Update Bag class to add factory/collection\n -f, --factory[=FACTORY] Create a Factory for the Bag [default: "interactive"]\n -c, --collection[=COLLECTION] Create a Collection for the Bag [default: "interactive"]\n -d, --docs Add Bag::from() docs to the Bag for IDE auto-complete\n -N, --namespace[=NAMESPACE] Specify the namespace for the Bag\n --pretend Dump the file contents instead of writing to disk\n -h, --help Display help for the given command. When no command is given display help for the list command\n -q, --quiet Do not output any message\n -V, --version Display this application version\n --ansi|--no-ansi Force (or disable --no-ansi) ANSI output\n -n, --no-interaction Do not ask any interactive question\n --env[=ENV] The environment the command should run under\n -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug ``` -------------------------------- ### make:bag Artisan Command Options Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.4/laravel-artisan-make-bag-command.md This snippet provides the full help output for the `make:bag` Artisan command, detailing its description, usage syntax, arguments, and all available options. It serves as a comprehensive reference for configuring the command's behavior, including force overwriting, factory/collection creation, documentation generation, and verbosity. ```bash Description: Create a new Bag value class, with optional factory and collection. Usage: make:bag [options] [--] Arguments: name Options: -F, --force Force overwriting all files -E, --force-except-bag Force overwriting Factory/Collection files -u, --update Update Bag class to add factory/collection -f, --factory[=FACTORY] Create a Factory for the Bag [default: "interactive"] -c, --collection[=COLLECTION] Create a Collection for the Bag [default: "interactive"] -d, --docs Add Bag::from() docs to the Bag for IDE auto-complete -N, --namespace[=NAMESPACE] Specify the namespace for the Bag --pretend Dump the file contents instead of writing to disk -h, --help Display help for the given command. When no command is given display help for the list command -q, --quiet Do not output any message -V, --version Display this application version --ansi|--no-ansi Force (or disable --no-ansi) ANSI output -n, --no-interaction Do not ask any interactive question --env[=ENV] The environment the command should run under -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug ``` -------------------------------- ### Example Bag Class Definition Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.4/laravel-artisan-make-bag-command.md This PHP code defines a `User` Bag class with several properties, including scalar types, a custom `Team` object, a `ReportCollection` (with a `Cast` attribute), and a `CarbonImmutable` instance. This class serves as an example for which documentation can be generated. ```php use App\Values\Collections\ReportCollection; use App\Values\Report; use App\Values\Team; use Carbon\CarbonImmutable; readonly class User extends Bag { public function __construct( public string $name, public int $age, public Team $team, #[Cast(CollectionOf::class, Report::class)] public ReportCollection $reports, public CarbonImmutable $lastLogin, ) { } } ``` -------------------------------- ### Creating a Bag Object Instance Using a Factory in PHP Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.0/testing.md This code demonstrates the simplest way to create an instance of a Bag object (`MyValue`) using its associated factory. By calling `::factory()` on the Bag class and then `->make()`, a new `MyValue` object is instantiated with the default values defined in its factory. ```PHP $bag = MyValue::factory()->make(); ``` -------------------------------- ### Example Bag Class Definition Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.3/laravel-artisan-make-bag-command.md This PHP code defines a `User` Bag class with various properties, including a `ReportCollection` that uses a `Cast` attribute. This class serves as an example of a Bag whose documentation can be updated to provide better IDE support. ```php use App\Values\Collections\ReportCollection;\nuse App\Values\Report;\nuse App\Values\Team;\nuse Carbon\CarbonImmutable;\n\nreadonly class User extends Bag {\n public function __construct(\n public string $name,\n public int $age,\n public Team $team,\n #[Cast(CollectionOf::class, Report::class)]\n public ReportCollection $reports,\n public CarbonImmutable $lastLogin,\n ) {\n }\n} ``` -------------------------------- ### Example Usage of Wrapped Collection (PHP) Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.0/wrapping.md This example illustrates the effect of applying the `#[Wrap('data')]` attribute to a `Collection` instance. Both `->toArray()` and `->toJson()` calls result in the collection's data being nested under a 'data' key, demonstrating the wrapping behavior for collections across both output formats. ```php $collection = MyValue::factory()->count(2)->make(); $collection->toArray(); // ['data' => [["name" => "Davey Shafik", "age" => 40], ...]] $collection->toJson(); // {"data":[{"name":"Davey Shafik","age":40}, ...]} ``` -------------------------------- ### Creating Bag Factory and Updating Docs Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.2/laravel-artisan-make-bag-command.md This command creates a factory for the `MyBag` class, generates documentation for its `from()` method, and updates any existing documentation. This step is performed after the Bag class has been customized to ensure the factory and docs reflect the final structure. ```bash php artisan make:bag MyBag --factory --docs --update ``` -------------------------------- ### Example Bag Class Definition (PHP) Source: https://github.com/dshafik/bag/blob/main/docs/versions/1.x/laravel-artisan-make-bag-command.md Defines a `User` Bag class with various properties, including a string, an integer, a custom `Team` object, a `ReportCollection` (with a `Cast` attribute for collection type), and a `CarbonImmutable` instance. This class serves as an example for which documentation can be generated. ```php use App\Values\Collections\ReportCollection;\nuse App\Values\Report;\nuse App\Values\Team;\nuse Carbon\CarbonImmutable;\n\nreadonly class User extends Bag {\n public function __construct(\n public string $name,\n public int $age,\n public Team $team,\n #[Cast(CollectionOf::class, Report::class)]\n public ReportCollection $reports,\n public CarbonImmutable $lastLogin,\n ) {\n }\n} ``` -------------------------------- ### Example Usage of Automatic Boolean Casting in Bag PHP Source: https://github.com/dshafik/bag/blob/main/docs/versions/1.x/variadics.md This snippet provides an example of instantiating `MyValue` with various input types (booleans and integers). It demonstrates how Bag's automatic casting converts `0` to `false` and `1` to `true` when the variadic parameter is type-hinted as `bool`, resulting in an array of boolean values. ```php $bag = new MyValue(true, false, 0, 1); // $bag->values = [true, false, false, true] ``` -------------------------------- ### Input Pipeline Flow Diagram - Mermaid Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.0/how-bag-works.md This Mermaid diagram illustrates the complete flow of the `InputPipeline` in the Bag library. It details each step from initial data input (`Bag::from($data)`) through transformation, parameter processing, validation, casting, and finally, the construction of the `Bag` object. The diagram also highlights various error points such as missing parameters, extra parameters, validation failures, and uninitialized computed properties. ```Mermaid graph TD; start("Bag::from($data)") --> transform(Transform Input) --> process(Process Parameters) --> variadic(Is Variadic?) --> fillNulls(Fill Nulls) --> mapInput(Map Input) --> laravelParams(Laravel Route Parameter Binding) -- Finalized Input Values --> missing(Missing Parameters) --> missingError{Error?} missingError -- Yes --> errorMissingParameters(MissingPropertiesException) missingError -- No --> extra(Extra Parameters) --> extraError{Error?} extraError -- Yes --> errorExtraParameters(ExtraPropertiesException) extraError -- No --> validate(Validate) --> valid{Valid?} valid -- No --> errorValidation(ValidationException) valid -- Yes --> cast(Cast Input) --> construct("new Bag(...)") --> computed(Verify Computed Values) --> initialized{Initialized?} initialized -- No --> errorInitialization(ComputedPropertyUninitializedException) initialized -- Yes --> bag(Bag Value Returned) class start mermaid-start class missingError,extraError,valid,initialized mermaid-decision class bag mermaid-end class errorMissingParameters,errorExtraParameters,errorValidation,errorInitialization mermaid-error click start "https://github.com/dshafik/bag/blob/main/src/Bag/Bag.php" _blank click transform "https://github.com/dshafik/bag/blob/main/src/Bag/Pipelines/Pipes/Transform.php" _blank click process "https://github.com/dshafik/bag/blob/main/src/Bag/Pipelines/Pipes/ProcessParameters.php" _blank click variadic "https://github.com/dshafik/bag/blob/main/src/Bag/Pipelines/Pipes/IsVariadic.php" _blank click fillNull "https://github.com/dshafik/bag/blob/main/src/Bag/Pipelines/Pipes/FillNulls.php" _blank click mapInput "https://github.com/dshafik/bag/blob/main/src/Bag/Pipelines/Pipes/MapInput.php" _blank click laravelParams "https://github.com/dshafik/bag/blob/main/src/Bag/Pipelines/Pipes/LaravelRouteParameters.php" _blank click missing "https://github.com/dshafik/bag/blob/main/src/Bag/Pipelines/Pipes/MissingParameters.php" _blank click extra "https://github.com/dshafik/bag/blob/main/src/Bag/Pipelines/Pipes/ExtraParameters.php" _blank click validate "https://github.com/dshafik/bag/blob/main/src/Bag/Pipelines/Pipes/Validate.php" _blank click cast "https://github.com/dshafik/bag/blob/main/src/Bag/Pipelines/Pipes/CastInputValues.php" _blank click construct "https://github.com/dshafik/bag/blob/main/src/Bag/Pipelines/Pipes/FillBag.php" _blank click computed "https://github.com/dshafik/bag/blob/main/src/Bag/Pipelines/Pipes/ComputedValues.php" _blank ``` -------------------------------- ### Example of Automatic Boolean Casting for Variadic Properties (PHP) Source: https://github.com/dshafik/bag/blob/main/docs/variadics.md This snippet provides a concrete example of the automatic boolean casting demonstrated in the previous code. It instantiates MyValue with mixed boolean and integer inputs, showing how Bag casts 0 to false and 1 to true when assigned to the values property, which is type-hinted as bool. ```PHP $bag = new MyValue(true, false, 0, 1); // $bag->values = [true, false, false, true] ``` -------------------------------- ### Creating Bag Factory and Documentation Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.4/laravel-artisan-make-bag-command.md This command creates a factory for `MyBag`, generates documentation for it, and updates the Bag class with `@method` annotations. This is the recommended step after customizing the Bag class to ensure proper factory and documentation generation. ```bash php artisan make:bag MyBag --factory --docs --update ``` -------------------------------- ### Example Bag Class Definition (PHP) Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.0/laravel-artisan-make-bag-command.md This PHP code defines a `User` Bag class with several properties, including a string, an integer, a custom `Team` object, a `ReportCollection` (which is cast as a collection of `Report` objects), and a `CarbonImmutable` instance. This class serves as an example of a Bag value object before its `@method` annotation is automatically generated. ```php use App\Values\Collections\ReportCollection; use App\Values\Report; use App\Values\Team; use Carbon\CarbonImmutable; readonly class User extends Bag { public function __construct( public string $name, public int $age, public Team $team, #[Cast(CollectionOf::class, Report::class)] public ReportCollection $reports, public CarbonImmutable $lastLogin, ) { } } ``` -------------------------------- ### `make:bag` Artisan Command Options Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.2/laravel-artisan-make-bag-command.md This snippet provides the full help output for the `php artisan make:bag` command, detailing its description, usage syntax, arguments, and all available options. It serves as a comprehensive reference for configuring the command's behavior, including force overwriting, factory/collection creation, documentation generation, and verbosity. ```bash Description: Create a new Bag value class, with optional factory and collection. Usage: make:bag [options] [--] Arguments: name Options: -F, --force Force overwriting all files -E, --force-except-bag Force overwriting Factory/Collection files -u, --update Update Bag class to add factory/collection -f, --factory[=FACTORY] Create a Factory for the Bag [default: "interactive"] -c, --collection[=COLLECTION] Create a Collection for the Bag [default: "interactive"] -d, --docs Add Bag::from() docs to the Bag for IDE auto-complete -N, --namespace[=NAMESPACE] Specify the namespace for the Bag --pretend Dump the file contents instead of writing to disk -h, --help Display help for the given command. When no command is given display help for the list command -q, --quiet Do not output any message -V, --version Display this application version --ansi|--no-ansi Force (or disable --no-ansi) ANSI output -n, --no-interaction Do not ask any interactive question --env[=ENV] The environment the command should run under -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug ``` -------------------------------- ### Applying String Validation Attribute in PHP Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.1/validation.md Example usage of the `#[Str]` attribute to enforce that a value is a string. This attribute is based on Laravel's `string` rule. ```PHP #[Str] ``` -------------------------------- ### Applying Numeric Validation Attribute in PHP Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.1/validation.md Example usage of the `#[Numeric]` attribute to enforce that a value is numeric. This attribute is based on Laravel's `numeric` rule. ```PHP #[Numeric] ``` -------------------------------- ### Creating Bag Factory and Updating Docs (Bash) Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.0/laravel-artisan-make-bag-command.md This command creates a factory for `MyBag`, adds documentation for the `Bag::from()` method, and updates the Bag class. The `--factory` option generates the factory, `--docs` adds the `@method` annotation, and `--update` ensures existing files are modified if they already exist. ```bash php artisan make:bag MyBag --factory --docs --update ``` -------------------------------- ### Applying Integer Validation Attribute in PHP Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.1/validation.md Example usage of the `#[Integer]` attribute to enforce that a value is an integer. This attribute is based on Laravel's `integer` rule. ```PHP #[Integer] ``` -------------------------------- ### Creating a Bag Object Instance Using a Factory in PHP Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.4/testing.md This snippet demonstrates the simplest way to create an instance of a Bag object using its associated factory. Calling `MyValue::factory()->make()` instantiates `MyValue` with the default values defined in `MyValueFactory`. ```PHP $bag = MyValue::factory()->make(); ``` -------------------------------- ### Applying In Validation Attribute in PHP Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.1/validation.md Example usage of the `#[In]` attribute to enforce that a value is present in a given list of values. This attribute is based on Laravel's `in` rule. ```PHP #[In('foo', 'bar')] ``` -------------------------------- ### Implementing a Custom Kebab Case Mapper in PHP Source: https://github.com/dshafik/bag/blob/main/docs/versions/1.x/mapping.md This snippet demonstrates how to create a custom mapper by implementing the `MapperInterface`. The `Kebab` class converts input and output property names to kebab-case by first camel-casing them. This allows for custom naming conventions beyond the built-in mapping attributes. ```php use Bag\Mappers\MapperInterface; use Illuminate\Support\Str; class Kebab implements MapperInterface { public function input(string $name): string { return Str::of($name)->camel()->kebab(); } public function output(string $name): string { return Str::of($name)->camel()->kebab(); } } ``` -------------------------------- ### Applying Boolean Validation Attribute in PHP Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.1/validation.md Example usage of the `#[Boolean]` attribute to enforce that a value is a boolean. This attribute is based on Laravel's `boolean` rule. ```PHP #[Boolean] ``` -------------------------------- ### Applying Between Validation Attribute in PHP Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.1/validation.md Example usage of the `#[Between]` attribute to enforce that a value is between two specified values (inclusive). This attribute is based on Laravel's `between` rule. ```PHP #[Between(1, 10)] ``` -------------------------------- ### Creating Bag Factory and Updating Docs Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.3/laravel-artisan-make-bag-command.md This command creates a factory for the `MyBag` class, generates documentation for the `Bag::from()` method, and updates any existing documentation. It is used after the Bag class has been customized to ensure consistency between the class, its factory, and its documentation. ```bash php artisan make:bag MyBag --factory --docs --update ``` -------------------------------- ### Illustrating the Input Pipeline Flow - Mermaid Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.1/how-bag-works.md This Mermaid diagram visually represents the complete flow of the InputPipeline within the Bag library. It outlines each stage, from initial data input (Bag::from($data)) through various processing steps like transformation, parameter handling, validation, and casting, culminating in the construction of a new Bag object. The diagram also highlights potential error points and their corresponding exceptions. ```mermaid graph TD; start("Bag::from($data)") --> transform(Transform Input) --> process(Process Parameters) --> variadic(Is Variadic?) --> fillNulls(Fill Nulls) --> mapInput(Map Input) --> laravelParams(Laravel Route Parameter Binding) -- Finalized Input Values --> missing(Missing Parameters) --> missingError{Error?} missingError -- Yes --> errorMissingParameters(MissingPropertiesException) missingError -- No --> extra(Extra Parameters) --> extraError{Error?} extraError -- Yes --> errorExtraParameters(AdditionalPropertiesException) extraError -- No --> strip(Strip Extra Parameters) --> validate(Validate) --> valid{Valid?} valid -- No --> errorValidation(ValidationException) valid -- Yes --> cast(Cast Input) --> construct("new Bag(...)") --> computed(Verify Computed Values) --> initialized{Initialized?} initialized -- No --> errorInitialization(ComputedPropertyUninitializedException) initialized -- Yes --> bag(Bag Value Returned) class start mermaid-start class missingError,extraError,valid,initialized mermaid-decision class bag mermaid-end class errorMissingParameters,errorExtraParameters,errorValidation,errorInitialization mermaid-error click start "https://github.com/dshafik/bag/blob/main/src/Bag/Bag.php" _blank click transform "https://github.com/dshafik/bag/blob/main/src/Bag/Pipelines/Pipes/Transform.php" _blank click process "https://github.com/dshafik/bag/blob/main/src/Bag/Pipelines/Pipes/ProcessParameters.php" _blank click variadic "https://github.com/dshafik/bag/blob/main/src/Bag/Pipelines/Pipes/IsVariadic.php" _blank click fillNull "https://github.com/dshafik/bag/blob/main/src/Bag/Pipelines/Pipes/FillNulls.php" _blank click mapInput "https://github.com/dshafik/bag/blob/main/src/Bag/Pipelines/Pipes/MapInput.php" _blank click laravelParams "https://github.com/dshafik/bag/blob/main/src/Bag/Pipelines/Pipes/LaravelRouteParameters.php" _blank click missing "https://github.com/dshafik/bag/blob/main/src/Bag/Pipelines/Pipes/MissingParameters.php" _blank click extra "https://github.com/dshafik/bag/blob/main/src/Bag/Pipelines/Pipes/ExtraParameters.php" _blank click strip "https://github.com/dshafik/bag/blob/main/src/Bag/Pipelines/Pipes/StripExtraParameters.php" _blank click validate "https://github.com/dshafik/bag/blob/main/src/Bag/Pipelines/Pipes/Validate.php" _blank click cast "https://github.com/dshafik/bag/blob/main/src/Bag/Pipelines/Pipes/CastInputValues.php" _blank click construct "https://github.com/dshafik/bag/blob/main/src/Bag/Pipelines/Pipes/FillBag.php" _blank click computed "https://github.com/dshafik/bag/blob/main/src/Bag/Pipelines/Pipes/ComputedValues.php" _blank ``` -------------------------------- ### Applying Size Validation Attribute in PHP Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.1/validation.md Example usage of the `#[Size]` attribute to enforce that a value has a specific size. This attribute is based on Laravel's `size` rule. ```PHP #[Size(10)] ``` -------------------------------- ### Applying Required Validation Attribute in PHP Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.1/validation.md Example usage of the `#[Required]` attribute to enforce that a value is present and not empty. This attribute is based on Laravel's `required` rule. ```PHP #[Required] ``` -------------------------------- ### Applying Max Validation Attribute in PHP Source: https://github.com/dshafik/bag/blob/main/docs/versions/2.1/validation.md Example usage of the `#[Max]` attribute to enforce that a value is at most a given size. This attribute is based on Laravel's `max` rule. ```PHP #[Max(100)] ```