<?php

namespace Application\Service\Extension\Helper;

use Application\Service\Extension\Helper\base\BaseHelper;

/**
 * @property FormContentHelper $formContentLog 表单内容日志
 */
class LogHelper extends BaseHelper
{
    public static array $attach = [];

    public static string $behavior = '';

    public static string $actionUrl = '';

    /**
     *
     * @var array|null
     */
    public ?array $postContentData = null;

    public function setAttach($attach, $key = 'default'): void
    {
        if (is_array($attach)) {
            foreach ($attach as $item) {
                self::$attach[$key][] = $item;
            }
        } else {
            self::$attach[$key][] = $attach;
        }
    }

    public function setBehavior(string $behavior)
    {
        self::$behavior = trim($behavior);
    }

    public function getBehavior(): string
    {
        return self::$behavior;
    }

    public function setActionUrl(string $actionUrl)
    {
        self::$actionUrl = trim($actionUrl);
    }

    public function getActionUrl(): string
    {
        return self::$actionUrl;
    }

    public function setTraceAttach(string $attach)
    {
        $this->setAttach([$attach], 'trace');
    }

    public function flush(): void
    {

    }

    public function __get($name)
    {
        if ($name === 'formContentLog') {
            return new FormContentHelper();
        }
    }

    protected function setCache($value, string $helperKey = self::HELPER_KEY_BASE)
    {
        // TODO: Implement setCache() method.
    }

    protected function getCacheKey()
    {
        // TODO: Implement getCacheKey() method.
    }

    /**
     * 处理注解
     */
    public function handleDocument(string $document)
    {
        $pregArr = [
            'behavior' => '/@behavior (.*)/',
        ];

        foreach ($pregArr as $index => $preg) {
            if ($index === 'behavior') {
                preg_match($preg, $document, $matches);
                if ($matches) {
                    $this->setBehavior($matches[1]);
                }
            }
        }
    }
}