Field.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php namespace Api\Lib\Platform;
  2. use Dever;
  3. class Field
  4. {
  5. private $data = array();
  6. public $ssl;
  7. public function __construct($data)
  8. {
  9. $this->data = $data;
  10. $this->ssl = new Ssl($this);
  11. $this->set('time', time());
  12. $this->set('timestamp', \Dever\Helper\Secure::timestamp());
  13. $this->set('nonce', \Dever\Helper\Secure::nonce());
  14. }
  15. public function set($key, $value)
  16. {
  17. $this->data[$key] = $value;
  18. }
  19. public function __set($key, $value)
  20. {
  21. $this->data[$key] = $value;
  22. }
  23. public function __get($key)
  24. {
  25. if (array_key_exists($key, $this->data)) {
  26. return $this->data[$key];
  27. } else {
  28. return null;
  29. }
  30. }
  31. public function setHost($value)
  32. {
  33. $this->set('body', $value);
  34. }
  35. public function setUri($value)
  36. {
  37. $this->set('uri', $value);
  38. }
  39. public function setNotify($value)
  40. {
  41. $this->set('notify', $value);
  42. }
  43. public function setSign($value)
  44. {
  45. $this->set('sign', $value);
  46. }
  47. public function setMethod($value)
  48. {
  49. $this->set('method', $value);
  50. }
  51. public function setBody($value)
  52. {
  53. $this->set('body', $value);
  54. }
  55. public function setBodyJson($value)
  56. {
  57. $this->set('body_json', $value);
  58. }
  59. public function setHeader($value)
  60. {
  61. $this->set('header', $value);
  62. }
  63. public function setNumber($value)
  64. {
  65. $this->set('number', $value);
  66. }
  67. public function value($value, $type = -1, $state = true)
  68. {
  69. $value = trim($value);
  70. if ($this->data && isset($this->data[$value])) {
  71. $value = $this->data[$value];
  72. } elseif (strstr($value, 'key=') && strstr($value, '&')) {
  73. $value = $this->parse($value);
  74. } elseif ($a = strstr($value, '{') || strstr($value, '(')) {
  75. $value = $this->eval($value);
  76. }
  77. return $this->handle($type, $value, $state);
  78. }
  79. protected function parse($value)
  80. {
  81. parse_str($value, $temp);
  82. $k = $temp['key'];
  83. unset($temp['key']);
  84. if (isset($this->data[$k]) && isset($temp[$this->data[$k]])) {
  85. $value = $temp[$this->data[$k]];
  86. }
  87. return $value;
  88. }
  89. protected function eval($value)
  90. {
  91. $func = function ($r) {
  92. return $this->value($r[1]);
  93. };
  94. $value = preg_replace_callback('/{(.*?)}/', $func, $value);
  95. $value = '$value = '.$value.';';
  96. eval($value);
  97. return $value;
  98. }
  99. protected function handle($type, $value, $state)
  100. {
  101. # state == true 是编码 == false 是解码
  102. if ($type == -1) {
  103. $value = (string) $value;
  104. } elseif ($type == -2) {
  105. $value = (float) $value;
  106. } elseif ($type == -3) {
  107. if ($state) {
  108. $value = Dever::json_encode($value);
  109. } else {
  110. $value = Dever::json_decode($value);
  111. }
  112. } elseif ($type == -4) {
  113. if ($state) {
  114. $value = strtotime($value);
  115. } else {
  116. $value = date('Y-m-d H:i:s', $value);
  117. }
  118. } elseif ($type == -5) {
  119. if ($state) {
  120. $value = date('Y-m-d H:i:s', $value);
  121. } else {
  122. $value = strtotime($value);
  123. }
  124. } elseif ($type == -6) {
  125. if ($state) {
  126. $value = date('Y-m-d\TH:i:sP', $value);
  127. } else {
  128. $value = strtotime($value);
  129. }
  130. } else {
  131. if ($state) {
  132. $value = $this->ssl->encrypt($type, $value);
  133. } else {
  134. $value = $this->ssl->decrypt($type, $value);
  135. }
  136. }
  137. return $value;
  138. }
  139. }