1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: hugh.li |
5
|
|
|
* Date: 2022/3/3 |
6
|
|
|
* Time: 18:32 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace HughCube\Laravel\WeChat\Model; |
10
|
|
|
|
11
|
|
|
use Exception; |
12
|
|
|
use Illuminate\Support\Str; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @see https://developers.weixin.qq.com/doc/offiaccount/Account_Management/Generating_a_Parametric_QR_Code.html |
16
|
|
|
*/ |
17
|
|
|
class QrScene |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string|int|null |
21
|
|
|
*/ |
22
|
|
|
protected $type; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string|int |
26
|
|
|
*/ |
27
|
|
|
protected $value; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string|int $value |
31
|
|
|
* @param string|int|null $type |
32
|
|
|
*/ |
33
|
|
|
public function __construct($value, $type = null) |
34
|
|
|
{ |
35
|
|
|
$this->type = $type; |
36
|
|
|
$this->value = $value; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string|int $value |
41
|
|
|
* @param string|int|null $type |
42
|
|
|
* @return static |
43
|
|
|
*/ |
44
|
|
|
public static function new($value, $type = null): QrScene |
45
|
|
|
{ |
46
|
|
|
$class = static::class; |
47
|
|
|
return new $class($value, $type); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param mixed $data |
52
|
|
|
* @return null|static |
53
|
|
|
*/ |
54
|
|
|
public static function create($data): ?QrScene |
55
|
|
|
{ |
56
|
|
|
if (is_int($data) || (is_numeric($data) && ctype_digit(strval($data)))) { |
57
|
|
|
return static::new($data); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (!is_string($data)) { |
61
|
|
|
return null; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$scene = $data; |
65
|
|
|
if (Str::startsWith($scene, 'qrscene_')) { |
66
|
|
|
$scene = Str::afterLast($data, 'qrscene_'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (!Str::contains($scene, ':')) { |
70
|
|
|
return static::new($scene); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$items = explode(':', $scene, 2); |
74
|
|
|
return static::new($items[1], $items[0]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return string|int|null |
79
|
|
|
*/ |
80
|
|
|
public function getType() |
81
|
|
|
{ |
82
|
|
|
return $this->type; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return string|int |
87
|
|
|
*/ |
88
|
|
|
public function getValue() |
89
|
|
|
{ |
90
|
|
|
return $this->value; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return string |
95
|
|
|
* @throws Exception |
96
|
|
|
*/ |
97
|
|
|
public function toString(): string |
98
|
|
|
{ |
99
|
|
|
if (null === $this->getType()) { |
100
|
|
|
$string = strval($this->getValue()); |
101
|
|
|
} else { |
102
|
|
|
$string = sprintf('%s:%s', $this->getType(), $this->getValue()); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if (64 < strlen($string) || empty($string)) { |
106
|
|
|
throw new Exception('The value is a string of 1 to 64 characters'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $string; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return string |
114
|
|
|
* @throws Exception |
115
|
|
|
*/ |
116
|
|
|
public function __toString() |
117
|
|
|
{ |
118
|
|
|
return $this->toString(); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|