Skip to content

Commit ef489a9

Browse files
committed
Improve codebase consistency
1 parent a2f1d55 commit ef489a9

File tree

5 files changed

+179
-184
lines changed

5 files changed

+179
-184
lines changed

src/Dictionary.php

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,29 @@ private static function extractPair(string $pair): array
8181
return [$found['key'], $found['value']];
8282
}
8383

84-
public function isEmpty(): bool
84+
public function toHttpValue(): string
8585
{
86-
return [] === $this->elements;
86+
$returnValue = [];
87+
foreach ($this->elements as $key => $element) {
88+
$returnValue[] = match (true) {
89+
$element instanceof Item && true === $element->value() => $key.$element->parameters()->toHttpValue(),
90+
default => $key.'='.$element->toHttpValue(),
91+
};
92+
}
93+
94+
return implode(', ', $returnValue);
8795
}
8896

8997
public function count(): int
9098
{
9199
return count($this->elements);
92100
}
93101

102+
public function isEmpty(): bool
103+
{
104+
return [] === $this->elements;
105+
}
106+
94107
/**
95108
* @return Iterator<string, Item|InnerList>
96109
*/
@@ -128,6 +141,17 @@ public function hasIndex(int $index): bool
128141
return null !== $this->filterIndex($index);
129142
}
130143

144+
private function filterIndex(int $index): int|null
145+
{
146+
$max = count($this->elements);
147+
148+
return match (true) {
149+
[] === $this->elements, 0 > $max + $index, 0 > $max - $index - 1 => null,
150+
0 > $index => $max + $index,
151+
default => $index,
152+
};
153+
}
154+
131155
public function getByIndex(int $index): Item|InnerList|null
132156
{
133157
$offset = $this->filterIndex($index);
@@ -138,19 +162,6 @@ public function getByIndex(int $index): Item|InnerList|null
138162
return array_values($this->elements)[$offset];
139163
}
140164

141-
public function toHttpValue(): string
142-
{
143-
$returnValue = [];
144-
foreach ($this->elements as $key => $element) {
145-
$returnValue[] = match (true) {
146-
$element instanceof Item && true === $element->value() => $key.$element->parameters()->toHttpValue(),
147-
default => $key.'='.$element->toHttpValue(),
148-
};
149-
}
150-
151-
return implode(', ', $returnValue);
152-
}
153-
154165
public function set(string $key, InnerList|Item|ByteSequence|Token|bool|int|float|string $element): void
155166
{
156167
self::validateKey($key);
@@ -173,17 +184,6 @@ private static function filterElement(InnerList|Item|ByteSequence|Token|bool|int
173184
};
174185
}
175186

176-
private function filterIndex(int $index): int|null
177-
{
178-
$max = count($this->elements);
179-
180-
return match (true) {
181-
[] === $this->elements, 0 > $max + $index, 0 > $max - $index - 1 => null,
182-
0 > $index => $max + $index,
183-
default => $index,
184-
};
185-
}
186-
187187
public function delete(string ...$keys): void
188188
{
189189
foreach ($keys as $key) {

src/InnerList.php

Lines changed: 61 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,77 @@ public static function fromHttpValue(string $httpValue): self
8181
);
8282
}
8383

84-
private static function convertItem(Item|ByteSequence|Token|bool|int|float|string|null $item): Item|null
84+
public function toHttpValue(): string
85+
{
86+
$returnArray = array_map(fn (Item|null $value): string|null => $value?->toHttpValue(), $this->elements);
87+
88+
return '('.implode(' ', $returnArray).')'.$this->parameters->toHttpValue();
89+
}
90+
91+
public function parameters(): Parameters
92+
{
93+
return $this->parameters;
94+
}
95+
96+
public function count(): int
8597
{
98+
return count($this->elements);
99+
}
100+
101+
public function isEmpty(): bool
102+
{
103+
return [] === $this->elements;
104+
}
105+
106+
/**
107+
* @return Iterator<Item|null>
108+
*/
109+
public function getIterator(): Iterator
110+
{
111+
foreach ($this->elements as $item) {
112+
yield $item;
113+
}
114+
}
115+
116+
public function has(int $index): bool
117+
{
118+
return null !== $this->filterIndex($index);
119+
}
120+
121+
private function filterIndex(int $index): int|null
122+
{
123+
$max = count($this->elements);
124+
86125
return match (true) {
87-
$item instanceof Item, null === $item => $item,
88-
default => Item::from($item),
126+
[] === $this->elements, 0 > $max + $index, 0 > $max - $index - 1 => null,
127+
0 > $index => $max + $index,
128+
default => $index,
89129
};
90130
}
91131

132+
public function get(int $index): Item|null
133+
{
134+
$offset = $this->filterIndex($index);
135+
if (null === $offset) {
136+
throw InvalidOffset::dueToIndexNotFound($index);
137+
}
138+
139+
return $this->elements[$offset];
140+
}
141+
92142
public function unshift(Item|ByteSequence|Token|bool|int|float|string|null ...$elements): void
93143
{
94144
$this->elements = [...array_map(self::convertItem(...), $elements), ...$this->elements];
95145
}
96146

