<?php
 namespace Modules\Utils\GdsProfileFill; class MergeRemarks { private $remarkType; private $originalRemarks; private $remarksVars; public function __construct($remarkType, $originalRemarks, array $remarksVars) { $this->remarkType = $remarkType; $this->originalRemarks = $originalRemarks; $this->remarksVars = $remarksVars; $remarksCfg = \App\Config\Remarks::getInstance(); if (!in_array($this->remarkType, $remarksCfg->getValidRemarkTypes())) { throw new \Exception("The remarks type is not valid"); } } public function getMergedRemarks() { if (!$this->remarksVars) { return $this->originalRemarks; } $originalRemarksLines = explodeClean("\n", $this->originalRemarks); $newRemarksLines = []; foreach ($originalRemarksLines as $rmLine) { $remarkVars = \App\Config\Remarks::process($rmLine, $this->remarkType); if ( !$remarkVars or !array_intersect(array_keys($remarkVars), array_keys($this->remarksVars)) ) { $newRemarksLines[] = trim($rmLine); } } $remarkVars = $this->remarksVars; foreach (\App\Config\Remarks::getInstance()->getAllRemarksFromCategory('service') as $remarkFormat) { $replaceValues = []; $allVarsFound = true; $cleanRmVars = []; foreach($remarkFormat['vars'] as $varDet) { $currentRmVar = $varDet['name']; if ( array_key_exists($currentRmVar, $remarkVars) and strlen($remarkVars[$currentRmVar]) ) { $replaceValues['['.$currentRmVar.']'] = $remarkVars[$currentRmVar]; $cleanRmVars[] = $currentRmVar; } else { $allVarsFound = false; } } if ($replaceValues and $allVarsFound) { $newRemarksLines[] = str_replace(array_keys($replaceValues), array_values($replaceValues), $remarkFormat['format']); foreach($cleanRmVars as $rmVar) { unset($remarkVars[$rmVar]); } } } return implode("\r\n", $newRemarksLines); } } 