LogicTrait.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * Created by CRMEB.
  4. * User: 136327134@qq.com
  5. * Date: 2019/4/9 16:50
  6. */
  7. namespace crmeb\traits;
  8. use crmeb\exceptions\AuthException;
  9. trait LogicTrait
  10. {
  11. /**
  12. * @var array
  13. */
  14. protected $items = [];
  15. /**
  16. * 实例化本身
  17. * @var object
  18. */
  19. protected static $instance;
  20. /**
  21. * 配置参数
  22. * @param array $config
  23. */
  24. protected function setConfig(array $config = [])
  25. {
  26. foreach ($config as $key => $value) {
  27. $this->set($this->items, $key, $value);
  28. }
  29. }
  30. /**
  31. * 设置参数
  32. * @param $array
  33. * @param $key
  34. * @param $value
  35. * @return mixed
  36. */
  37. protected function set(&$array, $key, $value)
  38. {
  39. if (is_null($key)) return $array = $value;
  40. $keys = explode('.', $key);
  41. while (count($keys) > 1) {
  42. $key = array_shift($keys);
  43. if (!isset($array[$key]) || !is_array($array[$key])) {
  44. $array[$key] = [];
  45. }
  46. $array = &$array[$key];
  47. }
  48. $array[array_shift($keys)] = $value;
  49. return $array;
  50. }
  51. /**
  52. * 实例化类
  53. */
  54. protected function registerProviders()
  55. {
  56. if (property_exists($this, 'providers')) {
  57. foreach ($this->providers as $key => $provider) {
  58. $this->register(new $provider(), $key);
  59. }
  60. }
  61. }
  62. /**
  63. * 获取类内配置信息
  64. * @param object $pimple
  65. * @return $this
  66. * */
  67. protected function register($pimple, $key)
  68. {
  69. $response = $pimple->register($this->items);
  70. if (is_array($response)) {
  71. [$key, $provider] = $response;
  72. $this->$key = $provider;
  73. } else if (is_string($key)) {
  74. $this->$key = $pimple;
  75. }
  76. return $this;
  77. }
  78. /**
  79. * 实例化本类
  80. * @param array $config
  81. * @return $this
  82. * */
  83. public static function instance($config = [])
  84. {
  85. if (is_null(self::$instance)) {
  86. self::$instance = new self();
  87. self::$instance->setConfig($config);
  88. self::$instance->registerProviders();
  89. if (method_exists(self::$instance, 'bool'))
  90. self::$instance->bool();
  91. }
  92. return self::$instance;
  93. }
  94. /**
  95. * 转数据类型
  96. * @param $var
  97. * @param string $type
  98. * @return array|bool|float|int|string|null
  99. */
  100. public function toType($var, $type = 'string')
  101. {
  102. if ($type === 'string') {
  103. return (string)$var;
  104. } else if ($type === 'array') {
  105. return is_array($var) ? $var : [$var];
  106. } else if ($type === 'boolean') {
  107. return (bool)$var;
  108. } else if ($type === 'float') {
  109. return (float)$var;
  110. } else if ($type === 'int') {
  111. return (int)$var;
  112. } else if ($type === 'null') {
  113. return null;
  114. }
  115. return $var;
  116. }
  117. /**
  118. * 设置属性
  119. * @param $method
  120. * @param $ages
  121. * @return $this
  122. */
  123. public function __call($method, $ages)
  124. {
  125. $keys = property_exists($this, 'providers') ? array_keys($this->providers) : [];
  126. $propsRuleKeys = property_exists($this, 'propsRule') ? array_keys($this->propsRule) : [];
  127. if (strstr($method, 'set') !== false) {
  128. $attribute = lcfirst(str_replace('set', '', $method));
  129. if (property_exists($this, $attribute) && in_array($attribute, $propsRuleKeys)) {
  130. $propsRuleValeu = $this->propsRule[$attribute];
  131. $valeu = null;
  132. if (is_array($propsRuleValeu)) {
  133. $type = $propsRuleValeu[1] ?? 'string';
  134. $callable = $propsRuleValeu[2] ?? null;
  135. if ($type == 'callable' && $callable) {
  136. $callable = $propsRuleValeu[2];
  137. if (method_exists($this, $callable))
  138. $ages[0] = $this->{$callable}($ages[0], $ages[1] ?? '');
  139. } else if ($type) {
  140. $ages[0] = $this->toType($ages[0], $type) ?? null;
  141. }
  142. } else {
  143. $valeu = $propsRuleValeu;
  144. }
  145. $this->{$attribute} = $ages[0] ?? $valeu;
  146. return $this;
  147. }
  148. } else if (in_array($method, $keys)) {
  149. if (property_exists($this, 'handleType') && array_shift($ages) !== true) {
  150. $this->handleType = $method;
  151. return $this;
  152. } else {
  153. return $this->{$method};
  154. }
  155. } else {
  156. throw new AuthException('Method does not exist:' . $method);
  157. }
  158. }
  159. }