Skip to content

Commit

Permalink
Shorten readme API section
Browse files Browse the repository at this point in the history
  • Loading branch information
gapple committed Feb 10, 2023
1 parent 44c635f commit b87f449
Showing 1 changed file with 10 additions and 108 deletions.
118 changes: 10 additions & 108 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,120 +19,22 @@ composer require gapple/structured-fields
API
---

### Parsing an Item

The `Parser::parseItem()` method returns a `[value, parameters]` tuple.

```php
print_r(\gapple\StructuredFields\Parser::parseItem("42"));

// Array
// (
// [0] => 42
// [1] => stdClass Object
// (
// )
// )
```

### Parsing a List

The `Parser::parseList()` method returns an array of `[value, parameters]` tuples.

```php
print_r(\gapple\StructuredFields\Parser::parseList("1, 42;towel;panic=?0"));

// Array
// (
// [0] => Array
// (
// [0] => 1
// [1] => stdClass Object
// (
// )
// )
// [1] => Array
// (
// [0] => 42
// [1] => stdClass Object
// (
// [towel] => 1
// [panic] =>
// )
// )
// )
```

### Parsing a Dictionary

The `Parser::parseDictionary()` method returns a `\stdClass` object with `[value, parameters]` tuples.

```php
print_r(\gapple\StructuredFields\Parser::parseDictionary("towel, panic=?0"));

// stdClass Object
// (
// [towel] => Array
// (
// [0] => 1
// [1] => stdClass Object
// (
// )
// )
// [panic] => Array
// (
// [0] =>
// [1] => stdClass Object
// (
// )
// )
// )
The `Parser` class provides static methods to convert a header string to structured data.
If the string cannot be parsed, a `ParseException` is thrown.

```

### Serializing an Item

The `Serializer::serializeItem()` method.

```php
print_r(\gapple\StructuredFields\Serializer::serializeItem(true));
// ?1

print_r(\gapple\StructuredFields\Serializer::serializeItem(42));
// 42

print_r(\gapple\StructuredFields\Serializer::serializeItem("42"));
// "42"

print_r(\gapple\StructuredHeStructuredFieldsaders\Serializer::serializeItem(new \gapple\StructuredFields\Bytes('🙂')));
// :8J+Zgg==:
Parser::parseItem(string): Item;
Parser::parseList(string): OuterList;
Parser::parseDictionary(string): Dictionary;
```

### Serializing a List

The `Serializer::serializeList()` method.
The `Serializer` class provides static methods to convert structured data to a header sting.
If the input cannot be serialized, a `SerializeException` is thrown.

```php
print_r(\gapple\StructuredFields\Serializer::serializeList([
[1, (object) []],
[42, (object) ['towel' => true, 'panic' => false]],
]));

// 1, 42;towel;panic=?0
```

### Serializing a Dictionary

The `Serializer::serializeDictionary()` method.

```php
print_r(\gapple\StructuredFields\Serializer::serializeDictionary((object) [
'towel' => [true, (object) []],
'panic' => [false, (object) []],
]));

// towel, panic=?0

Serializer::serializeItem(mixed, ?object): string;
Serializer::serializeList(iterable): string;
Serializer::serializeDictionary(object): string;
```

[1]: https://www.rfc-editor.org/rfc/rfc8941.html

0 comments on commit b87f449

Please sign in to comment.