33namespace GGrach \FishGenerator \Generators ;
44
55use GGrach \FishGenerator \Exceptions \GenerateElementException ;
6- use GGrach \FishGenerator \Exceptions \SearchIblockException ;
76use GGrach \FishGenerator \Exceptions \GeneratorTypeException ;
87use GGrach \FishGenerator \PropertyRulesElementFilter ;
8+ use GGrach \FishGenerator \Parser \RuleGenerationParser ;
99
1010/**
1111 * Логика генерации элемента
1212 */
1313class ElementGenerator extends PropertyRulesElementFilter {
1414
15- /**
16- * Свойства которые считаются за стандартные свойства битрикса
17- */
18- const STANDART_PROPERTIES = [
19- 'NAME ' , 'ACTIVE ' , 'CODE ' , 'IBLOCK_SECTION_ID ' , 'DETAIL_TEXT ' , 'PREVIEW_TEXT ' , 'SORT '
20- ];
21-
2215 /**
2316 * @var Object Адаптированный генератор
2417 * @todo Добавить интерфейс, возможность сменить генератор
@@ -46,10 +39,10 @@ public function generate(int $countElements) {
4639
4740 $ categoryPhoto = $ this ->getRandomCategoryPhoto ();
4841
49- $ arPropertyGenerated = $ this ->getGeneratedPropertyArray ( );
42+ $ arValidPropertiesForGeneration = RuleGenerationParser:: parse ( $ this ->arPropertyGenerateRules );
5043
51- $ linkPreviewImage = $ this -> dataGenerator -> imageUrl ( 1000 , 1000 , $ categoryPhoto );
52- $ linkDetailImage = $ this -> dataGenerator -> imageUrl ( 1000 , 1000 , $ categoryPhoto );
44+ $ linkPreviewImage = ' https://loremflickr.com/ 1000/ 1000/ ' . $ categoryPhoto. ' ?salt= ' . \uniqid ( );
45+ $ linkDetailImage = ' https://loremflickr.com/ 1000/ 1000/ ' . $ categoryPhoto. ' ?salt= ' . \uniqid ( );
5346
5447 $ previewPicture = $ this ->generatePhotoFromLink ($ linkPreviewImage );
5548 $ detailPicture = $ this ->generatePhotoFromLink ($ linkDetailImage );
@@ -65,15 +58,15 @@ public function generate(int $countElements) {
6558 ];
6659
6760 // Вставляем стандартные свойства
68- if (!empty ($ arPropertyGenerated ['STANDART_PROPERTIES ' ])) {
69- foreach ($ arPropertyGenerated ['STANDART_PROPERTIES ' ] as $ propCode => $ propValue ) {
61+ if (!empty ($ arValidPropertiesForGeneration ['STANDART_PROPERTIES ' ])) {
62+ foreach ($ arValidPropertiesForGeneration ['STANDART_PROPERTIES ' ] as $ propCode => $ propValue ) {
7063 $ arField [$ propCode ] = $ propValue ;
7164 }
7265 }
7366
7467 // Вставляем свойства
75- if (!empty ($ arPropertyGenerated ['PROPERTIES ' ])) {
76- $ arField ['PROPERTY_VALUES ' ] = $ arPropertyGenerated ['PROPERTIES ' ];
68+ if (!empty ($ arValidPropertiesForGeneration ['PROPERTIES ' ])) {
69+ $ arField ['PROPERTY_VALUES ' ] = $ arValidPropertiesForGeneration ['PROPERTIES ' ];
7770 }
7871
7972 $ arField ['NAME ' ] = str_replace ('$ ' , ($ i + 1 ), $ arField ['NAME ' ]);
@@ -85,8 +78,9 @@ public function generate(int $countElements) {
8578 $ arField ['CODE ' ] = \CUtil::translit ($ arField ['NAME ' ], "ru " );
8679
8780 $ el = new \CIBlockElement ();
81+ $ productId = $ el ->Add ($ arField , false , true , false );
8882
89- if ($ productId = $ el -> Add ( $ arField , false , true , false ) ) {
83+ if ($ productId ) {
9084 $ this ->addSuccess ($ arField );
9185 } else {
9286 $ this ->addError ($ el ->LAST_ERROR . ' ON LINE ' . __LINE__ );
@@ -108,76 +102,6 @@ public function generate(int $countElements) {
108102 return $ this ->getResultData ();
109103 }
110104
111- /**
112- * Получить обработанный массив сгенерированных данных для элемента
113- *
114- * @return array
115- */
116- protected function getGeneratedPropertyArray (): array {
117-
118- $ arPropertyGenerated = [
119- 'PROPERTIES ' => [],
120- 'STANDART_PROPERTIES ' => []
121- ];
122-
123- if (!empty ($ this ->arPropertyGenerateRules )) {
124-
125- foreach ($ this ->arPropertyGenerateRules as $ propertyName => $ typeGenerator ) {
126-
127- $ isStandartProperties = in_array (str_replace (['* ' , '= ' ], ['' , '' ], $ propertyName ), static ::STANDART_PROPERTIES ) && ($ propertyName [0 ] == '* ' || $ propertyName [1 ] == '* ' );
128-
129- $ isDefaultValue = false ;
130-
131- if ($ isStandartProperties ) {
132- if ($ propertyName [0 ] === '= ' || $ propertyName [1 ] === '= ' ) {
133- $ isDefaultValue = true ;
134- }
135- } else {
136- if ($ propertyName [0 ] === '= ' ) {
137- $ isDefaultValue = true ;
138- }
139- }
140-
141- if (!is_array ($ typeGenerator )) {
142- $ arParams = explode ('( ' , str_replace (') ' , '' , $ typeGenerator ));
143- $ typeGenerator = array_shift ($ arParams );
144-
145- $ valuePropety = null ;
146-
147- if (!$ isDefaultValue ) {
148- $ valuePropety = $ this ->generateItem ($ typeGenerator , $ arParams );
149- } else {
150- $ valuePropety = $ typeGenerator ;
151- }
152- } else {
153-
154- if (sizeof ($ typeGenerator ) == 2 ) {
155-
156- if (is_numeric ($ typeGenerator [1 ]) && is_string ($ typeGenerator [0 ])) {
157-
158- $ count = $ typeGenerator [1 ];
159-
160- $ arParams = explode ('( ' , str_replace (') ' , '' , $ typeGenerator [0 ]));
161- $ typeGenerator = array_shift ($ arParams );
162-
163- $ valuePropety = $ this ->generateItem ($ typeGenerator , $ arParams , $ count );
164- }
165- }
166- }
167-
168- if ($ valuePropety !== null ) {
169- if ($ isStandartProperties ) {
170- $ arPropertyGenerated ['STANDART_PROPERTIES ' ][str_replace (['* ' , '= ' ], ['' , '' ], $ propertyName )] = $ valuePropety ;
171- } else {
172- $ arPropertyGenerated ['PROPERTIES ' ][str_replace (['* ' , '= ' ], ['' , '' ], $ propertyName )] = $ valuePropety ;
173- }
174- }
175- }
176- }
177-
178- return $ arPropertyGenerated ;
179- }
180-
181105 /**
182106 * Сгенерировать данные
183107 *
@@ -304,7 +228,9 @@ protected function generateItem(string $typeGenerator, array $arParams = [], int
304228 $ categoryPhoto = $ this ->getRandomCategoryPhoto ();
305229
306230 $ fileArray = [
307- 'VALUE ' => $ this ->generatePhotoFromLink ($ this ->dataGenerator ->imageUrl ($ width , $ height , $ categoryPhoto ))
231+ 'VALUE ' => $ this ->generatePhotoFromLink (
232+ 'https://loremflickr.com/ ' .$ width .'/ ' .$ height .'/ ' .$ categoryPhoto .'?salt= ' . \uniqid ()
233+ )
308234 ];
309235
310236 if (!empty ($ fileArray ['VALUE ' ]['tmp_name ' ])) {
@@ -313,7 +239,7 @@ protected function generateItem(string $typeGenerator, array $arParams = [], int
313239 }
314240 }
315241 } else {
316- $ linkImg = $ this -> dataGenerator -> imageUrl ( $ width, $ height, $ categoryPhoto );
242+ $ linkImg = ' https://loremflickr.com/ ' . $ width. ' / ' . $ height. ' / ' . $ categoryPhoto. ' ?salt= ' . \uniqid ( );
317243 $ valuePropety = $ this ->generatePhotoFromLink ($ linkImg );
318244 }
319245 break ;
@@ -330,7 +256,7 @@ protected function generateItem(string $typeGenerator, array $arParams = [], int
330256 if (!empty ($ arParams [0 ])) {
331257 $ arRand = explode (', ' , $ arParams [0 ]);
332258
333- $ arRand = array_map (function ($ el ) {
259+ $ arRand = array_map (function ($ el ) {
334260 return trim ($ el );
335261 }, $ arRand );
336262
0 commit comments