HEX
Server: LiteSpeed
System: Linux sv4.hami.host 5.14.0-611.54.3.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Thu May 7 16:31:24 EDT 2026 x86_64
User: armgroup (1008)
PHP: 8.2.32
Disabled: show_source, system, shell_exec, passthru, exec, popen, proc_open, mail, socket_create, socket_create_listen, socket_create_pair, link, dl, openlog, syslog, stream_socket_server, curl_multi_init
Upload Files
File: /home/armgroup/libs/packages/chartisan/php/src/Chartisan.php
<?php

declare(strict_types=1);

namespace Chartisan\PHP;

/**
 * Represents a chartisan chart instance.
 */
class Chartisan
{
    /**
     * Stores the server data of the chart.
     */
    protected ServerData $serverData;

    /**
     * Creates a new instance of a chartisan chart.
     */
    public function __construct(ServerData $serverData)
    {
        $this->serverData = $serverData;
    }

    /**
     * Creates a new instance of a chartisan chart.
     */
    public static function build(): Chartisan
    {
        return new Chartisan(new ServerData);
    }

    /**
     * Sets the chart labels.
     *
     * @param  string[]  $labels
     */
    public function labels(array $labels): Chartisan
    {
        $this->serverData->chart->labels = $labels;

        return $this;
    }

    /**
     * Adds extra information to the chart.
     */
    public function extra(array $value): Chartisan
    {
        $this->serverData->chart->extra = $value;

        return $this;
    }

    /**
     * AdvancedDataset appends a new dataset to the chart or modifies an existing one.
     * If the ID has already been used, the dataset will be replaced with this one.
     */
    public function advancedDataset(string $name, array $values, ?array $extra): Chartisan
    {
        $dataset = $this->getDataset($name);
        if ($dataset) {
            $dataset->name = $name;
            $dataset->values = $values;
            $dataset->extra = $extra;
        } else {
            $this->serverData->datasets[] = new DatasetData($name, $values, $extra);
        }

        return $this;
    }

    /**
     * Dataset adds a new simple dataset to the chart. If more advanced control is
     * needed, consider using `AdvancedDataset` instead.
     */
    public function dataset(string $name, array $values): Chartisan
    {
        return $this->advancedDataset($name, $values, null);
    }

    /**
     * Returns the string representation JSON encoded.
     */
    public function toJSON(): string
    {
        return json_encode($this->toObject());
    }

    /**
     * Transforms it to an object.
     */
    public function toObject(): ServerData
    {
        return $this->serverData;
    }

    /**
     * Gets the dataset with the given name.
     *
     * @return ServerData|null
     */
    protected function getDataset(string $name): ?DatasetData
    {
        foreach ($this->serverData->datasets as $dataset) {
            if ($dataset->name == $name) {
                return $dataset;
            }
        }

        return null;
    }
}