Skip to content

Commit 7d97b9d

Browse files
committed
Simplify public API
1 parent 7def8ce commit 7d97b9d

File tree

5 files changed

+21
-107
lines changed

5 files changed

+21
-107
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Structured Field Values for PHP
66
[![Build](https://github.com/bakame-php/http-structured-fields/workflows/build/badge.svg)](https://github.com/bakame-php/http-structured-fields/actions?query=workflow%3A%22build%22)
77

88
The package uses pragmatic value objects to parse and serialize [HTTP Structured Fields][1] in PHP.
9+
Structured fields are intended for use by specifications of new HTTP fields that wish to use a
10+
common syntax that is more restrictive than traditional HTTP field values.
911

1012
You will be able to:
1113

@@ -96,7 +98,7 @@ to an `Item` instance or other container types **BUT** the items it contains ca
9698
themselves contain `Parameters` instance. More on parameters public API
9799
will be cover in subsequent paragraphs.
98100

99-
#### Examples
101+
#### Usage
100102

101103
Instantiation via type recognition is done using the `Item::from` named constructor.
102104

@@ -132,13 +134,13 @@ $item->isInteger(); //return true
132134
### Containers
133135

134136
Apart from the `Item`, the RFC defines different containers with different requirements. The
135-
package exposes those containers via the following methods `Parameters`, `Dictionary`,
137+
package exposes those containers via the following value objects `Parameters`, `Dictionary`,
136138
`InnerList` and `OrderedList` with the same basic public API. At any given time it
137139
is possible to:
138140

141+
- iterate over each contained element and its optional associated key via the `IteratorAggregate` interface;
139142
- tell whether the container is empty via an `isEmpty` method;
140143
- know the number of elements contained in the container via the `Countable` interface;
141-
- iterate over each contained elements and its optional associated key via the `IteratorAggregate` interface;
142144
- tell whether an element is attached to the container using its `index` or `key` via `hasIndex` and `hasKey` methods;
143145
- get any element by its string `key` or by its integer `index` via `getByKey` and `getByIndex` methods when applicable;
144146
- merge multiple instance of the same container using the `merge` method;

src/InnerList.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -159,24 +159,6 @@ public function isEmpty(): bool
159159
return [] === $this->elements;
160160
}
161161

162-
/**
163-
* @return array<string>
164-
*/
165-
public function keys(): array
166-
{
167-
return [];
168-
}
169-
170-
public function getByKey(string $key): Item|null
171-
{
172-
throw InvalidOffset::dueToKeyNotFound($key);
173-
}
174-
175-
public function hasKey(string $key): bool
176-
{
177-
return false;
178-
}
179-
180162
public function getByIndex(int $index): Item|null
181163
{
182164
$offset = $this->filterIndex($index);

src/InnerListTest.php

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function it_can_add_or_remove_elements(): void
4848
self::assertCount(1, $instance);
4949
self::assertFalse($instance->hasIndex(1));
5050

51-
$instance->push(Item::from('BarBaz'));
51+
$instance->push('BarBaz');
5252
$instance->insert(1, );
5353
$element = $instance->getByIndex(1);
5454
self::assertCount(2, $instance);
@@ -77,12 +77,11 @@ public function it_fails_to_instantiate_with_wrong_parameters_in_field(): void
7777
public function it_can_unshift_insert_and_replace(): void
7878
{
7979
$container = new InnerList();
80-
$container->unshift(Item::from('42'));
81-
$container->push(Item::from(42));
82-
$container->insert(1, Item::from(42.0));
83-
$container->replace(0, Item::from(ByteSequence::fromDecoded('Hello World')));
80+
$container->unshift('42');
81+
$container->push(42);
82+
$container->insert(1, 42.0);
83+
$container->replace(0, ByteSequence::fromDecoded('Hello World'));
8484

85-
self::assertFalse($container->hasKey('42'));
8685
self::assertCount(3, $container);
8786
self::assertFalse($container->isEmpty());
8887
self::assertSame('(:SGVsbG8gV29ybGQ=: 42.0 42)', $container->toHttpValue());
@@ -96,7 +95,7 @@ public function it_fails_to_replace_invalid_index(): void
9695
$this->expectException(InvalidOffset::class);
9796

9897
$container = new InnerList();
99-
$container->replace(0, Item::from(ByteSequence::fromDecoded('Hello World')));
98+
$container->replace(0, ByteSequence::fromDecoded('Hello World'));
10099
}
101100

102101
/**
@@ -107,20 +106,7 @@ public function it_fails_to_insert_at_an_invalid_index(): void
107106
$this->expectException(InvalidOffset::class);
108107

109108
$container = new InnerList();
110-
$container->insert(3, Item::from(ByteSequence::fromDecoded('Hello World')));
111-
}
112-
113-
/**
114-
* @test
115-
*/
116-
public function it_fails_to_return_an_member_with_invalid_key(): void
117-
{
118-
$this->expectException(InvalidOffset::class);
119-
120-
$instance = new InnerList();
121-
self::assertFalse($instance->hasKey('foobar'));
122-
123-
$instance->getByKey('foobar');
109+
$container->insert(3, ByteSequence::fromDecoded('Hello World'));
124110
}
125111

126112
/**
@@ -136,18 +122,6 @@ public function it_fails_to_return_an_member_with_invalid_index(): void
136122
$instance->getByIndex(3);
137123
}
138124

139-
/**
140-
* @test
141-
*/
142-
public function it_can_returns_the_container_element_keys(): void
143-
{
144-
$instance = new InnerList();
145-
self::assertSame([], $instance->keys());
146-
147-
$instance->push(Item::from(false), Item::from(true));
148-
self::assertSame([], $instance->keys());
149-
}
150-
151125
/**
152126
* @test
153127
*/

src/OrderedList.php

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -57,32 +57,9 @@ public function isEmpty(): bool
5757
return [] === $this->elements;
5858
}
5959

60-
/**
61-
* @return array<string>
62-
*/
63-
public function keys(): array
64-
{
65-
return [];
66-
}
67-
68-
public function getByKey(string $key): Item|InnerList
69-
{
70-
throw InvalidOffset::dueToKeyNotFound($key);
71-
}
72-
73-
public function hasKey(string $key): bool
74-
{
75-
return false;
76-
}
77-
78-
public function getByIndex(int $index): Item|InnerList
60+
public function hasIndex(int $index): bool
7961
{
80-
$offset = $this->filterIndex($index);
81-
if (null === $offset) {
82-
throw InvalidOffset::dueToIndexNotFound($index);
83-
}
84-
85-
return $this->elements[$offset];
62+
return null !== $this->filterIndex($index);
8663
}
8764

8865
private function filterIndex(int $index): int|null
@@ -96,9 +73,14 @@ private function filterIndex(int $index): int|null
9673
};
9774
}
9875

