Value.php 4.2 KB

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