Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F93510093
PhabricatorCustomFieldMonogramParser.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Fri, Nov 29, 08:24
Size
1 KB
Mime Type
text/x-php
Expires
Sun, Dec 1, 08:24 (2 d)
Engine
blob
Format
Raw Data
Handle
22657303
Attached To
rPH Phabricator
PhabricatorCustomFieldMonogramParser.php
View Options
<?php
abstract
class
PhabricatorCustomFieldMonogramParser
extends
Phobject
{
abstract
protected
function
getPrefixes
();
abstract
protected
function
getSuffixes
();
abstract
protected
function
getInfixes
();
abstract
protected
function
getMonogramPattern
();
public
function
parseCorpus
(
$corpus
)
{
$prefixes
=
$this
->
getPrefixes
();
$suffixes
=
$this
->
getSuffixes
();
$infixes
=
$this
->
getInfixes
();
$prefix_regex
=
$this
->
buildRegex
(
$prefixes
);
$infix_regex
=
$this
->
buildRegex
(
$infixes
,
true
);
$suffix_regex
=
$this
->
buildRegex
(
$suffixes
,
true
,
true
);
$monogram_pattern
=
$this
->
getMonogramPattern
();
$pattern
=
'/'
.
'(?:^|
\b
)'
.
$prefix_regex
.
$infix_regex
.
'((?:'
.
$monogram_pattern
.
'[,
\s
]*)+)'
.
$suffix_regex
.
'(?:$|
\b
)'
.
'/'
;
$matches
=
null
;
$ok
=
preg_match_all
(
$pattern
,
$corpus
,
$matches
,
PREG_SET_ORDER
|
PREG_OFFSET_CAPTURE
);
if
(
$ok
===
false
)
{
throw
new
Exception
(
pht
(
'Regular expression "%s" is invalid!'
,
$pattern
));
}
$results
=
array
();
foreach
(
$matches
as
$set
)
{
$results
[]
=
array
(
'match'
=>
$set
[
0
][
0
],
'prefix'
=>
$set
[
1
][
0
],
'infix'
=>
$set
[
2
][
0
],
'monograms'
=>
array_filter
(
preg_split
(
'/[,
\s
]+/'
,
$set
[
3
][
0
])),
'suffix'
=>
idx
(
idx
(
$set
,
4
,
array
()),
0
,
''
),
'offset'
=>
$set
[
0
][
1
],
);
}
return
$results
;
}
private
function
buildRegex
(
array
$list
,
$optional
=
false
,
$final
=
false
)
{
$parts
=
array
();
foreach
(
$list
as
$string
)
{
$parts
[]
=
preg_quote
(
$string
,
'/'
);
}
$parts
=
implode
(
'|'
,
$parts
);
$maybe_tail
=
$final
?
''
:
'
\s
+'
;
$maybe_optional
=
$optional
?
'?'
:
''
;
return
'(?i:('
.
$parts
.
')'
.
$maybe_tail
.
')'
.
$maybe_optional
;
}
}
Event Timeline
Log In to Comment