Request.php 1.9 KB

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