Request.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php namespace Api\Lib\Platform;
  2. use Dever;
  3. class Request
  4. {
  5. public function init($field, $platform_id, $type, $type_id)
  6. {
  7. $this->field = $field;
  8. $this->platform_id = $platform_id;
  9. $this->type = $type;
  10. $this->type_id = $type_id;
  11. return $this;
  12. }
  13. public function body()
  14. {
  15. $body = [];
  16. $this->field->setBody($body);
  17. $this->load($body, 'body');
  18. $this->field->setBodyJson($body ? Dever::json_encode($body) : '');
  19. return $body;
  20. }
  21. public function header()
  22. {
  23. $header = [];
  24. $this->field->setHeader($header);
  25. $this->load($header, 'header');
  26. $this->field->setHeaderJson($header ? Dever::json_encode($header) : '');
  27. return $header;
  28. }
  29. protected function load(&$data, $type)
  30. {
  31. $this->get($data, 'platform', $type, ['platform_id' => $this->platform_id]);
  32. $this->get($data, $this->type, $type, [$this->type . '_id' => $this->type_id]);
  33. }
  34. protected function get(&$data, $prefix, $type, $where)
  35. {
  36. $request = Dever::db('api/' . $prefix . '_request_' . $type)->select($where);
  37. if ($request) {
  38. foreach ($request as $k => $v) {
  39. $value = $this->field->value($v['value'], $v['type']);
  40. if ($value) {
  41. if (strstr($v['key'], '.')) {
  42. $keys = explode('.', $v['key']);
  43. $temp = &$data;
  44. foreach ($keys as $key) {
  45. $temp = &$temp[$key];
  46. }
  47. $temp = $value;
  48. } else {
  49. $data[$v['key']] = $value;
  50. }
  51. $this->field->set($v['key'], $value);
  52. $this->field->add($v['key'], $value, $type);
  53. }
  54. }
  55. }
  56. }
  57. }