44
55namespace Paneon \VueToTwig \Utils ;
66
7+ use Exception ;
8+ use Paneon \VueToTwig \Models \Concat ;
79use Paneon \VueToTwig \Models \Property ;
810use Paneon \VueToTwig \Models \Replacements ;
911use ReflectionException ;
@@ -18,6 +20,11 @@ class TwigBuilder
1820 */
1921 protected $ options ;
2022
23+ /**
24+ * @var Concat[]
25+ */
26+ protected $ concat = [];
27+
2128 /**
2229 * TwigBuilder constructor.
2330 *
@@ -231,8 +238,6 @@ private function refactorConditionPart(string $condition): string
231238 $ condition = str_replace ('.length ' , '|length ' , $ condition );
232239 $ condition = str_replace ('.trim ' , '|trim ' , $ condition );
233240
234- // $condition = $this->convertConcat($condition);
235-
236241 foreach (Replacements::getConstants () as $ constant => $ value ) {
237242 $ condition = str_replace ($ value , Replacements::getSanitizedConstant ($ constant ), $ condition );
238243 }
@@ -279,33 +284,6 @@ public function refactorTextNode(string $content): string
279284 return $ refactoredContent ;
280285 }
281286
282- public function convertConcat (string $ content ): string
283- {
284- if (preg_match_all ('/(\S*)(\s*\+\s*(\S+))+/ ' , $ content , $ matches , PREG_SET_ORDER )) {
285- foreach ($ matches as $ match ) {
286- $ parts = explode ('+ ' , $ match [0 ]);
287- $ lastPart = null ;
288- $ convertedContent = '' ;
289- foreach ($ parts as $ part ) {
290- $ part = trim ($ part );
291- if ($ lastPart !== null ) {
292- if (is_numeric ($ lastPart ) && is_numeric ($ part )) {
293- $ convertedContent .= ' + ' . $ part ;
294- } else {
295- $ convertedContent .= ' ~ ' . $ part ;
296- }
297- } else {
298- $ convertedContent = $ part ;
299- }
300- $ lastPart = $ part ;
301- }
302- $ content = str_replace ($ match [0 ], $ convertedContent , $ content );
303- }
304- }
305-
306- return $ content ;
307- }
308-
309287 private function convertTemplateString (string $ content ): string
310288 {
311289 if (preg_match_all ('/`([^`]+)`/ ' , $ content , $ matches , PREG_SET_ORDER )) {
@@ -340,4 +318,111 @@ public function prepareBindingOutput(string $value, bool $twigOutput = true): st
340318
341319 return $ open . ' ' . $ value . ' ' . $ close ;
342320 }
321+
322+ /**
323+ * @param Property[] $properties
324+ *
325+ * @throws Exception
326+ */
327+ public function concatConvertHandler (string $ content , array $ properties ): string
328+ {
329+ preg_match_all ('/{{(.*?)}}/sm ' , $ content , $ outputTerms );
330+ foreach ($ outputTerms [1 ] as $ outputTerm ) {
331+ $ oldOutputTerm = $ outputTerm ;
332+
333+ while (preg_match ('/\(([^()]*)\)/ ' , $ outputTerm , $ chambersTerm )) {
334+ $ outputTerm = str_replace (
335+ '( ' . $ chambersTerm [1 ] . ') ' ,
336+ $ this ->convertConcat ($ chambersTerm [1 ], $ properties ),
337+ $ outputTerm
338+ );
339+ }
340+
341+ $ concat = $ this ->replaceConcatCharacter ($ outputTerm , $ properties );
342+ $ newOutputTerm = $ concat ->getValue () ?? '' ;
343+
344+ while (preg_match_all (Concat::CONCAT_REGEX , $ newOutputTerm , $ chambersTerms )) {
345+ foreach ($ chambersTerms [0 ] as $ chambersTerm ) {
346+ $ newOutputTerm = str_replace (
347+ $ chambersTerm ,
348+ $ this ->concat [$ chambersTerm ]->getValue (),
349+ $ newOutputTerm
350+ );
351+ }
352+ }
353+
354+ $ content = str_replace ('{{ ' . $ oldOutputTerm . '}} ' , '{{ ' . $ newOutputTerm . '}} ' , $ content );
355+
356+ $ this ->concat = [];
357+ }
358+
359+ return $ content ;
360+ }
361+
362+ /**
363+ * @param Property[] $properties
364+ *
365+ * @throws Exception
366+ */
367+ private function convertConcat (string $ content , array $ properties ): string
368+ {
369+ $ concat = $ this ->replaceConcatCharacter ($ content , $ properties );
370+
371+ $ concat ->setValue ('( ' . $ concat ->getValue () . ') ' );
372+
373+ $ concatId = $ concat ->getConcatContentVariableString ();
374+ $ this ->concat [$ concatId ] = $ concat ;
375+
376+ return $ concatId ;
377+ }
378+
379+ /**
380+ * @param Property[] $properties
381+ *
382+ * @throws Exception
383+ */
384+ private function replaceConcatCharacter (string $ content , array $ properties ): Concat
385+ {
386+ $ parts = explode ('+ ' , $ content );
387+ $ isNumericConcat = true ;
388+
389+ foreach ($ parts as $ part ) {
390+ $ isNumericConcat = $ isNumericConcat && $ this ->isNumeric ($ part , $ properties );
391+ }
392+
393+ return new Concat (
394+ implode ($ isNumericConcat ? '+ ' : '~ ' , $ parts ),
395+ $ isNumericConcat
396+ );
397+ }
398+
399+ /**
400+ * @param Property[] $properties
401+ */
402+ private function isNumeric (string $ value , array $ properties ): bool
403+ {
404+ $ value = trim ($ value );
405+
406+ foreach ($ properties as $ property ) {
407+ if (strtolower ($ property ->getName ()) === strtolower ($ value )) {
408+ return $ property ->getType () && strtolower ($ property ->getType ()) === 'number ' ;
409+ }
410+ }
411+
412+ if (isset ($ this ->concat [$ value ])) {
413+ return $ this ->concat [$ value ]->isNumeric ();
414+ }
415+
416+ $ mathematicsParts = preg_split ('/[-*\/%]+/ ' , $ value );
417+ if (count ($ mathematicsParts ) > 1 ) {
418+ $ isNumeric = true ;
419+ foreach ($ mathematicsParts as $ mathematicsPart ) {
420+ $ isNumeric = $ isNumeric && $ this ->isNumeric ($ mathematicsPart , $ properties );
421+ }
422+
423+ return $ isNumeric ;
424+ }
425+
426+ return is_numeric ($ value );
427+ }
343428}
0 commit comments