/** * carves mail data by tags and returns * tagged array * * @param MailPart $mail * * return mail object of type you wish * * @throws \Exception * @return Array */ public function carveData(MailPart $mail) { // prn($mail->dataGetters); // prn($mail); // prn($mail->dataUniters); // exit; $resArray = []; if (isset($mail)) { $resArray = $mail->getData(); $configurators = []; if (is_array($resArray)) { $configurators = MailConvert::getConfigurators(); } foreach ($resArray as $tag => $part) { // prn($tag,$resArray[$tag]); try { $resArray[$tag] = $configurators[$tag]->configure($resArray[$tag]); } catch (\Exception $ex) { $exceptionMessage = isset($configurators[$tag]) ? 'Internal error in ' . get_class($configurators[$tag]) . ' configurator for mail tag ' . $tag : 'Configurator for tag ' . $tag . ' doesn\'t set'; throw new \Exception($exceptionMessage); } } } // prn($resArray); // exit; return $resArray; }
/** * @param string $contentType * * @return MailPart */ private function configurePart($contentType) { // prn($contentType); $contentType = explode('/', $contentType); $part = null; if (isset($contentType[0]) && in_array($contentType[0], array_keys($this::$typeSettings))) { if (isset($contentType[1]) && isset($this::$typeSettings[$contentType[0]][$contentType[1]])) { $setting = $this::$typeSettings[$contentType[0]][$contentType[1]]; $part = new MailPart($setting['type'], $contentType); } elseif (isset($this::$typeSettings[$contentType[0]]['default'])) { $setting = $this::$typeSettings[$contentType[0]]['default']; $part = new MailPart($setting['type'], $contentType); } if (isset($setting)) { if ($setting['type'] == MailPart::DATA_PART_TYPE) { // foreach($setting['data_tags'] as $tag) // { // $Tags = $this::$Tags; // $tagSetting = $Tags[$setting['type']][$tag]; // $dataGetter = new $tagSetting['data_getter']( ['tag'=>$tag] ); // $part->addDataGetter($dataGetter); // } foreach ($setting['parser'] as $parser) { $Parsers = self::$BodyParsers; $parser = new $Parsers[$parser](); $part->addParser($parser); } } elseif ($setting['type'] == MailPart::COMBINER_PART_TYPE) { $iterator = array_keys($setting['iterator'])[0]; $iteratorSettings = $setting['iterator'][$iterator]; $Iterators = $this::$PartIterators; $iterator = new $Iterators[$iterator]($iteratorSettings); $part->setIterator($iterator); // foreach($setting['data_tags'] as $tag) // { // $Tags = $this::$Tags; // $tagSetting = $Tags[$setting['type']][$tag]; // $dataUniter = new $tagSetting['data_uniter'](); // $part->addDataUniter($tag, $dataUniter); // } } foreach ($setting['data_tags'] as $tag) { $Tags = $this::$Tags; // prn($contentType,$Tags,$setting['type'],$tag); // if(isset($Tags[$setting['type']][$tag])) // { // $tagSetting = $Tags[$setting['type']][$tag]; // } // elseif(isset($Tags['common'][$tag])) // { // $tagSetting = $Tags['common'][$tag]; // } // else // { // throw new \Exception( // 'Tags settings are wrong. Mail service can\'t find setting for tag '.$tag.' either in common or '.$setting['type'].' section.' // ); // } // switch($setting['type']) // { // case MailPart::COMBINER_PART_TYPE: // $dataUniter = new $tagSetting['data_uniter'](); // $part->addDataUniter($tag, $dataUniter); // case MailPart::DATA_PART_TYPE: // $dataGetter = new $tagSetting['data_getter']( ['tag'=>$tag] ); // $part->addDataGetter($dataGetter); // break; // } $tagSetting = $Tags[$tag]; if ($setting['type'] == MailPart::COMBINER_PART_TYPE) { $dataUniter = new $tagSetting['data_uniter'](); $part->addDataUniter($tag, $dataUniter); } if (in_array($tagSetting['data_getter']['part_type'], [MailPart::COMMON_PART_TYPE, $setting['type']])) { $dataGetter = new $tagSetting['data_getter']['class'](['tag' => $tag]); $part->addDataGetter($dataGetter); } } } } return $part; }