Skip to content

Decomposers ​

The Support\Decomposers sub-namespace contains three parser classes used by the code generators to convert compact CLI-style definition strings into structured PHP arrays.


ModelRelationParser ​

Unusualify\Modularous\Support\Decomposers\ModelRelationParser

Parses a relation definition string (as entered in make:model --relations) into an array of Eloquent relationship descriptors, and can render the resulting public function …() method stubs.

Supported Relation Types ​

belongsTo, hasOne, hasMany, hasOneThrough, hasManyThrough, belongsToMany, morphTo, morphOne, morphToMany

Input Format ​

Each relation is expressed as a colon-separated string:

company:belongsTo
tags:belongsToMany:tag
commentable:morphTo

Key Methods ​

MethodDescription
parse(string $relations): arrayParse the string and return an array of relation descriptors
toArray(): arrayCall parse() on the stored relations
render(): stringRender all relations as PHP method stubs

Example ​

php
use Unusualify\Modularous\Support\Decomposers\ModelRelationParser;

$parser = new ModelRelationParser('company:belongsTo,tags:belongsToMany:tag');
$rendered = $parser->render();
// outputs public function company() { return $this->belongsTo(Company::class); } ...

SchemaParser (Decomposers) ​

Unusualify\Modularous\Support\Decomposers\SchemaParser

Parses a field-definition string (as entered in make:model --fields) into an array of input schema descriptors consumed by hydrate and Vue input generators.

Input Format ​

Fields are comma-separated, each with name:type[:modifier…] syntax:

title:string,body:textarea,published_at:datetime:nullable,status:select

Key Methods ​

MethodDescription
parse(string $schema): arrayParse the string and return field descriptors
toArray(): arrayCall parse() on the stored schema
render(): stringRender input schema stubs for use in Hydrate files

ValidatorParser ​

Unusualify\Modularous\Support\Decomposers\ValidatorParser

Parses a validation-rules string (as entered in make:model --rules) into a ['field' => 'rules'] array suitable for pasting into a StoreRequest / UpdateRequest.

Input Format ​

Rules are &-separated, each field expressed as field=rule1|rule2:

title=required|string|max:255&body=nullable|string&published_at=nullable|date

Key Methods ​

MethodDescription
parse(mixed $rules): arrayParse and return ['field' => 'rules_string']
toArray(): arrayCall parse() on the stored rules
toReplacement(): stringRender the array as a PHP array_export string with correct indentation for insertion into a stub
getFields(): arrayReturn the raw &-split field tokens

Example ​

php
use Unusualify\Modularous\Support\Decomposers\ValidatorParser;

$parser = new ValidatorParser('title=required|string|max:255&body=nullable|string');
$rules  = $parser->toArray();
// ['title' => 'required|string|max:255', 'body' => 'nullable|string']

  • Migrations\SchemaParser — renders migration $table->… code from the same schema string format
  • Generators — consume all three parsers during scaffolding