Review generated order input mapper

from Data types
PHP 8.3.33 advanced 6 min 4 issues to find

Review this generated request mapper before it handles customer-controlled data.

Accept only quantity, express, and coupon_id; require a nonnegative integer quantity, parse a Boolean or the strings 'true'/'false', preserve a missing coupon as null, and reject unknown fields.

php
<?php
final class OrderInput
{
    public int $quantity;
    public bool $express;
    public ?int $couponId = null;

    public static function fromArray(array $input): self
    {
        $order = new self();
        foreach ($input as $field => $value) {
            $order->$field = $value;
        }

        $order->quantity = (int) ($input['quantity'] ?? null);
        $order->express = (bool) ($input['express'] ?? false);
        $order->couponId = (int) ($input['coupon_id'] ?? null);
        return $order;
    }
}

generated code is illustrative, not from any one model

Open in playground
Report an error