99-
public function hasIndex(int $index): bool
76+
public function getByIndex(int $index): Item|InnerList
10077
{
101-
return null !== $this->filterIndex($index);
78+
$offset = $this->filterIndex($index);
79+
if (null === $offset) {
80+
throw InvalidOffset::dueToIndexNotFound($index);
81+
}
82+
83+
return $this->elements[$offset];
10284
}
10385

10486
public function unshift(InnerList|Item|ByteSequence|Token|bool|int|float|string ...$elements): void

src/OrderedListTest.php

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ public function it_can_unshift_insert_and_replace(): void
7373
$container->insert(1, Item::from(42.0));
7474
$container->replace(0, Item::from(ByteSequence::fromDecoded('Hello World')));
7575

76-
self::assertFalse($container->hasKey('42'));
7776
self::assertCount(3, $container);
7877
self::assertFalse($container->isEmpty());
7978
self::assertSame(':SGVsbG8gV29ybGQ=:, 42.0, 42', $container->toHttpValue());
@@ -101,19 +100,6 @@ public function it_fails_to_insert_at_an_invalid_index(): void
101100
$container->insert(3, Item::from(ByteSequence::fromDecoded('Hello World')));
102101
}
103102

104-
/**
105-
* @test
106-
*/
107-
public function it_fails_to_return_an_member_with_invalid_key(): void
108-
{
109-
$this->expectException(InvalidOffset::class);
110-
111-
$instance = new OrderedList();
112-
self::assertFalse($instance->hasKey('foobar'));
113-
114-
$instance->getByKey('foobar');
115-
}
116-
117103
/**
118104
* @test
119105
*/
@@ -127,18 +113,6 @@ public function it_fails_to_return_an_member_with_invalid_index(): void
127113
$instance->getByIndex(3);
128114
}
129115

130-
/**
131-
* @test
132-
*/
133-
public function it_can_returns_the_container_element_keys(): void
134-
{
135-
$instance = new OrderedList();
136-
self::assertSame([], $instance->keys());
137-
138-
$instance->push(Item::from(false), Item::from(true));
139-
self::assertSame([], $instance->keys());
140-
}
141-
142116
/**
143117
* @test
144118
*/

0 commit comments

Comments
 (0)