<?php

namespace IMATHUZH\Qfq\Core\Parser;

class ReportCacheKeyParser {

    private String $cacheKeyInput;

    /**
     * @param String $cacheKeyInput
     */
    public function __construct(String $cacheKeyInput) {
        $this->cacheKeyInput = $cacheKeyInput;
    }

    public const TABLES_KEY = 'tables';

    /**
     * @return array
     */
    public function parse(): array {
        $parts = explode('|', $this->cacheKeyInput);
        $result = [];

        foreach ($parts as $part) {
            $keyName = explode(':', $part, 2);      // ['S', 'grId,action'] or ['@', 'Person,Event']
            $values = explode(',', $keyName[1]);    // ['grId', 'action']

            // '@' means tables (Syntax: |@:Person,Event)
            $key = $keyName[0] === '@' ? self::TABLES_KEY : $keyName[0];
            $result[$key] = $values;
        }

        return $result;
    }

}