Your IP : 216.73.216.68


Current Path : /home/jeromecohp/www/plugins/system/yooessentials/modules/api/src/Google/
Upload File :
Current File : /home/jeromecohp/www/plugins/system/yooessentials/modules/api/src/Google/GoogleSheetApi.php

<?php
/**
 * @package   Essentials YOOtheme Pro (Freemium) 2.0.19 build 1026.0920
 * @author    ZOOlanders https://www.zoolanders.com
 * @copyright Copyright (C) Joolanders, SL
 * @license   http://www.gnu.org/licenses/gpl.html GNU/GPL
 */

namespace ZOOlanders\YOOessentials\Api\Google;

class GoogleSheetApi extends AbstractGoogleApi implements GoogleSheetApiInterface
{
    /**
     * @see https://developers.google.com/sheets/api/reference/rest/v4/ValueInputOption
     */
    public const INPUT_TYPE_RAW = 'RAW';
    public const INPUT_TYPE_USER_ENTERED = 'USER_ENTERED';

    protected $apiEndpoint = 'https://sheets.googleapis.com/v4';

    public function spreadsheet(string $spreadsheetId, array $params = []): array
    {
        return $this->get("spreadsheets/$spreadsheetId", $params);
    }

    public function sheets(string $spreadsheetId, array $fields = ['sheetId', 'title'], array $params = []): array
    {
        return $this->spreadsheet(
            $spreadsheetId,
            array_merge(
                [
                    'fields' => 'sheets.properties(' . implode(',', $fields) . ')',
                ],
                $params
            )
        )['sheets'] ?? [];
    }

    public function values(string $spreadsheetId, string $range, array $params = []): array
    {
        $range = urlencode($range);

        return $this->get("spreadsheets/$spreadsheetId/values/$range", $params);
    }

    public function append(string $spreadsheetId, array $values, string $range, array $params = []): array
    {
        $body = [
            'range' => $range,
            'values' => [$values],
        ];

        $params += ['valueInputOption' => self::INPUT_TYPE_RAW];

        $range = urlencode($range);

        $url = "spreadsheets/$spreadsheetId/values/$range:append?" . http_build_query($params, '', '&');

        return $this->post($url, $body);
    }
}