Value.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. $default = $source;
  37. $source = explode('.', $source);
  38. $dest = explode('.', $dest);
  39. $extracted = $this->extracted($array, $source, $default, $type);
  40. return $this->transform($extracted, $dest);
  41. }
  42. public function extracted(&$array, $source, $default, $type = '')
  43. {
  44. $current = array_shift($source);
  45. if (substr($current, -2) == '[]') {
  46. $current = substr($current, 0, -2);
  47. $result = [];
  48. if (isset($array[$current]) && is_array($array[$current])) {
  49. foreach ($array[$current] as $item) {
  50. $sub = $this->extracted($item, $source, $default, $type);
  51. if ($sub !== null) {
  52. $result[] = $sub;
  53. }
  54. }
  55. }
  56. return $result;
  57. } else {
  58. $result = '';
  59. if (isset($array[$current])) {
  60. if (empty($source)) {
  61. $result = $array[$current];
  62. } else {
  63. return $this->extracted($array[$current], $source, $default, $type);
  64. }
  65. } elseif ($this->field->$current) {
  66. $result = $this->field->$current;
  67. } else {
  68. $result = $default;
  69. }
  70. if ($type) {
  71. $result .= '||' . $type;
  72. }
  73. return $result;
  74. }
  75. return null;
  76. }
  77. protected function transform($value, $dest)
  78. {
  79. $current = array_shift($dest);
  80. if (substr($current, -2) == '[]') {
  81. $current = substr($current, 0, -2);
  82. $result = [];
  83. $result[$current] = [];
  84. foreach ($value as $item) {
  85. $result[$current][] = $this->transform($item, $dest);
  86. }
  87. return $result;
  88. } else {
  89. if (empty($dest)) {
  90. return [$current => $value];
  91. } else {
  92. return [$current => $this->transform($value, $dest)];
  93. }
  94. }
  95. }
  96. protected function value($data)
  97. {
  98. if (!is_array($data)) {
  99. return $data;
  100. }
  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. $value = $temp[0];
  123. $type = $temp[1];
  124. if (strstr($value, '.')) {
  125. $value = $this->extracted($data, explode('.', $value), $value);
  126. }
  127. $state = false;
  128. # 临时特殊处理,以后封装
  129. if (strstr($value, '\n')) {
  130. $array = explode('\n', $value);
  131. foreach ($array as &$v1) {
  132. if (isset($data[$v1])) {
  133. $v1 = $data[$v1];
  134. }
  135. }
  136. $value = implode("\n", $array);
  137. $state = true;
  138. }
  139. $data[$k] = $this->field->value($value, $type, $state);
  140. if (!$state && strpos($data[$k], '{') === 0) {
  141. $data[$k] = Dever::json_decode($data[$k]);
  142. }
  143. $this->field->set($k, $data[$k]);
  144. }
  145. }
  146. return $data;
  147. }
  148. }