Skip to content

Commit 2aff1aa

Browse files
committed
renamed getOComp to getNormalizedValue and moved it to functions.php
1 parent 318967b commit 2aff1aa

File tree

5 files changed

+71
-140
lines changed

5 files changed

+71
-140
lines changed

src/Store/QueryHandler/InsertQueryHandler.php

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace sweetrdf\InMemoryStoreSqlite\Store\QueryHandler;
1515

16+
use function sweetrdf\InMemoryStoreSqlite\getNormalizedValue;
1617
use sweetrdf\InMemoryStoreSqlite\KeyValueBag;
1718

1819
class InsertQueryHandler extends QueryHandler
@@ -164,7 +165,7 @@ private function addTripleToGraph(array $triple, string $graph): void
164165
'o' => $objectId,
165166
'o_type' => $triple['o_type_int'],
166167
'o_lang_dt' => $oLangDtId,
167-
'o_comp' => $this->getOComp($triple['o']),
168+
'o_comp' => getNormalizedValue($triple['o']),
168169
]);
169170
} else {
170171
$tripleId = $check['t'];
@@ -230,50 +231,6 @@ private function prepareTriple(array $triple, string $graph): array
230231
return $triple;
231232
}
232233

233-
/**
234-
* Get normalized value for ORDER BY operations.
235-
*/
236-
private function getOComp($val): string
237-
{
238-
/* try date (e.g. 21 August 2007) */
239-
if (
240-
preg_match('/^[0-9]{1,2}\s+[a-z]+\s+[0-9]{4}/i', $val)
241-
&& ($uts = strtotime($val))
242-
&& (-1 !== $uts)
243-
) {
244-
return date("Y-m-d\TH:i:s", $uts);
245-
}
246-
247-
/* xsd date (e.g. 2009-05-28T18:03:38+09:00 2009-05-28T18:03:38GMT) */
248-
if (true === (bool) strtotime($val)) {
249-
return date('Y-m-d\TH:i:s\Z', strtotime($val));
250-
}
251-
252-
if (is_numeric($val)) {
253-
$val = sprintf('%f', $val);
254-
if (preg_match("/([\-\+])([0-9]*)\.([0-9]*)/", $val, $m)) {
255-
return $m[1].sprintf('%018s', $m[2]).'.'.sprintf('%-015s', $m[3]);
256-
}
257-
if (preg_match("/([0-9]*)\.([0-9]*)/", $val, $m)) {
258-
return '+'.sprintf('%018s', $m[1]).'.'.sprintf('%-015s', $m[2]);
259-
}
260-
261-
return $val;
262-
}
263-
264-
/* any other string: remove tags, linebreaks etc., but keep MB-chars */
265-
// [\PL\s]+ ( = non-Letters) kills digits
266-
$re = '/[\PL\s]+/isu';
267-
$re = '/[\s\'\"\´\`]+/is';
268-
$val = trim(preg_replace($re, '-', strip_tags($val)));
269-
if (\strlen($val) > 35) {
270-
$fnc = \function_exists('mb_substr') ? 'mb_substr' : 'substr';
271-
$val = $fnc($val, 0, 17).'-'.$fnc($val, -17);
272-
}
273-
274-
return $val;
275-
}
276-
277234
/**
278235
* Generates the next valid ID based on latest values in id2val, s2val and o2val.
279236
*

src/Store/QueryHandler/LoadQueryHandler.php

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace sweetrdf\InMemoryStoreSqlite\Store\QueryHandler;
1515

1616
use function sweetrdf\InMemoryStoreSqlite\calcURI;
17+
use function sweetrdf\InMemoryStoreSqlite\getNormalizedValue;
1718
use sweetrdf\InMemoryStoreSqlite\Store\TurtleLoader;
1819

1920
class LoadQueryHandler extends QueryHandler
@@ -72,7 +73,7 @@ public function addT($s, $p, $o, $s_type, $o_type, $o_dt = '', $o_lang = '')
7273
'p' => $this->getStoredTermID($p, '0', 'id'),
7374
'o' => $this->getStoredTermID($o, $type_ids[$o_type], 'o'),
7475
'o_lang_dt' => $this->getStoredTermID($o_dt.$o_lang, $o_dt ? '0' : '2', 'id'),
75-
'o_comp' => $this->getOComp($o),
76+
'o_comp' => getNormalizedValue($o),
7677
's_type' => $type_ids[$s_type],
7778
'o_type' => $type_ids[$o_type],
7879
];
@@ -236,47 +237,6 @@ public function getTripleID($t)
236237
}
237238
}
238239

239-
public function getOComp($val)
240-
{
241-
/* try date (e.g. 21 August 2007) */
242-
if (
243-
preg_match('/^[0-9]{1,2}\s+[a-z]+\s+[0-9]{4}/i', $val)
244-
&& ($uts = strtotime($val))
245-
&& (-1 !== $uts)
246-
) {
247-
return date("Y-m-d\TH:i:s", $uts);
248-
}
249-
250-
/* xsd date (e.g. 2009-05-28T18:03:38+09:00 2009-05-28T18:03:38GMT) */
251-
if (true === (bool) strtotime($val)) {
252-
return date('Y-m-d\TH:i:s\Z', strtotime($val));
253-
}
254-
255-
if (is_numeric($val)) {
256-
$val = sprintf('%f', $val);
257-
if (preg_match("/([\-\+])([0-9]*)\.([0-9]*)/", $val, $m)) {
258-
return $m[1].sprintf('%018s', $m[2]).'.'.sprintf('%-015s', $m[3]);
259-
}
260-
if (preg_match("/([0-9]*)\.([0-9]*)/", $val, $m)) {
261-
return '+'.sprintf('%018s', $m[1]).'.'.sprintf('%-015s', $m[2]);
262-
}
263-
264-
return $val;
265-
}
266-
267-
/* any other string: remove tags, linebreaks etc., but keep MB-chars */
268-
// [\PL\s]+ ( = non-Letters) kills digits
269-
$re = '/[\PL\s]+/isu';
270-
$re = '/[\s\'\"\´\`]+/is';
271-
$val = trim(preg_replace($re, '-', strip_tags($val)));
272-
if (\strlen($val) > 35) {
273-
$fnc = \function_exists('mb_substr') ? 'mb_substr' : 'substr';
274-
$val = $fnc($val, 0, 17).'-'.$fnc($val, -17);
275-
}
276-
277-
return $val;
278-
}
279-
280240
public function bufferTripleSQL($t)
281241
{
282242
$tbl = 'triple';

src/functions.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,50 @@ function calcBase(string $path): string
6666
return 'file://'.realpath($r); /* real path */
6767
}
6868

