comparison vendor/symfony/http-foundation/AcceptHeaderItem.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 1fec387a4317
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
19 class AcceptHeaderItem 19 class AcceptHeaderItem
20 { 20 {
21 private $value; 21 private $value;
22 private $quality = 1.0; 22 private $quality = 1.0;
23 private $index = 0; 23 private $index = 0;
24 private $attributes = array(); 24 private $attributes = [];
25 25
26 /** 26 /**
27 * @param string $value 27 * @param string $value
28 * @param array $attributes 28 * @param array $attributes
29 */ 29 */
30 public function __construct($value, array $attributes = array()) 30 public function __construct($value, array $attributes = [])
31 { 31 {
32 $this->value = $value; 32 $this->value = $value;
33 foreach ($attributes as $name => $value) { 33 foreach ($attributes as $name => $value) {
34 $this->setAttribute($name, $value); 34 $this->setAttribute($name, $value);
35 } 35 }
44 */ 44 */
45 public static function fromString($itemValue) 45 public static function fromString($itemValue)
46 { 46 {
47 $bits = preg_split('/\s*(?:;*("[^"]+");*|;*(\'[^\']+\');*|;+)\s*/', $itemValue, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); 47 $bits = preg_split('/\s*(?:;*("[^"]+");*|;*(\'[^\']+\');*|;+)\s*/', $itemValue, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
48 $value = array_shift($bits); 48 $value = array_shift($bits);
49 $attributes = array(); 49 $attributes = [];
50 50
51 $lastNullAttribute = null; 51 $lastNullAttribute = null;
52 foreach ($bits as $bit) { 52 foreach ($bits as $bit) {
53 if (($start = substr($bit, 0, 1)) === ($end = substr($bit, -1)) && ('"' === $start || '\'' === $start)) { 53 if (($start = substr($bit, 0, 1)) === ($end = substr($bit, -1)) && ('"' === $start || '\'' === $start)) {
54 $attributes[$lastNullAttribute] = substr($bit, 1, -1); 54 $attributes[$lastNullAttribute] = substr($bit, 1, -1);
55 } elseif ('=' === $end) { 55 } elseif ('=' === $end) {
56 $lastNullAttribute = $bit = substr($bit, 0, -1); 56 $lastNullAttribute = $bit = substr($bit, 0, -1);
57 $attributes[$bit] = null; 57 $attributes[$bit] = null;
58 } else { 58 } else {
59 $parts = explode('=', $bit); 59 $parts = explode('=', $bit);
60 $attributes[$parts[0]] = isset($parts[1]) && strlen($parts[1]) > 0 ? $parts[1] : ''; 60 $attributes[$parts[0]] = isset($parts[1]) && \strlen($parts[1]) > 0 ? $parts[1] : '';
61 } 61 }
62 } 62 }
63 63
64 return new self(($start = substr($value, 0, 1)) === ($end = substr($value, -1)) && ('"' === $start || '\'' === $start) ? substr($value, 1, -1) : $value, $attributes); 64 return new self(($start = substr($value, 0, 1)) === ($end = substr($value, -1)) && ('"' === $start || '\'' === $start) ? substr($value, 1, -1) : $value, $attributes);
65 } 65 }
66 66
67 /** 67 /**
68 * Returns header value's string representation. 68 * Returns header value's string representation.
69 * 69 *
70 * @return string 70 * @return string
71 */ 71 */
72 public function __toString() 72 public function __toString()
73 { 73 {
74 $string = $this->value.($this->quality < 1 ? ';q='.$this->quality : ''); 74 $string = $this->value.($this->quality < 1 ? ';q='.$this->quality : '');
75 if (count($this->attributes) > 0) { 75 if (\count($this->attributes) > 0) {
76 $string .= ';'.implode(';', array_map(function ($name, $value) { 76 $string .= ';'.implode(';', array_map(function ($name, $value) {
77 return sprintf(preg_match('/[,;=]/', $value) ? '%s="%s"' : '%s=%s', $name, $value); 77 return sprintf(preg_match('/[,;=]/', $value) ? '%s="%s"' : '%s=%s', $name, $value);
78 }, array_keys($this->attributes), $this->attributes)); 78 }, array_keys($this->attributes), $this->attributes));
79 } 79 }
80 80