Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
uidgenerator.php
1<?php
2
4
7
9{
10 public const DATE_PART_FORMAT = 'Ymd\THis\Z';
11 public const MAX_UID_LENGTH = 255;
12 private const CORRECT_LENGTH = 2;
13
17 private Date $date;
21 private string $portalName;
25 private int $userId;
26
30 public static function createInstance(): UidGenerator
31 {
32 return new self();
33 }
34
41 public function getUidWithDate(): string
42 {
43 $portalName = $this->portalName ?? '';
44
45 $datePart = $this->date
46 ->setTimezone(Util::prepareTimezone())
47 ->format(self::DATE_PART_FORMAT)
48 ;
49
50 $postfix = md5((string)time(). $this->userId);
51
52 $datePartLength = mb_strlen($datePart);
53 $portalNameLength = mb_strlen($portalName);
54 $hashLength = mb_strlen($postfix);
55
56 if (($datePartLength + $portalNameLength + $hashLength) > self::MAX_UID_LENGTH)
57 {
58 $allowableLength = self::MAX_UID_LENGTH - $datePartLength - $hashLength - self::CORRECT_LENGTH;
59 $portalName = substr($this->portalName, 0, $allowableLength);
60 }
61
62 return $datePart . '-' . $postfix . "@" . $portalName;
63 }
64
69 public function setDate(Date $date): UidGenerator
70 {
71 $this->date = $date;
72
73 return $this;
74 }
75
80 public function setPortalName(string $portalName): UidGenerator
81 {
82 $this->portalName = $portalName;
83
84 return $this;
85 }
86
91 public function setUserId(int $userId): UidGenerator
92 {
93 $this->userId = $userId;
94
95 return $this;
96 }
97}
static prepareTimezone(?string $tz=null)
Definition util.php:75