1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of PHP-Typography. |
4
|
|
|
* |
5
|
|
|
* Copyright 2017-2019 Peter Putzer. |
6
|
|
|
* |
7
|
|
|
* This program is free software; you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU General Public License as published by |
9
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
10
|
|
|
* (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU General Public License along |
18
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc., |
19
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
20
|
|
|
* |
21
|
|
|
* *** |
22
|
|
|
* |
23
|
|
|
* @package mundschenk-at/php-typography |
24
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.html |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace PHP_Typography\Text_Parser; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Tokenized text. |
31
|
|
|
* |
32
|
|
|
* Multibyte characters are assumed to be encoded as UTF-8. |
33
|
|
|
* |
34
|
|
|
* @since 5.0.0 |
35
|
|
|
* |
36
|
|
|
* @author Peter Putzer <[email protected]> |
37
|
|
|
* |
38
|
|
|
* @property-read string $value The token value. |
39
|
|
|
* @property-read int $type The token type. Can be any of the following constants: |
40
|
|
|
* - Token::SPACE |
41
|
|
|
* - Token::PUNCTUATION |
42
|
|
|
* - Token::WORD |
43
|
|
|
* - Token::OTHER |
44
|
|
|
* @property-read bool $mutable Wether the properties of the object can be modified. |
45
|
|
|
*/ |
46
|
|
|
final class Token { |
47
|
|
|
const SPACE = 1; |
48
|
|
|
const PUNCTUATION = 2; |
49
|
|
|
const WORD = 3; |
50
|
|
|
const OTHER = 4; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* The token type. Can be any of the following constants: |
54
|
|
|
* - Token::SPACE |
55
|
|
|
* - Token::PUNCTUATION |
56
|
|
|
* - Token::WORD |
57
|
|
|
* - Token::OTHER |
58
|
|
|
* |
59
|
|
|
* @var int |
60
|
|
|
*/ |
61
|
|
|
private $type; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* The token value. |
65
|
|
|
* |
66
|
|
|
* @var string |
67
|
|
|
*/ |
68
|
|
|
private $value; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Ensure that properties can only be set once via the constructor. |
72
|
|
|
* |
73
|
|
|
* @var boolean |
74
|
|
|
*/ |
75
|
|
|
private $mutable = true; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Creates a new token. |
79
|
|
|
* |
80
|
|
|
* @param string $value The token value. |
81
|
|
|
* @param int $type Optional. Default Token::WORD. |
82
|
|
|
* |
83
|
|
|
* @throws \BadMethodCallException If the constructor is called twice. |
84
|
|
|
* @throws \UnexpectedValueException If the type attribute is outside the allowed range. |
85
|
|
|
*/ |
86
|
23 |
|
public function __construct( $value, $type = self::WORD ) { |
87
|
23 |
|
if ( false === $this->mutable ) { |
88
|
4 |
|
throw new \BadMethodCallException( 'Constructor called twice.' ); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
switch ( $type ) { |
92
|
23 |
|
case self::SPACE: |
93
|
21 |
|
case self::PUNCTUATION: |
94
|
18 |
|
case self::WORD: |
95
|
3 |
|
case self::OTHER: |
96
|
20 |
|
$this->type = $type; |
|
|
|
|
97
|
20 |
|
break; |
98
|
|
|
|
99
|
|
|
default: |
100
|
3 |
|
throw new \UnexpectedValueException( "Invalid type $type." ); |
101
|
|
|
} |
102
|
|
|
|
103
|
20 |
|
if ( ! \is_string( $value ) ) { |
|
|
|
|
104
|
1 |
|
throw new \UnexpectedValueException( 'Value has to be a string.' ); |
105
|
|
|
} else { |
106
|
19 |
|
$this->value = $value; |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
19 |
|
$this->mutable = false; |
|
|
|
|
110
|
19 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Provide read-only access to properties. |
114
|
|
|
* |
115
|
|
|
* @param string $property The property name. |
116
|
|
|
* |
117
|
|
|
* @return mixed |
118
|
|
|
*/ |
119
|
7 |
|
public function __get( $property ) { |
120
|
7 |
|
if ( \property_exists( $this, $property ) ) { |
121
|
6 |
|
return $this->$property; |
122
|
|
|
} |
123
|
1 |
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Prevent setting undeclared properties. |
127
|
|
|
* |
128
|
|
|
* @param string $id The property name. |
129
|
|
|
* @param mixed $val The value. |
130
|
|
|
* @return void |
131
|
|
|
* |
132
|
|
|
* @throws \BadMethodCallException The Token class is immutable. |
133
|
|
|
*/ |
134
|
4 |
|
public function __set( $id, $val ) { |
135
|
4 |
|
throw new \BadMethodCallException( 'Object of class Text_Parser\Token is immutable.' ); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Prevent un-setting of properties. |
140
|
|
|
* |
141
|
|
|
* @param string $id The property name. |
142
|
|
|
* @return void |
143
|
|
|
* |
144
|
|
|
* @throws \BadMethodCallException The Token class is immutable. |
145
|
|
|
*/ |
146
|
4 |
|
public function __unset( $id ) { |
147
|
4 |
|
throw new \BadMethodCallException( 'Object of class Text_Parser\Token is immutable.' ); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Create a new token with the same type, but a different value. |
152
|
|
|
* If the value is unchanged, the original token is returned. |
153
|
|
|
* |
154
|
|
|
* @param string $value The value attribute. |
155
|
|
|
* |
156
|
|
|
* @return Token |
157
|
|
|
*/ |
158
|
3 |
|
public function with_value( $value ) { |
159
|
3 |
|
if ( $this->value === $value ) { |
160
|
1 |
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
2 |
|
$cloned_token = clone $this; |
164
|
2 |
|
$cloned_token->value = $value; |
|
|
|
|
165
|
|
|
|
166
|
2 |
|
return $cloned_token; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
} |
170
|
|
|
|