Value.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php namespace Api\Lib\Platform;
  2. use Dever;
  3. class Value
  4. {
  5. private $field;
  6. public function init($field)
  7. {
  8. $this->field = $field;
  9. return $this;
  10. }
  11. public function get($config, $data)
  12. {
  13. $result = [];
  14. if ($config) {
  15. foreach ($data as $k => $v) {
  16. $this->field->set($k, $v);
  17. }
  18. $source = [];
  19. $dest = [];
  20. foreach ($config as $k => $v) {
  21. $temp = $this->convert($data, $v['value'], $v['key'], $v['type']);
  22. if ($temp) {
  23. $result = array_replace_recursive($result, $temp);
  24. }
  25. }
  26. }
  27. if ($result) {
  28. $result = $this->value($result);
  29. } else {
  30. $result = $data;
  31. }
  32. return $result;
  33. }
  34. public function convert($array, $source, $dest, $type = '')
  35. {
  36. $source = explode('.', $source);
  37. $dest = explode('.', $dest);
  38. $extracted = $this->extracted($array, $source, $type);
  39. return $this->transform($extracted, $dest);
  40. }
  41. public function extracted(&$array, $source, $type = '')
  42. {
  43. $current = array_shift($source);
  44. if (substr($current, -2) == '[]') {
  45. $current = substr($current, 0, -2);
  46. $result = [];
  47. if (isset($array[$current]) && is_array($array[$current])) {
  48. foreach ($array[$current] as $item) {
  49. $sub = $this->extracted($item, $source, $type);
  50. if ($sub !== null) {
  51. $result[] = $sub;
  52. }
  53. }
  54. }
  55. return $result;
  56. } else {
  57. $result = '';
  58. if (isset($array[$current])) {
  59. if (empty($source)) {
  60. $result = $array[$current];
  61. } else {
  62. return $this->extracted($array[$current], $source, $type);
  63. }
  64. } elseif ($this->field->$current) {
  65. $result = $this->field->$current;
  66. } else {
  67. $result = $current;
  68. }
  69. if ($type) {
  70. $result .= '||' . $type;
  71. }
  72. return $result;
  73. }
  74. return null;
  75. }
  76. protected function transform($value, $dest)
  77. {
  78. $current = array_shift($dest);
  79. if (substr($current, -2) == '[]') {
  80. $current = substr($current, 0, -2);
  81. $result = [];
  82. $result[$current] = [];
  83. foreach ($value as $item) {
  84. $result[$current][] = $this->transform($item, $dest);
  85. }
  86. return $result;
  87. } else {
  88. if (empty($dest)) {
  89. return [$current => $value];
  90. } else {
  91. return [$current => $this->transform($value, $dest)];
  92. }
  93. }
  94. }
  95. protected function value($data)
  96. {
  97. if (!is_array($data)) {
  98. return $data;
  99. }
  100. foreach ($data as $k => $v) {
  101. if (!is_array($v)) {
  102. $temp = explode('||', $v);
  103. $this->field->set($k, $temp[0]);
  104. }
  105. }
  106. foreach ($data as $k => $v) {
  107. if (is_array($v)) {
  108. if (isset($v[0])) {
  109. foreach ($v as $k1 => $v1) {
  110. $data[$k][$k1] = $this->value($v1);
  111. }
  112. } else {
  113. $data[$k] = $this->value($v, $key);
  114. }
  115. } else {
  116. $temp = explode('||', $v);
  117. if (empty($temp[1])) {
  118. $temp[1] = '';
  119. }
  120. $state = false;
  121. # 临时特殊处理,以后封装
  122. if (strstr($temp[0], '\n')) {
  123. $array = explode('\n', $temp[0]);
  124. foreach ($array as &$v1) {
  125. if (isset($data[$v1])) {
  126. $v1 = $data[$v1];
  127. }
  128. }
  129. $temp[0] = implode("\n", $array);
  130. $state = true;
  131. } elseif (strstr($temp[0], '+')) {
  132. $array = explode('+', $temp[0]);
  133. foreach ($array as &$v1) {
  134. if (isset($data[$v1])) {
  135. $v1 = $data[$v1];
  136. }
  137. }
  138. $temp[0] = implode('', $array);
  139. $state = true;
  140. }
  141. $data[$k] = $this->field->value($temp[0], $temp[1], $state);
  142. $this->field->set($k, $data[$k]);
  143. }
  144. }
  145. return $data;
  146. }
  147. }