Skip to content

Instantly share code, notes, and snippets.

@illifw
Created June 27, 2025 09:24
Show Gist options
  • Select an option

  • Save illifw/05050a16b444473afa7ac80c4dcbc77d to your computer and use it in GitHub Desktop.

Select an option

Save illifw/05050a16b444473afa7ac80c4dcbc77d to your computer and use it in GitHub Desktop.
<?php
/**
* Format parameters
* @method $this name(string|null $name)
* @method $this value(string $value)
* @property-read string|null $name optional
* @property-read string $value required
*/
final class ThingArgument implements
IListable,
IFormatable
{
use TBuild;
public function getDefaultFormatter(): IThingArgumentFormatter
{
return new class extends Formatter implements
IThingArgumentFormatter {
public function internal_format(
IFormatable $Source
): string {
/**
* @var ThingArgument $Source
*/
$is1 = null !== $Source->name;
// 2 value
return (string) ($Print = new Sprintf(
...[
'name' => $Source->name,
'value' => $Source->value
]
))
// [name: ]
->valS($Print->Mapped->name, $is1)
->text(':', $is1)
->space($is1)
// [value]
->valS($Print->Mapped->value)
;
}
};
}
/**
* @var (callable():Optional|Required)[]
*/
protected array $options {
get => [
fn(): Required => $this->required(BuildRequired::ArgumentValue),
fn(): Optional => $this->optional(BuildOptional::ArgumentName),
];
}
}
<?php
final class ThingArguments implements
IThingList,
IFormatable
{
use TThingList;
use TFormatable;
public function getDefaultFormatter(): IThingArgumentsFormatter
{
return new class extends Formatter implements
IThingArgumentsFormatter {
use TDebugInfo;
public function internal_format(
IFormatable $Source
): string {
/**
* @var ICountable&IArrayable $Source
*/
return static::implode($Source, ',' . PHP_EOL);
}
};
}
public function add(
ThingArgument $Argument
): static {
$this->internal_add($Argument);
return $this;
}
}
<?php
/**
* Format parameters
* @method $this name(string $name)
* @method $this arguments(ThingArguments $arguments)
* @property-read string $name required
* @property-read ThingArguments $arguments optional
*/
final class ThingAttribute implements
IGroupable,
IFormatable,
Stringable
{
use TBuild;
use TSortable;
use TSortByName;
public static function getDefaultSorter(
): __PHP_Closure {
return static::createSorter(
SortBy::Name
);
}
public function getDefaultFormatter(): IThingAttributeFormatter
{
return new class extends Formatter implements
IThingAttributeFormatter {
protected function internal_format(
IFormatable $Source
): string {
/**
* @var ThingAttribute $Source
*/
$is2 = 0 !== count($Source->arguments);
$Source->arguments->setFormatter(
new class extends Formatter implements
IThingArgumentsFormatter {
use TDebugInfo;
#[Show]
public int $indent {
get => 1;
}
protected function internal_format(
IFormatable $Source
): string {
/**
* @var ICountable&IArrayable $Source
*/
return PHP_EOL . parent::implode($Source, ',' . PHP_EOL);
}
}
);
return (string) ($Print = new Sprintf(
...[
'name' => $Source->name,
'arguments' => $Source->arguments
]
))
->text('#[')
// [$name]
->valS($Print->Mapped->name)
->text('(', $is2)
->text((string) $Source->arguments, $is2)
->eol($is2)
->text(')', $is2)
->text(']')
;
}
};
}
/**
* @var (callable():Optional|Required)[]
*/
protected array $options {
get => [
fn(): Required => $this->required(BuildRequired::AttributeName),
fn(): Optional => $this->optional(BuildOptional::AttributeArguments),
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment