Value.php 4.1 KB

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