| Total Complexity | 12 |
| Total Lines | 106 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 12 | class Binder implements JsonSerializable |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var string |
||
| 16 | */ |
||
| 17 | private $key; |
||
| 18 | /** |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | private $name; |
||
| 22 | /** |
||
| 23 | * @var mixed |
||
| 24 | */ |
||
| 25 | private $value; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var int |
||
| 29 | */ |
||
| 30 | protected static $index = 0; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Binder constructor. |
||
| 34 | * @param string $name 绑定名 |
||
| 35 | * @param mixed $value 绑定值 |
||
| 36 | * @param string|null $key SQL键名 |
||
| 37 | */ |
||
| 38 | public function __construct(string $name, $value, string $key = null) |
||
| 39 | { |
||
| 40 | $this->name = $name; |
||
| 41 | $this->value = $value; |
||
| 42 | $this->key = $key; |
||
| 43 | static::$index++; |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @param string $name |
||
| 48 | * @return string |
||
| 49 | */ |
||
| 50 | public static function index(string $name): string |
||
| 51 | { |
||
| 52 | return '_' . $name . '_' . static::$index; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @return string |
||
| 57 | */ |
||
| 58 | public function getName(): string |
||
| 59 | { |
||
| 60 | return $this->name; |
||
| 61 | } |
||
| 62 | |||
| 63 | |||
| 64 | /** |
||
| 65 | * @return mixed |
||
| 66 | */ |
||
| 67 | public function getValue() |
||
| 68 | { |
||
| 69 | return $this->value; |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * 创建值的类型 |
||
| 74 | * |
||
| 75 | * @param mixed $value |
||
| 76 | * @return int |
||
| 77 | */ |
||
| 78 | public static function typeOf($value) |
||
| 79 | { |
||
| 80 | if (null === $value) { |
||
| 81 | $type = PDO::PARAM_NULL; |
||
| 82 | } elseif (is_bool($value)) { |
||
| 83 | $type = PDO::PARAM_BOOL; |
||
| 84 | } elseif (is_numeric($value) && intval($value) === $value) { |
||
| 85 | $type = PDO::PARAM_INT; |
||
| 86 | } else { |
||
| 87 | $type = PDO::PARAM_STR; |
||
| 88 | } |
||
| 89 | return $type; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @return string|null |
||
| 94 | */ |
||
| 95 | public function getKey() |
||
| 96 | { |
||
| 97 | return $this->key; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @return array|mixed |
||
| 102 | */ |
||
| 103 | public function jsonSerialize() |
||
| 104 | { |
||
| 105 | return [ |
||
| 106 | 'name' => $this->name, |
||
| 107 | 'key' => $this->key, |
||
| 108 | 'value' => $this->value, |
||
| 109 | ]; |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @return false|string |
||
| 114 | */ |
||
| 115 | public function __toString() |
||
| 118 | } |
||
| 119 | } |
||
| 120 |