fork download
  1. <?php
  2. $templateData = [
  3. 0 => 'h',
  4. 1 => 1,
  5. 2 => 2,
  6. 3 => 3,
  7. 4 => 'c-1-3',
  8. 5 => 3,
  9. 6 => 5,
  10. 7 => 'c-2-3',
  11. 8 => 3,
  12. 9 => 'f',
  13. 10 => 2
  14. ];
  15.  
  16. $tmpKey = '';
  17. $tmpArray = [];
  18.  
  19. class Test
  20. {
  21. /**
  22.   * @var []
  23.   */
  24. private $data = [];
  25.  
  26. /**
  27.   * @var []
  28.   */
  29. private $columns = [];
  30.  
  31. /**
  32.   * @var array
  33.   */
  34. private $numOfColumns = 0;
  35.  
  36. /**
  37.   * @var []
  38.   */
  39. private $header = [];
  40.  
  41. /**
  42.   * @var []
  43.   */
  44. private $footer = [];
  45.  
  46. /**
  47.   * @var string
  48.   */
  49. private $current = [
  50. 'key' => '',
  51. 'matches' => ''
  52. ];
  53.  
  54. /**
  55.   * @var []
  56.   */
  57. private $rules = [
  58. 'header' => [
  59. 'expression' => 'h',
  60. 'handler' => 'headerHandler',
  61. 'matches' => false
  62. ],
  63. 'footer' => [
  64. 'expression' => 'f',
  65. 'handler' => 'footerHandler',
  66. 'matches' => false
  67. ],
  68. 'columns' => [
  69. 'expression' => 'c-\d+-\d+',
  70. 'handler' => 'columnHandler',
  71. 'matches' => "columnMatches"
  72. ]
  73. ];
  74.  
  75. /**
  76.   * @param $data
  77.   */
  78. public function __construct($data)
  79. {
  80. $this->data = $data;
  81. $this->process();
  82. }
  83.  
  84. /**
  85.   * @throws Exception
  86.   */
  87. private function process()
  88. {
  89. foreach($this->data as $data) {
  90.  
  91. if (!is_int($data)) {
  92. if (!$this->setRule($data)) {
  93. throw new \RuntimeException("Rule Not found.");
  94. }
  95. } else {
  96. $this->handleRule($data);
  97. }
  98. }
  99. }
  100.  
  101. /**
  102.   * @param $data
  103.   */
  104. private function headerHandler($data)
  105. {
  106. $this->header[] = $data;
  107. }
  108.  
  109. /**
  110.   * @param $data
  111.   */
  112. private function footerHandler($data)
  113. {
  114. $this->footer[] = $data;
  115. }
  116.  
  117. /**
  118.   * @param $data
  119.   */
  120. private function columnHandler($data)
  121. {
  122. $columns = explode('-', $this->current['matches']);
  123. if ($this->columns[($columns[1] - 1)][0] == 0){
  124. $this->columns[($columns[1] - 1)][0] = $data;
  125. } else {
  126. array_push($this->columns[($columns[1] - 1)], $data);
  127. }
  128. }
  129.  
  130. /**
  131.   * @param $data
  132.   */
  133. private function columnMatches($data)
  134. {
  135. $columns = explode('-', $data);
  136.  
  137. if ($this->numOfColumns == 0) {
  138. $this->numOfColumns = end($columns);
  139. $this->columns = array_fill(0, $this->numOfColumns, [0]);
  140. }
  141. }
  142.  
  143. /**
  144.   * @param $data
  145.   * @return bool
  146.   */
  147. private function setRule($data)
  148. {
  149. foreach($this->rules as $key => $rules) {
  150. if (preg_match("/^{$rules['expression']}$/", $data, $matches)) {
  151. if ($rules['matches']) {
  152. $this->{$rules['matches']}($data);
  153. }
  154. $this->current['key'] = $key;
  155. $this->current['matches'] = $data;
  156. return true;
  157. }
  158. }
  159.  
  160. return false;
  161. }
  162.  
  163. /**
  164.   * @return mixed
  165.   */
  166. public function getHeader()
  167. {
  168. return $this->header;
  169. }
  170.  
  171. /**
  172.   * @return mixed
  173.   */
  174. public function getColumns()
  175. {
  176. return $this->columns;
  177. }
  178.  
  179. /**
  180.   * @return mixed
  181.   */
  182. public function getFooter()
  183. {
  184. return $this->footer;
  185. }
  186.  
  187. /**
  188.   * @return array
  189.   */
  190. public function getResult()
  191. {
  192. return [
  193. 0 => [$this->header],
  194. 1 => $this->columns,
  195. 2 => $this->footer
  196. ];
  197. }
  198.  
  199. /**
  200.   * @param $data
  201.   */
  202. private function handleRule($data)
  203. {
  204. $this->{$this->rules[$this->current['key']]['handler']}($data);
  205. }
  206.  
  207. }
  208.  
  209. $test = new Test($templateData);
  210. echo '<h1>Header</h1>';
  211. var_dump($test->getHeader());
  212. echo '<h1>Footer</h1>';
  213. var_dump($test->getFooter());
  214. echo '<h1>Columns</h1>';
  215. var_dump($test->getColumns());
  216. echo '<h1>Result</h1>';
  217. var_dump($test->getResult());exit;
Success #stdin #stdout 0.02s 24400KB
stdin
Standard input is empty
stdout
<h1>Header</h1>array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}
<h1>Footer</h1>array(1) {
  [0]=>
  int(2)
}
<h1>Columns</h1>array(3) {
  [0]=>
  array(2) {
    [0]=>
    int(3)
    [1]=>
    int(5)
  }
  [1]=>
  array(1) {
    [0]=>
    int(3)
  }
  [2]=>
  array(1) {
    [0]=>
    int(0)
  }
}
<h1>Result</h1>array(3) {
  [0]=>
  array(1) {
    [0]=>
    array(3) {
      [0]=>
      int(1)
      [1]=>
      int(2)
      [2]=>
      int(3)
    }
  }
  [1]=>
  array(3) {
    [0]=>
    array(2) {
      [0]=>
      int(3)
      [1]=>
      int(5)
    }
    [1]=>
    array(1) {
      [0]=>
      int(3)
    }
    [2]=>
    array(1) {
      [0]=>
      int(0)
    }
  }
  [2]=>
  array(1) {
    [0]=>
    int(2)
  }
}