Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F25940420
D4075.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
4 KB
Referenced Files
None
Subscribers
None
D4075.diff
View Options
diff --git a/omnitools/src/Collections/WeightedList.php b/omnitools/src/Collections/WeightedList.php
--- a/omnitools/src/Collections/WeightedList.php
+++ b/omnitools/src/Collections/WeightedList.php
@@ -6,6 +6,8 @@
use Countable;
use Iterator;
use IteratorAggregate;
+use Keruald\OmniTools\Strings\Multibyte\OmniString;
+use TypeError;
/**
* Represent a list of values associated to a weight.
@@ -17,30 +19,42 @@
/**
* @var WeightedValue[]
*/
- private $list;
-
- public function __construct () {
- $this->list = [];
- }
+ private array $list;
/**
- * @param string $expression A string like "a,b;q=0.1,c;q=0.4"
+ * Build a new instance of the list.
*
- * @return WeightedList
+ * Without any argument, create an empty list.
*
- * @see RFC 7231, section 5.3.1
+ * @param iterable $items a collection of WeightedValue values [facultative]
+ * @throws TypeError if the iterable doesn't yield WeightedValue values.
*/
- public static function parse (string $expression) : WeightedList {
- $list = new WeightedList();
+ public function __construct (iterable $items = []) {
+ $this->list = [];
- if ($expression !== "") {
- $items = explode(',', $expression);
- foreach ($items as $item) {
- $list->addFromString($item);
+ foreach ($items as $item) {
+ if (!$item instanceof WeightedValue) {
+ throw new TypeError;
}
+
+ $this->list[] = $item;
}
+ }
- return $list;
+ /**
+ * Parse an expression as defined in the RFC 7231, section 5.3.1,
+ * for example the content of the HTTP_ACCEPT_LANGUAGE header.
+ *
+ * @param string $expression A string like "a,b;q=0.1,c;q=0.4"
+ * @return WeightedList
+ */
+ public static function parse (string $expression) : WeightedList {
+ $instance = new self();
+ $instance->list = (new OmniString($expression))
+ ->explode(",")
+ ->map(fn($item) => WeightedValue::parse($item))
+ ->toArray();
+ return $instance;
}
///
@@ -94,8 +108,8 @@
$values = [];
foreach ($this->list as $item) {
- $weights[] = $item->getWeight();
- $values[] = $item->getValue();
+ $weights[] = $item->weight;
+ $values[] = $item->value;
}
array_multisort($weights, SORT_DESC, $values, SORT_ASC);
diff --git a/omnitools/src/Collections/WeightedValue.php b/omnitools/src/Collections/WeightedValue.php
--- a/omnitools/src/Collections/WeightedValue.php
+++ b/omnitools/src/Collections/WeightedValue.php
@@ -22,28 +22,27 @@
const DEFAULT_WEIGHT = 1.0;
///
- /// Private members
+ /// Constructors
///
/**
- * @var float
+ * Build a new instance of a weighed value.
+ *
+ * @param mixed $value The value
+ * @param float $weight The weight ; if omitted, the weight will be 1.0
*/
- private $weight = self::DEFAULT_WEIGHT;
+ public function __construct (
+ public mixed $value,
+ public float $weight = self::DEFAULT_WEIGHT
+ ) {
+ }
/**
- * @var mixed
+ * Parse an expression like "fr-FR;q=0.7" to extract value and weight.
+ *
+ * @param string $expression The expression, "<value>;q⁼<weight>"
+ * @return WeightedValue
*/
- private $value;
-
- ///
- /// Constructors
- ///
-
- public function __construct ($value, float $weight = self::DEFAULT_WEIGHT) {
- $this->value = $value;
- $this->weight = $weight;
- }
-
public static function parse (string $expression) : WeightedValue {
$pair = explode(';q=', $expression);
@@ -58,20 +57,32 @@
/// Getters and setters
///
+ /**
+ * @deprecated Use directly $instance->weight
+ */
public function getWeight () : float {
return $this->weight;
}
+ /**
+ * @deprecated Use directly $instance->weight = $weight
+ */
public function setWeight (float $weight) : self {
$this->weight = $weight;
return $this;
}
+ /**
+ * @deprecated Use directly $instance->value
+ */
public function getValue () {
return $this->value;
}
+ /**
+ * @deprecated Use directly $instance->value = $value
+ */
public function setValue ($value) : self {
$this->value = $value;
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Apr 20, 04:35 (2 h, 14 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3641638
Default Alt Text
D4075.diff (4 KB)
Attached To
Mode
D4075: Refactor weighted list and value
Attached
Detach File
Event Timeline
Log In to Comment