Description
This library provides a simple API for Media type designation., (*1)
It can be used to generate Media type strings from string literals such as
PHP class names. This is useful for APIs or serialization processes which should
denote a platform agnostic type literal., (*2)
This library leans on the RFC 6838 specification., (*3)
It is a companion to DreadLabs/media-type-decoding,
providing encoding of a Media type string literal., (*4)
Installation
composer install dreadlabs/media-type-encoding:~1.0
Usage
Example 1, (*5)
From a fully-qualified, namespaced PHP class name, designate a Media type within
the application/ top-level type. The subtype is located in the Personal
tree. The UpperCamelCased naming convention of PHP classes should be designated
to a hyphened form. The designated Media type comes with a version parameter and
a +json suffix., (*6)
$mediaType = new Application(RegistrationTree::personal(new HyphenedFromUpperCamelCased(new Exploded('\\'))));
$withParameter = $mediaType->withParameter(new Parameter('version', '1.0'));
$withSuffix = $withParameter->withSuffix(new Json());
echo (string)$withSuffix->designated('Acme\\Example\\Class');
> 'application/prs.acme.example.class+json; version=1.0'
Example 2, (*7)
This example enhances Example 1. Let's imagine you are structuring your PHP
project in such a way, that the Domain
namespace contains your business logic.
The PHP library you are building is named after a Bounded context called SalesApi
.
There, you have a Domain Event called UserCreated
. The Media type encoding
should not carry too much information about the vendor namespace and the
physical structures of the filesystem., (*8)
$imaginaryClassName = 'Acme\\SalesApi\\Domain\\Event\\UserCreated';
$namespace = 'Acme\\SalesApi\\Domain\\Event';
$mediaType = new Application(RegistrationTree::vendor(new HyphenedFromUpperCamelCased(new Exploded('\\'))));
$designatedMediaType = $mediaType->designated(str_replace($namespace, 'SalesApi', $imaginaryClassName));
self::assertEquals('application/vnd.sales-api.user-created', (string)$designatedMediaType);
The reason why I am showing you this is, because I thought about implementing a
subtype Prefixed
which allows replacing parts of the FQCN. But this would add
unnecessary complexity where a simple str_replace
is sufficent., (*9)
Development
Requirements
Please read the contribution guide and ensure you have a
working Docker environment., (*10)
Setup
Fork and clone this repository as described in the contribution guide., (*11)
Open a terminal and run the setup script:, (*12)
script/setup
Run tests
script/console run composer test:unit
script/console run composer test:integration
script/console run composer test:acceptance:fail-fast
Links
License
MIT, (*13)