galancev/segmentator

Segmentator 组件

v2.2 2021-05-19 14:47 UTC

This package is auto-updated.

Last update: 2024-09-19 22:45:32 UTC


README

用于轻松创建分段器

要创建新的分段器,需要从 Segmentator 类继承新的分段器类,可选地调整 cookie 名称、其生命周期和应工作的域名。还必须定义 getNewSegment 和 isCorrectSegment 方法。

示例

<?php

use Galantcev\Components\Segmentator;

/**
 * Новый яростный сегментатор
 */
class NewPerfectSegmentator extends Segmentator
{
    /**
     * Имя куки
     * @var string
     */
    protected $cookieName = 'PerfectSegment';

    /**
     * Домен куки
     * @var string
     */
    protected $cookieDomain = '.google.com';

    const SEGMENT_SHOW_BLOCK = 1;
    const SEGMENT_HIDE_BLOCK = 2;

    /**
     * Генерирует новый сегмент
     * @return int
     */
    protected function getNewSegment()
    {
        $rand = mt_rand(0, 100);

        if ($rand <= 66) {
            $segment = self::SEGMENT_SHOW_BLOCK;
        } else {
            $segment = self::SEGMENT_HIDE_BLOCK;
        }

        return $segment;
    }

    /**
     * Проверяет сегмент на корректность
     * @param int $segment
     * @return bool
     */
    protected function isCorrectSegment($segment)
    {
        return in_array($segment, [
            self::SEGMENT_SHOW_BLOCK,
            self::SEGMENT_HIDE_BLOCK,
        ]);
    }

    /**
     * Если сегмент с отображением блока
     * @return bool
     */
    public function isSegmentShowBlock()
    {
        return $this->isSegment(self::SEGMENT_SHOW_BLOCK);
    }

    /**
     * Если сегмент со скрытием блока
     * @return bool
     */
    public function isSegmentHideBlock()
    {
        return $this->isSegment(self::SEGMENT_HIDE_BLOCK);
    }
}