<?php

namespace stripe\helpers;

use Exception;
use objects\products_o;

class stripe_product
{
    /**
     * Product id
     * @var int $id
     */
    protected int $id;
    /**
     * Product object
     * @var products_o $product
     */
    protected products_o $product;


    /**
     * @throws Exception
     */
    public function __construct(int $id)
    {
        // Get the product name from the database
        $products_o = new products_o();
        $product = $products_o->select($id);
        $product->requireSelected();
        $this->id = $id;
        $this->product = $product;
    }

    public function __toString(): string
    {
        // Return the line item as a JSON string
        return json_encode(
            self::get()
        );
    }

    public function get(): array
    {
        return [
            'name' => $this->product->name->value(),
            'id' => $this->id,
        ];
    }
}