lotharthesavior/chocookies

用于在Psr7对象上管理Cookies的简单包。

0.0.4 2022-05-23 08:33 UTC

This package is auto-updated.

Last update: 2024-09-23 14:02:51 UTC


README

Tests

通过Psr7标准进行Cookie管理的包。

安装

使用composer安装

composer require lotharthesavior/chocookies

用法

use Chocookies\Cookies;

Cookies::setCookie($requestOrResponse, 'my-cookie-key', 'my-cookie-data');
Cookies::getCookie($requestOrResponse, 'my-cookie-key');

接口

namespace Chocookies\Interfaces;

use Carbon\Carbon;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

interface CookiesInterface
{
    /**
     * Set Cookie.
     *
     * @param ResponseInterface|ServerRequestInterface $serverObject
     * @param string $cookieKey
     * @param string $cookieValue
     * @param Carbon|null $expireHour
     * @return void
     */
    public static function setCookie(
        ResponseInterface|ServerRequestInterface &$serverObject,
        string $cookieKey,
        string $cookieValue,
        ?Carbon $expireHour = null
    ): void;

    /**
     * Retrieve cookie by the key.
     *
     * @param ResponseInterface|ServerRequestInterface $serverObject
     * @param string $key
     * @return string|null
     */
    public static function getCookie(ResponseInterface|ServerRequestInterface $serverObject, string $key): ?string;

    /**
     * Expire cookie at the ResponseInterface object.
     *
     * @param ServerRequestInterface $request
     * @param ResponseInterface $response
     * @param string $key
     * @return void
     */
    public static function expireCookie(
        ServerRequestInterface $request,
        ResponseInterface &$response,
        string $key
    ): void;

    /**
     * Get an expired version of a cookie. Used to expire cookies.
     *
     * @param ServerRequestInterface $request
     * @param string $key
     * @return string
     */
    public static function getExpiredCookie(ServerRequestInterface $request, string $key): string;
}