流程控制

Testing Is Documentation

tests/Flow/FlowControlTest.php流程控制 - 图1

QueryPHP 为流程控制类统一抽象了一个基础流程控制类 \Leevel\Flow\FlowControl,流程控制类可以轻松接入。

系统一些关键服务,比如说数据库查询条件、HTTP 响应等流程控制类均接入了统一的抽象层。

Uses

  1. <?php
  2. use Leevel\Flow\FlowControl;

基础使用方法

fixture 定义

Tests\Flow\Test1

  1. namespace Tests\Flow;
  2. class Test1
  3. {
  4. use FlowControl;
  5. protected $value = [];
  6. public function __call(string $method, array $args)
  7. {
  8. }
  9. public function value()
  10. {
  11. return implode(' ', $this->value);
  12. }
  13. public function condition1()
  14. {
  15. if ($this->checkFlowControl()) {
  16. return $this;
  17. }
  18. $this->value[] = 'condition1';
  19. return $this;
  20. }
  21. public function condition2()
  22. {
  23. if ($this->checkFlowControl()) {
  24. return $this;
  25. }
  26. $this->value[] = 'condition2';
  27. return $this;
  28. }
  29. public function condition3()
  30. {
  31. if ($this->checkFlowControl()) {
  32. return $this;
  33. }
  34. $this->value[] = 'condition3';
  35. return $this;
  36. }
  37. public function condition4()
  38. {
  39. if ($this->checkFlowControl()) {
  40. return $this;
  41. }
  42. $this->value[] = 'condition4';
  43. return $this;
  44. }
  45. public function condition5()
  46. {
  47. if ($this->checkFlowControl()) {
  48. return $this;
  49. }
  50. $this->value[] = 'condition5';
  51. return $this;
  52. }
  53. }
  1. public function testBaseUse(): void
  2. {
  3. $test = new Test1();
  4. $this->assertSame('', $test->value());
  5. $condition1 = 1;
  6. $value = $test
  7. ->if($condition1)
  8. ->condition1()
  9. ->else()
  10. ->condition2()
  11. ->fi()
  12. ->value();
  13. $this->assertSame('condition1', $value);
  14. }

else 条件语句

  1. public function testElse(): void
  2. {
  3. $test = new Test1();
  4. $condition1 = 0;
  5. $value = $test
  6. ->if($condition1)
  7. ->condition1()
  8. ->else()
  9. ->condition2()
  10. ->fi()
  11. ->value();
  12. $this->assertSame('condition2', $value);
  13. }

else 条件语句例子

测试例子

  1. # Tests\Flow\FlowControlTest::getElseData
  2. public function getElseData()
  3. {
  4. return [
  5. [0, 'condition1 condition5'],
  6. [1, 'condition2 condition5'],
  7. [2, 'condition3 condition5'],
  8. [3, 'condition4'], // else 仅能根据上一次的 elif 或 if 来做判断,这里为 elif(3 === $condition)
  9. [4, 'condition5'],
  10. [5, 'condition5'],
  11. [6, 'condition5'],
  12. ];
  13. }
  1. public function testElseMulti(int $condition, string $result): void
  2. {
  3. $test = new Test1();
  4. $value = $test
  5. ->if(0 === $condition)
  6. ->condition1()
  7. ->elif(1 === $condition)
  8. ->condition2()
  9. ->elif(2 === $condition)
  10. ->condition3()
  11. ->elif(3 === $condition)
  12. ->condition4()
  13. ->else() // else 仅能根据上一次的 elif 或 if 来做判断,这里为 elif(3 === $condition)
  14. ->condition5()
  15. ->fi()
  16. ->value();
  17. $this->assertSame($result, $value);
  18. }

elif 条件语句

  1. public function testElseIfs(): void
  2. {
  3. $test = new Test1();
  4. $condition1 = 1;
  5. $value = $test
  6. ->if(0 === $condition1)
  7. ->condition1()
  8. ->elif(1 === $condition1)
  9. ->condition2()
  10. ->fi()
  11. ->value();
  12. $this->assertSame('condition2', $value);
  13. }