Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
numbergeneratorfactory.php
1<?
3
6
12{
13 const EVENT_GENERATOR_CLASSES_COLLECT = 'onNumberGeneratorsClassesCollect';
14
20 public function createGeneratorByType($type)
21 {
22 $class = $this->getClassByType($type);
23
24 return new $class();
25 }
26
32 public function getTypeToClassMap()
33 {
34 static $generatorTypeToClassMap = [];
35 if (empty($generatorTypeToClassMap))
36 {
37 $generatorClasses = [
38 Generator\SequentNumberGenerator::class,
39 Generator\DateNumberGenerator::class,
40 Generator\RandomNumberGenerator::class,
41 Generator\PrefixNumberGenerator::class,
42 ];
43 $generatorClasses = array_merge_recursive($generatorClasses, $this->collectCustomGeneratorClasses());
44
45 foreach ($generatorClasses as $generatorClass)
46 {
48 $generatorTypeToClassMap[$generatorClass::getType()] = $generatorClass;
49 }
50 }
51
52 return $generatorTypeToClassMap;
53 }
54
58 private function collectCustomGeneratorClasses()
59 {
60 $event = new Event('main', self::EVENT_GENERATOR_CLASSES_COLLECT);
61 $event->send();
62 $results = $event->getResults();
63
64 $generatorClasses = [];
65 foreach ($results as $result)
66 {
67 if ($result->getType() != EventResult::SUCCESS)
68 {
69 continue;
70 }
71 $className = $result->getParameters();
72
73 if (class_exists($className) && (is_subclass_of($className, Generator\NumberGenerator::class)))
74 {
75 $generatorClasses[] = $result->getParameters();
76 }
77 }
78
79 return $generatorClasses;
80 }
81
87 public function getClassByType($type)
88 {
89 if (in_array($type, array_keys($this->getTypeToClassMap())))
90 {
91 return $this->getTypeToClassMap()[$type];
92 }
93
94 return null;
95 }
96
101 public function getClasses()
102 {
103 return array_values($this->getTypeToClassMap());
104 }
105}