69+
/**
70+
* Normalize value for ORDER BY operations.
71+
*/
72+
function getNormalizedValue(string $val): string
73+
{
74+
/* try date (e.g. 21 August 2007) */
75+
if (
76+
preg_match('/^[0-9]{1,2}\s+[a-z]+\s+[0-9]{4}/i', $val)
77+
&& ($uts = strtotime($val))
78+
&& (-1 !== $uts)
79+
) {
80+
return (string) date("Y-m-d\TH:i:s", $uts);
81+
}
82+
83+
/* xsd date (e.g. 2009-05-28T18:03:38+09:00 2009-05-28T18:03:38GMT) */
84+
if (true === (bool) strtotime($val)) {
85+
return (string) date('Y-m-d\TH:i:s\Z', strtotime($val));
86+
}
87+
88+
if (is_numeric($val)) {
89+
$val = sprintf('%f', $val);
90+
if (preg_match("/([\-\+])([0-9]*)\.([0-9]*)/", $val, $m)) {
91+
return $m[1].sprintf('%018s', $m[2]).'.'.sprintf('%-015s', $m[3]);
92+
}
93+
if (preg_match("/([0-9]*)\.([0-9]*)/", $val, $m)) {
94+
return '+'.sprintf('%018s', $m[1]).'.'.sprintf('%-015s', $m[2]);
95+
}
96+
97+
return $val;
98+
}
99+
100+
/* any other string: remove tags, linebreaks etc., but keep MB-chars */
101+
// [\PL\s]+ ( = non-Letters) kills digits
102+
$re = '/[\PL\s]+/isu';
103+
$re = '/[\s\'\"\´\`]+/is';
104+
$val = trim(preg_replace($re, '-', strip_tags($val)));
105+
if (\strlen($val) > 35) {
106+
$fnc = \function_exists('mb_substr') ? 'mb_substr' : 'substr';
107+
$val = $fnc($val, 0, 17).'-'.$fnc($val, -17);
108+
}
109+
110+
return $val;
111+
}
112+
69113
/**
70114
* @return array<string,string>
71115
*/

tests/Unit/Store/QueryHandler/LoadQueryHandlerTest.php

Lines changed: 0 additions & 53 deletions
This file was deleted.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Tests\Unit\functions;
4+
5+
use function sweetrdf\InMemoryStoreSqlite\getNormalizedValue;
6+
use Tests\TestCase;
7+
8+
class getNormalizedValueTest extends TestCase
9+
{
10+
/**
11+
* Tests to behavior, if a datetime string was given.
12+
*/
13+
public function test()
14+
{
15+
// case with +hourse
16+
$string = '2009-05-28T18:03:38+09:00';
17+
$this->assertEquals('2009-05-28T09:03:38Z', getNormalizedValue($string));
18+
19+
// GMT case
20+
$string = '2009-05-28T18:03:38GMT';
21+
$this->assertEquals('2009-05-28T18:03:38Z', getNormalizedValue($string));
22+
}
23+
}

0 commit comments

Comments
 (0)