Value.php 4.8 KB

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