Autolink.php
5 years ago
Configuration.php
5 years ago
EmojiRegex.php
5 years ago
Extractor.php
5 years ago
HitHighlighter.php
5 years ago
ParseResults.php
5 years ago
Parser.php
5 years ago
Regex.php
5 years ago
StringUtils.php
5 years ago
TldLists.php
5 years ago
Validator.php
5 years ago
Configuration.php
216 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @author Takashi Nojima |
| 5 | * @copyright Copyright 2018, Takashi Nojima |
| 6 | * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License v2.0 |
| 7 | * @package Twitter.Text |
| 8 | */ |
| 9 | |
| 10 | namespace Twitter\Text; |
| 11 | |
| 12 | /** |
| 13 | * Twitter Text Configuration |
| 14 | * |
| 15 | * @author Takashi Nojima |
| 16 | * @copyright Copyright 2018, Takashi Nojima |
| 17 | * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License v2.0 |
| 18 | * @package Twitter.Text |
| 19 | * |
| 20 | * @property int $version |
| 21 | * @property int $maxWeightedTweetLength |
| 22 | * @property int $scale |
| 23 | * @property int $defaultWeight |
| 24 | * @property bool $emojiParsingEnabled |
| 25 | * @property int $transformedURLLength |
| 26 | * @property array $ranges |
| 27 | */ |
| 28 | class Configuration |
| 29 | { |
| 30 | /** |
| 31 | * configuration from v3.json |
| 32 | * |
| 33 | * @var array |
| 34 | */ |
| 35 | private static $v3Config = array( |
| 36 | 'version' => 3, |
| 37 | 'maxWeightedTweetLength' => 280, |
| 38 | 'scale' => 100, |
| 39 | 'defaultWeight' => 200, |
| 40 | 'emojiParsingEnabled' => true, |
| 41 | 'transformedURLLength' => 23, |
| 42 | 'ranges' => array( |
| 43 | array( |
| 44 | 'start' => 0, |
| 45 | 'end' => 4351, |
| 46 | 'weight' => 100, |
| 47 | ), |
| 48 | array( |
| 49 | 'start' => 8192, |
| 50 | 'end' => 8205, |
| 51 | 'weight' => 100, |
| 52 | ), |
| 53 | array( |
| 54 | 'start' => 8208, |
| 55 | 'end' => 8223, |
| 56 | 'weight' => 100, |
| 57 | ), |
| 58 | array( |
| 59 | 'start' => 8242, |
| 60 | 'end' => 8247, |
| 61 | 'weight' => 100, |
| 62 | ), |
| 63 | ), |
| 64 | ); |
| 65 | |
| 66 | /** |
| 67 | * configuration from v2.json |
| 68 | * |
| 69 | * @var array |
| 70 | */ |
| 71 | private static $v2Config = array( |
| 72 | 'version' => 2, |
| 73 | 'maxWeightedTweetLength' => 280, |
| 74 | 'scale' => 100, |
| 75 | 'defaultWeight' => 200, |
| 76 | 'transformedURLLength' => 23, |
| 77 | 'ranges' => array( |
| 78 | array( |
| 79 | 'start' => 0, |
| 80 | 'end' => 4351, |
| 81 | 'weight' => 100, |
| 82 | ), |
| 83 | array( |
| 84 | 'start' => 8192, |
| 85 | 'end' => 8205, |
| 86 | 'weight' => 100, |
| 87 | ), |
| 88 | array( |
| 89 | 'start' => 8208, |
| 90 | 'end' => 8223, |
| 91 | 'weight' => 100, |
| 92 | ), |
| 93 | array( |
| 94 | 'start' => 8242, |
| 95 | 'end' => 8247, |
| 96 | 'weight' => 100, |
| 97 | ), |
| 98 | ), |
| 99 | ); |
| 100 | |
| 101 | /** |
| 102 | * configuration from v1.json |
| 103 | * |
| 104 | * @var array |
| 105 | */ |
| 106 | private static $v1Config = array( |
| 107 | 'version' => 1, |
| 108 | 'maxWeightedTweetLength' => 140, |
| 109 | 'scale' => 1, |
| 110 | 'defaultWeight' => 1, |
| 111 | 'transformedURLLength' => 23, |
| 112 | 'ranges' => array(), |
| 113 | ); |
| 114 | |
| 115 | /** |
| 116 | * @var array |
| 117 | */ |
| 118 | private $config; |
| 119 | |
| 120 | /** |
| 121 | * construct |
| 122 | * |
| 123 | * @param array $config |
| 124 | */ |
| 125 | public function __construct(array $config = null) |
| 126 | { |
| 127 | if ($config === null) { |
| 128 | $config = static::$v3Config; |
| 129 | } |
| 130 | |
| 131 | $this->config = $config; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * property accessor |
| 136 | * |
| 137 | * @param string $name |
| 138 | * @return mixed |
| 139 | */ |
| 140 | public function __get($name) |
| 141 | { |
| 142 | return isset($this->config[$name]) ? $this->config[$name] : null; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * convert to array |
| 147 | * |
| 148 | * @return array |
| 149 | */ |
| 150 | public function toArray() |
| 151 | { |
| 152 | return $this->config; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Create configuration from json string |
| 157 | * |
| 158 | * @param string $json as configuration |
| 159 | * @return Configuration |
| 160 | */ |
| 161 | public static function fromJson($json) |
| 162 | { |
| 163 | return new Configuration(json_decode($json, true)); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Get twitter-text 1.x configuration |
| 168 | * |
| 169 | * @return Configuration |
| 170 | */ |
| 171 | public static function v1() |
| 172 | { |
| 173 | return new self(static::$v1Config); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Get twitter-text 2.x configuration |
| 178 | * |
| 179 | * @return Configuration |
| 180 | */ |
| 181 | public static function v2() |
| 182 | { |
| 183 | return new self(static::$v2Config); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * maxWeightedTweetLength * scale |
| 188 | * |
| 189 | * @return int |
| 190 | */ |
| 191 | public function getScaledMaxWeightedTweetLength() |
| 192 | { |
| 193 | return $this->maxWeightedTweetLength * $this->scale; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * transformedURLLength * scale |
| 198 | * |
| 199 | * @return int |
| 200 | */ |
| 201 | public function getScaledTransformedURLLength() |
| 202 | { |
| 203 | return $this->transformedURLLength * $this->scale; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Get whether emoji parsing is enabled. |
| 208 | * |
| 209 | * @return bool `true` if emoji parsing is enabled, otherwise `false`. |
| 210 | */ |
| 211 | public function getEmojiParsingEnabled() |
| 212 | { |
| 213 | return (bool)$this->emojiParsingEnabled; |
| 214 | } |
| 215 | } |
| 216 |