首页 > 解决方案 > 哪个是使用 PHP (8) 检查用户在 Ussd 程序中选择的选项的更好方法

问题描述

我正在使用非洲跟踪测试环境在 PHP(版本 8)中构建一个简单的基于 USSD 的支付网关

我不知道哪个是检查当前用户选择或与之交互的级别或选项的更好方法

我目前的代码..

     public function handleSession(Request $request)
    {
       $text=$request->input('text');
       // We split the response(text) to an array
      // It helps us to check which option the current user selects
  
       $level = explode("*", $text);
        // Initially, the response is an empty string
       if ( $text == "" ) {
         // We send our ussd menu
           $response = $this->welcomeInterface();
       }
       // If user selects the first option from the menu (first level menu)
       
       if(isset($level[0]) && $level[0] == 1 && !isset($level[1]))
       {
           $response = $this->buyerOptionsInterface();
       }
        // If user select the second option from the menu
       if(isset($level[0]) && $level[0] == 2 && !isset($level[1]))
       {
           $response = $this->sellerOptionsInterface();
       }
      // If user select the third option from the menu (last option)
       if(isset($level[0]) && $level[0] == 3  && !isset($level[1]))
       {
           $response= $this->termsAndConditionsInterface();
          
       }

       // If user selects the first option from our first level menu, and first option of second level menu 
       if(isset($level[0]) && $level[0] == 1 && isset($level[1]) && $level[1] == 1 && !isset($level[2])) {
           $response = $this->insertSellerPhoneNumber();
       }

      // If user select the first option of first level menu,and first option of second level menu and first option of third level menu
       if(isset($level[0]) && $level[0] == 1 && isset($level[1]) && $level[1] == 1 && isset($level[2]) && $level[2] == 1 && !isset($level[3])) {
         $response = $this->insertPrice();
       }

           header('Content-type: text/plain');
           echo $response;
   }

代码有效,但是当用户被说到第五层时,它们变得更难阅读

哪个是重构上述代码的更好方法,框架是 Laravel (Version 8)

标签: php

解决方案


推荐阅读