Find a way to declare PHP object class as schema

Created on 4 February 2023, almost 2 years ago
Updated 3 April 2023, over 1 year ago

Problem/Motivation

I did some work with components lately, and noticed one thing that changed the show for me:
With PHP8.1's constructor promotion and readonle properties we get super cheap typesafe immutable objects. Eg like that:

final class FullResult {
  public function __construct(
    public readonly ListInfo      $listInfo,
    public readonly MessageInfo   $messageInfo,
    public readonly ResultVerdict $verdict,
    public readonly ResultSummary $summary,
    public readonly ResultDetails $details,
  ) {}

That's it! I passed them to twig, and even with Drupal's restricitons on methods, it was fun to work with that (fyi: In PHPStorm, symfony plugin helps with IDE integration in twig.) I neithe needed nor wanted to convert my typesafe immutable objects to an array.

Guessing that that will be interesting for others too, i wonder if we can improve object support for parameters.

Proposed resolution

Allow class or interface declaration for parameters as alternative to json schema.

πŸ“Œ Task
Status

Fixed

Version

2.0

Component

Code

Created by

πŸ‡©πŸ‡ͺGermany geek-merlin Freiburg, Germany

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024