123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <?php namespace Api\Lib\Platform;
- use Dever;
- class Field
- {
- private $data = array();
- public $ssl;
- public function __construct($data)
- {
- $this->data = $data;
- $this->ssl = new Ssl($this);
- $this->set('time', time());
- $this->set('timestamp', \Dever\Helper\Secure::timestamp());
- $this->set('nonce', \Dever\Helper\Secure::nonce());
- }
- public function set($key, $value)
- {
- $this->data[$key] = $value;
- }
- public function __set($key, $value)
- {
- $this->data[$key] = $value;
- }
- public function __get($key)
- {
- if (array_key_exists($key, $this->data)) {
- return $this->data[$key];
- } else {
- return null;
- }
- }
- public function setHost($value)
- {
- $this->set('body', $value);
- }
- public function setUri($value)
- {
- $this->set('uri', $value);
- }
- public function setNotify($value)
- {
- $this->set('notify', $value);
- }
- public function setSign($value)
- {
- $this->set('sign', $value);
- }
- public function setMethod($value)
- {
- $this->set('method', $value);
- }
- public function setBody($value)
- {
- $this->set('body', $value);
- }
- public function setBodyJson($value)
- {
- $this->set('body_json', $value);
- }
- public function setHeader($value)
- {
- $this->set('header', $value);
- }
- public function setNumber($value)
- {
- $this->set('number', $value);
- }
- public function value($value, $type = -1, $state = true)
- {
- $value = trim($value);
- if ($this->data && isset($this->data[$value])) {
- $value = $this->data[$value];
- } elseif (strstr($value, 'key=') && strstr($value, '&')) {
- $value = $this->parse($value);
- } elseif ($a = strstr($value, '{') || strstr($value, '(')) {
- $value = $this->eval($value);
- }
- return $this->handle($type, $value, $state);
- }
- protected function parse($value)
- {
- parse_str($value, $temp);
- $k = $temp['key'];
- unset($temp['key']);
- if (isset($this->data[$k]) && isset($temp[$this->data[$k]])) {
- $value = $temp[$this->data[$k]];
- }
- return $value;
- }
- protected function eval($value)
- {
- $func = function ($r) {
- return $this->value($r[1]);
- };
- $value = preg_replace_callback('/{(.*?)}/', $func, $value);
- $value = '$value = '.$value.';';
- eval($value);
- return $value;
- }
- protected function handle($type, $value, $state)
- {
- # state == true 是编码 == false 是解码
- if ($type == -1) {
- $value = (string) $value;
- } elseif ($type == -2) {
- $value = (float) $value;
- } elseif ($type == -3) {
- if ($state) {
- $value = Dever::json_encode($value);
- } else {
- $value = Dever::json_decode($value);
- }
- } elseif ($type == -4) {
- if ($state) {
- $value = strtotime($value);
- } else {
- $value = date('Y-m-d H:i:s', $value);
- }
- } elseif ($type == -5) {
- if ($state) {
- $value = date('Y-m-d H:i:s', $value);
- } else {
- $value = strtotime($value);
- }
- } elseif ($type == -6) {
- if ($state) {
- $value = date('Y-m-d\TH:i:sP', $value);
- } else {
- $value = strtotime($value);
- }
- } else {
- if ($state) {
- $value = $this->ssl->encrypt($type, $value);
- } else {
- $value = $this->ssl->decrypt($type, $value);
- }
- }
- return $value;
- }
- }
|