147+
private static function convertItem(Item|ByteSequence|Token|bool|int|float|string|null $item): Item|null
148+
{
149+
return match (true) {
150+
$item instanceof Item, null === $item => $item,
151+
default => Item::from($item),
152+
};
153+
}
154+
97155
public function push(Item|ByteSequence|Token|bool|int|float|string|null ...$elements): void
98156
{
99157
foreach (array_map(self::convertItem(...), $elements) as $element) {
@@ -112,17 +170,6 @@ public function insert(int $index, Item|ByteSequence|Token|bool|int|float|string
112170
};
113171
}
114172

115-
private function filterIndex(int $index): int|null
116-
{
117-
$max = count($this->elements);
118-
119-
return match (true) {
120-
[] === $this->elements, 0 > $max + $index, 0 > $max - $index - 1 => null,
121-
0 > $index => $max + $index,
122-
default => $index,
123-
};
124-
}
125-
126173
public function replace(int $index, Item|ByteSequence|Token|bool|int|float|string|null $element): void
127174
{
128175
if (!$this->has($index)) {
@@ -152,51 +199,4 @@ public function merge(self ...$others): void
152199
}
153200
}
154201
}
155-
156-
public function isEmpty(): bool
157-
{
158-
return [] === $this->elements;
159-
}
160-
161-
public function get(int $index): Item|null
162-
{
163-
$offset = $this->filterIndex($index);
164-
if (null === $offset) {
165-
throw InvalidOffset::dueToIndexNotFound($index);
166-
}
167-
168-
return $this->elements[$offset];
169-
}
170-
171-
public function has(int $index): bool
172-
{
173-
return null !== $this->filterIndex($index);
174-
}
175-
176-
public function parameters(): Parameters
177-
{
178-
return $this->parameters;
179-
}
180-
181-
public function count(): int
182-
{
183-
return count($this->elements);
184-
}
185-
186-
/**
187-
* @return Iterator<Item|null>
188-
*/
189-
public function getIterator(): Iterator
190-
{
191-
foreach ($this->elements as $item) {
192-
yield $item;
193-
}
194-
}
195-
196-
public function toHttpValue(): string
197-
{
198-
$returnArray = array_map(fn (Item|null $value): string|null => $value?->toHttpValue(), $this->elements);
199-
200-
return '('.implode(' ', $returnArray).')'.$this->parameters->toHttpValue();
201-
}
202202
}

src/Item.php

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,34 @@ private static function parseString(string $string): array
172172
throw new SyntaxError("The HTTP textual representation `$originalString` for a string contains an invalid end string.");
173173
}
174174

175+
public function toHttpValue(): string
176+
{
177+
return $this->serializeValue($this->value).$this->parameters->toHttpValue();
178+
}
179+
180+
private function serializeValue(Token|ByteSequence|int|float|string|bool $value): string
181+
{
182+
return match (true) {
183+
$value instanceof Token => $value->toHttpValue(),
184+
$value instanceof ByteSequence => $value->toHttpValue(),
185+
is_string($value) => '"'.preg_replace('/(["\\\])/', '\\\$1', $value).'"',
186+
is_int($value) => (string) $value,
187+
is_float($value) => $this->serializeDecimal($value),
188+
default => '?'.($value ? '1' : '0'),
189+
};
190+
}
191+
192+
private function serializeDecimal(float $value): string
193+
{
194+
/** @var string $result */
195+
$result = json_encode(round($value, 3, PHP_ROUND_HALF_EVEN));
196+
if (str_contains($result, '.')) {
197+
return $result;
198+
}
199+
200+
return $result.'.0';
201+
}
202+
175203
public function value(): Token|ByteSequence|int|float|string|bool
176204
{
177205
return $this->value;
@@ -182,11 +210,6 @@ public function parameters(): Parameters
182210
return $this->parameters;
183211
}
184212

185-
public function toHttpValue(): string
186-
{
187-
return $this->serializeValue($this->value).$this->parameters->toHttpValue();
188-
}
189-
190213
public function isInteger(): bool
191214
{
192215
return is_int($this->value);
@@ -216,27 +239,4 @@ public function isByteSequence(): bool
216239
{
217240
return $this->value instanceof ByteSequence;
218241
}
219-
220-
private function serializeValue(Token|ByteSequence|int|float|string|bool $value): string
221-
{
222-
return match (true) {
223-
$value instanceof Token => $value->toHttpValue(),
224-
$value instanceof ByteSequence => $value->toHttpValue(),
225-
is_string($value) => '"'.preg_replace('/(["\\\])/', '\\\$1', $value).'"',
226-
is_int($value) => (string) $value,
227-
is_float($value) => $this->serializeDecimal($value),
228-
default => '?'.($value ? '1' : '0'),
229-
};
230-
}
231-
232-
private function serializeDecimal(float $value): string
233-
{
234-
/** @var string $result */
235-
$result = json_encode(round($value, 3, PHP_ROUND_HALF_EVEN));
236-
if (str_contains($result, '.')) {
237-
return $result;
238-
}
239-
240-
return $result.'.0';
241-
}
242242
}

0 commit comments

Comments
 (0)