Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
mention.php
1<?php
2
10
12{
13 protected const PATTERN_USER = '/\[user\s*=\s*([^\]]*)\](.+?)\[\/user\]/is' . BX_UTF_PCRE_MODIFIER;
14 protected const PATTERN_PROJECT = '/\[project\s*=\s*([^\]]*)\](.+?)\[\/project\]/is' . BX_UTF_PCRE_MODIFIER;
15 protected const PATTERN_DEPARTMENT = '/\[department\s*=\s*([^\]]*)\](.+?)\[\/department\]/is' . BX_UTF_PCRE_MODIFIER;
16
17 protected static function getPatternsList(): array
18 {
19 return [
23 ];
24 }
25
32 public static function getUserIds(string $text = ''): array
33 {
34 $usersData = self::getUsers($text);
35 return array_map(function($item) { return $item['ID']; }, $usersData);
36 }
37
44 public static function getUsers(string $text = ''): array
45 {
46 return self::getEntitiesList($text, self::PATTERN_USER);
47 }
48
49 public static function getProjectIds(string $text = ''): array
50 {
51 $projectsData = self::getProjects($text);
52 return array_map(function($item) { return $item['ID']; }, $projectsData);
53 }
54
55 public static function getProjects(string $text = ''): array
56 {
57 return self::getEntitiesList($text, self::PATTERN_PROJECT);
58 }
59
60 public static function getDepartmentIds(string $text = ''): array
61 {
62 $departmentsData = self::getDepartments($text);
63 return array_map(function($item) { return $item['ID']; }, $departmentsData);
64 }
65
66 public static function getDepartments(string $text = ''): array
67 {
68 return self::getEntitiesList($text, self::PATTERN_DEPARTMENT);
69 }
70
71 protected static function getEntitiesList(string $text = '', string $pattern = ''): array
72 {
73 $result = [];
74 preg_match_all($pattern, $text, $matches);
75
76 if (empty($matches))
77 {
78 return $result;
79 }
80
81 foreach($matches[1] as $key => $userId)
82 {
83 $result[] = [
84 'ID' => (int)$userId,
85 'NAME' => $matches[2][$key],
86 ];
87 }
88
89 return $result;
90 }
91
98 public static function clear(string $text = ''): string
99 {
100 $result = $text;
101
102 foreach (self::getPatternsList() as $pattern)
103 {
104 $result = preg_replace(
105 $pattern,
106 '\\2',
107 $result
108 );
109 }
110
111 return $result;
112 }
113}
static getUsers(string $text='')
Definition mention.php:44
static getDepartmentIds(string $text='')
Definition mention.php:60
static getUserIds(string $text='')
Definition mention.php:32
static clear(string $text='')
Definition mention.php:98
static getDepartments(string $text='')
Definition mention.php:66
static getProjects(string $text='')
Definition mention.php:55
static getProjectIds(string $text='')
Definition mention.php:49
static getEntitiesList(string $text='', string $pattern='')
Definition mention.php:71