Request.php 2.0 KB

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