123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- <?php namespace Api\Lib\Platform;
- use Dever;
- class Request
- {
- public function __construct($field, $platform_id, $type, $type_id)
- {
- $this->field = $field;
- $this->platform_id = $platform_id;
- $this->type = $type;
- $this->type_id = $type_id;
- }
- public function body()
- {
- $body = array();
- $this->field->setBody($body);
- $this->load($body, 'body');
- $this->field->setBodyJson($body ? Dever::json_encode($body) : '');
- return $body;
- }
- public function header()
- {
- $header = array();
- $this->field->setHeader($header);
- $this->load($header, 'header');
- $this->field->setHeaderJson($header ? Dever::json_encode($header) : '');
- return $header;
- }
- protected function load(&$data, $type)
- {
- $this->get($data, 'platform', $type, array('platform_id' => $this->platform_id));
- $this->get($data, $this->type, $type, array($this->type . '_id' => $this->type_id));
- }
- protected function get(&$data, $prefix, $type, $where)
- {
- $request = Dever::db($prefix . '_request_' . $type, 'api')->select($where);
- if ($request) {
- foreach ($request as $k => $v) {
- $value = $this->field->value($v['value'], $v['type']);
- if ($value) {
- if (strstr($v['key'], '.')) {
- $keys = explode('.', $v['key']);
- $temp = &$data;
- foreach ($keys as $key) {
- $temp = &$temp[$key];
- }
- $temp = $value;
- } else {
- $data[$v['key']] = $value;
- }
- $this->field->set($v['key'], $value);
- $this->field->add($v['key'], $value, $type);
- }
- }
- }
- }
- }
|