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

namespace ZOOlanders\YOOessentials\Source\Provider\Request\Type;

class RequestType
{
    public static function config(): array
    {
        return [
            'fields' => [
                'href' => [
                    'type' => 'String',
                    'metadata' => [
                        'label' => 'Href',
                    ],
                ],
                'time' => [
                    'type' => 'String',
                    'metadata' => [
                        'label' => 'Timestamp',
                        'filters' => ['datemodify', 'date'],
                    ],
                    'extensions' => [
                        'call' => __CLASS__ . '::resolveTimestamp',
                    ],
                ],
                'ip' => [
                    'type' => 'String',
                    'metadata' => [
                        'label' => 'IP',
                    ],
                ],
                'method' => [
                    'type' => 'String',
                    'metadata' => [
                        'label' => 'Method',
                    ],
                ],
                'origin' => [
                    'type' => 'String',
                    'metadata' => [
                        'label' => 'Origin',
                    ],
                ],
                'useragent' => [
                    'type' => 'String',
                    'metadata' => [
                        'label' => 'Useragent',
                    ],
                ],
                'url' => [
                    'type' => 'YooessentialsRequestUrl',
                    'metadata' => [
                        'label' => 'Url',
                    ],
                    'extensions' => [
                        'call' => __CLASS__ . '::resolveUrl',
                    ],
                ],
            ],

            'metadata' => [
                'type' => true,
                'label' => 'Request',
            ],
        ];
    }

    public static function resolveUrl(array $request): array
    {
        return parse_url($request['href'] ?? []);
    }

    public static function resolveTimestamp(array $request): int
    {
        return round($request['time'] ?? 0);
    }
}
