posOrderId = $posOrderId; $this->productUpdates = $productUpdates; } public static function fromArray( array $data ): self { if ( empty( $data['pos_order_id'] ) || ! is_string( $data['pos_order_id'] ) ) { throw new InvalidArgumentException( 'pos_order_id is required.' ); } if ( empty( $data['product_updates'] ) || ! is_array( $data['product_updates'] ) ) { throw new InvalidArgumentException( 'product_updates is required and must not be empty.' ); } return new self( $data['pos_order_id'], array_map( [ self::class, 'validateProductUpdate' ], $data['product_updates'] ) ); } private static function validateProductUpdate( $update ): array { if ( ! is_array( $update ) || empty( $update['product_id'] ) || ! isset( $update['stock_quantity'] ) ) { throw new InvalidArgumentException( 'Each product update requires product_id and stock_quantity.' ); } if ( ! is_numeric( $update['stock_quantity'] ) ) { throw new InvalidArgumentException( 'stock_quantity must be numeric.' ); } return [ 'product_id' => (int) $update['product_id'], 'stock_quantity' => (int) $update['stock_quantity'], ]; } public function posOrderId(): string { return $this->posOrderId; } public function productUpdates(): array { return $this->productUpdates; } }