4
4
* from within a MARC record.
5
5
*
6
6
* @author Carsten Klee <[email protected] >
7
- * @package CK\MARCspec
8
7
* @copyright For the full copyright and license information, please view the LICENSE
9
8
* file that was distributed with this source code.
10
9
*/
13
12
use CK \MARCspec \Exception \InvalidMARCspecException ;
14
13
15
14
/**
16
- * A MARCspec comparison string class
17
- */
15
+ * A MARCspec comparison string class.
16
+ */
18
17
class ComparisonString implements ComparisonStringInterface, \JsonSerializable, \ArrayAccess
19
18
{
20
-
21
19
/**
22
20
* @var string The escaped comparison string
23
21
*/
24
22
private $ raw ;
25
23
26
24
/**
27
- *
28
- * {@inheritdoc}
29
- *
30
- * @throws \InvalidArgumentException if argument is not a string or
31
- * comparison string is not properly escaped
32
- */
25
+ * {@inheritdoc}
26
+ *
27
+ * @throws \InvalidArgumentException if argument is not a string or
28
+ * comparison string is not properly escaped
29
+ */
33
30
public function __construct ($ raw )
34
31
{
35
-
36
32
if (!is_string ($ raw )) {
37
33
throw new \InvalidArgumentException ('Argument must be of type string. Got '
38
34
.gettype ($ raw ).'. ' );
39
35
}
40
-
36
+
41
37
if (false !== strpos ($ raw , ' ' )) {
42
38
throw new InvalidMARCspecException (
43
39
InvalidMARCspecException::CS .
44
40
InvalidMARCspecException::SPACE ,
45
41
$ raw
46
42
);
47
43
}
48
-
49
- /** char of list ${}!=~?|\s must be escaped if not at index 0*/
44
+
45
+ /* char of list ${}!=~?|\s must be escaped if not at index 0*/
50
46
if (!preg_match ('/^(.(?:[^${}!=~?| ]|(?<= \\\\)[${}!=~?|])*)$/ ' , $ raw )) {
51
47
throw new InvalidMARCspecException (
52
48
InvalidMARCspecException::CS .
53
49
InvalidMARCspecException::ESCAPE ,
54
50
$ raw
55
51
);
56
52
}
57
-
53
+
58
54
$ this ->raw = $ raw ;
59
55
}
60
-
56
+
61
57
/**
62
- * {@inheritdoc}
63
- */
58
+ * {@inheritdoc}
59
+ */
64
60
public function getComparable ()
65
61
{
66
62
$ comparable = str_replace ('\s ' , ' ' , $ this ->raw );
63
+
67
64
return stripcslashes ($ comparable );
68
65
}
69
-
66
+
70
67
/**
71
- * {@inheritdoc}
72
- */
68
+ * {@inheritdoc}
69
+ */
73
70
public function getRaw ()
74
71
{
75
72
return $ this ->raw ;
76
73
}
77
-
74
+
78
75
/**
79
- * {@inheritdoc}
80
- */
76
+ * {@inheritdoc}
77
+ */
81
78
public static function escape ($ arg )
82
79
{
83
- $ specialChars = ['{ ' ,'} ' ,'! ' ,'= ' ,'~ ' ,'? ' ];
80
+ $ specialChars = ['{ ' , '} ' , '! ' , '= ' , '~ ' , '? ' ];
84
81
for ($ i = 0 ; $ i < count ($ specialChars ); $ i ++) {
85
82
$ arg = str_replace ($ specialChars [$ i ], '\\' .$ specialChars [$ i ], $ arg );
86
83
}
84
+
87
85
return $ arg = str_replace (' ' , '\s ' , $ arg );
88
86
}
89
-
87
+
90
88
/**
91
89
* {@inheritdoc}
92
90
*/
93
91
public function __toString ()
94
92
{
95
- return "\\" .$ this ->raw ;
93
+ return '\\' .$ this ->raw ;
96
94
}
97
-
95
+
98
96
/**
99
97
* {@inheritdoc}
100
98
*/
101
99
public function jsonSerialize ()
102
100
{
103
- return ['comparisonString ' => $ this ->raw ];
101
+ return ['comparisonString ' => $ this ->raw ];
104
102
}
105
-
103
+
106
104
/**
107
- * Access object like an associative array
105
+ * Access object like an associative array.
108
106
*
109
107
* @api
110
108
*
@@ -121,9 +119,9 @@ public function offsetExists($offset)
121
119
return false ;
122
120
}
123
121
}
124
-
122
+
125
123
/**
126
- * Access object like an associative array
124
+ * Access object like an associative array.
127
125
*
128
126
* @api
129
127
*
@@ -142,9 +140,9 @@ public function offsetGet($offset)
142
140
throw new \UnexpectedValueException ("Offset $ offset does not exist. " );
143
141
}
144
142
}
145
-
143
+
146
144
/**
147
- * Access object like an associative array
145
+ * Access object like an associative array.
148
146
*
149
147
* @api
150
148
*
@@ -154,9 +152,9 @@ public function offsetSet($offset, $value)
154
152
{
155
153
throw new \UnexpectedValueException ("Offset $ offset cannot be set. " );
156
154
}
157
-
155
+
158
156
/**
159
- * Access object like an associative array
157
+ * Access object like an associative array.
160
158
*
161
159
* @param string $offset
162
160
*/
0 commit comments