Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F12870358
WeightedList.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
WeightedList.php
View Options
<?php
namespace
Keruald\OmniTools\Collections
;
use
ArrayIterator
;
use
Countable
;
use
Iterator
;
use
IteratorAggregate
;
class
WeightedList
implements
IteratorAggregate
,
Countable
{
/**
* @var WeightedValue[]
*/
private
$list
;
public
function
__construct
()
{
$this
->
list
=
[];
}
/**
* @param string $expression A string like "a,b;q=0.1,c;q=0.4"
*
* @return WeightedList
*
* @see RFC 7231, section 5.3.1
*/
public
static
function
parse
(
string
$expression
)
:
WeightedList
{
$list
=
new
WeightedList
();
if
(
$expression
!==
""
)
{
$items
=
explode
(
','
,
$expression
);
foreach
(
$items
as
$item
)
{
$list
->
addFromString
(
$item
);
}
}
return
$list
;
}
///
/// Helper methods
///
public
function
add
(
$item
,
float
$weight
=
1.0
)
:
void
{
$this
->
list
[]
=
new
WeightedValue
(
$item
,
$weight
);
}
public
function
addWeightedValue
(
WeightedValue
$value
)
:
void
{
$this
->
list
[]
=
$value
;
}
public
function
addFromString
(
string
$expression
)
:
void
{
$this
->
addWeightedValue
(
WeightedValue
::
parse
(
$expression
));
}
public
function
clear
()
:
void
{
$this
->
list
=
[];
}
public
function
getHeaviest
()
{
$value
=
null
;
foreach
(
$this
->
list
as
$candidate
)
{
if
(
$value
===
null
||
$candidate
->
compareTo
(
$value
)
>
0
)
{
$value
=
$candidate
;
}
}
return
$value
;
}
public
function
toSortedArray
()
:
array
{
$weights
=
[];
$values
=
[];
foreach
(
$this
->
list
as
$item
)
{
$weights
[]
=
$item
->
getWeight
();
$values
[]
=
$item
->
getValue
();
}
array_multisort
(
$weights
,
SORT_DESC
,
$values
,
SORT_ASC
);
return
$values
;
}
///
/// IteratorAggregate implementation
///
public
function
getIterator
()
:
Iterator
{
return
new
ArrayIterator
(
$this
->
list
);
}
///
/// Countable implementation
///
public
function
count
()
:
int
{
return
count
(
$this
->
list
);
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Mon, Nov 17, 15:48 (22 h, 4 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3110256
Default Alt Text
WeightedList.php (2 KB)
Attached To
Mode
rKOT Keruald OmniTools
Attached
Detach File
Event Timeline
Log In to Comment