[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/Configurator/RendererGenerators/PHP/ -> SwitchStatement.php (source)

   1  <?php
   2  
   3  /**
   4  * @package   s9e\TextFormatter
   5  * @copyright Copyright (c) 2010-2022 The s9e authors
   6  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
   7  */
   8  namespace s9e\TextFormatter\Configurator\RendererGenerators\PHP;
   9  
  10  class SwitchStatement
  11  {
  12      /**
  13      * @var array Dictionary of [value => php code]
  14      */
  15      protected $branchesCode;
  16  
  17      /**
  18      * @var string PHP code for the default case
  19      */
  20      protected $defaultCode;
  21  
  22      /**
  23      * @param array  $branchesCode Dictionary of [value => php code]
  24      * @param string $defaultCode  PHP code for the default case
  25      */
  26  	public function __construct(array $branchesCode, $defaultCode = '')
  27      {
  28          ksort($branchesCode);
  29  
  30          $this->branchesCode = $branchesCode;
  31          $this->defaultCode  = $defaultCode;
  32      }
  33  
  34      /**
  35      * Create and return the source code for a switch statement
  36      *
  37      * @param  string $expr         Expression used for the switch clause
  38      * @param  array  $branchesCode Dictionary of [value => php code]
  39      * @param  string $defaultCode  PHP code for the default case
  40      * @return string               PHP code
  41      */
  42  	public static function generate($expr, array $branchesCode, $defaultCode = '')
  43      {
  44          $switch = new static($branchesCode, $defaultCode);
  45  
  46          return $switch->getSource($expr);
  47      }
  48  
  49      /**
  50      * Return the source code for this switch statement
  51      *
  52      * @param  string $expr Expression used for the switch clause
  53      * @return string       PHP code
  54      */
  55  	protected function getSource($expr)
  56      {
  57          $php = 'switch(' . $expr . '){';
  58          foreach ($this->getValuesPerCodeBranch() as $branchCode => $values)
  59          {
  60              foreach ($values as $value)
  61              {
  62                  $php .= 'case' . var_export((string) $value, true) . ':';
  63              }
  64              $php .= $branchCode . 'break;';
  65          }
  66          if ($this->defaultCode > '')
  67          {
  68              $php .= 'default:' . $this->defaultCode;
  69          }
  70          $php = preg_replace('(break;$)', '', $php) . '}';
  71  
  72          return $php;
  73      }
  74  
  75      /**
  76      * Group branches by their content and return the switch values for each branch
  77      *
  78      * @return array
  79      */
  80  	protected function getValuesPerCodeBranch()
  81      {
  82          $values = [];
  83          foreach ($this->branchesCode as $value => $branchCode)
  84          {
  85              $values[$branchCode][] = $value;
  86          }
  87  
  88          return $values;
  89      }
  90  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1