Download the PHP package gilsegura/psr-messages without Composer

On this page you can find all versions of the php package gilsegura/psr-messages. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package psr-messages

PSR MESSAGES

tests codecov static analysis coding standards

Framework-agnostic toolkit for parsing, validating and producing HTTP messages on top of PSR-7 and PSR-15, with first-class support for JSON and JSON:API. It turns a raw request body, headers and query parameters into typed, validated schema objects, and builds JSON / JSON:API responses and error documents. The gilsegura/psr-messages-bundle package wires these middlewares into Symfony.

The guiding principle: the fixed, abstract structure lives here; the concrete shapes live in the consuming library. This toolkit owns the JSON:API formats (how a resource, a relationship, a link, a query parameter is shaped and parsed) and the abstractions a resource builds on; each application provides one concretion per resource — its create/update/query/headers schemas and its resource presenter — typed to that resource.

Installation

Media types

MediaType is the enum the toolkit is organised around: JSON (application/json) and JSON_API (application/vnd.api+json). It resolves the media type from a header line, and SupportsMediaTypeTrait / MediaTypeParserInterface / MediaTypeResponseFactoryInterface let parsers and response factories declare which media type they handle.

The request lifecycle

For every request the toolkit runs validation first, parsing second:

  1. Validate the raw body, headers and query against JSON Schema (SchemaValidator, HeadersValidator, QueryParamsValidator), surfacing a typed exception on failure (InvalidBodyException, InvalidHeadersException, InvalidQueryException). What an endpoint allows — which fields, includes, sort fields and sparse fieldsets are accepted — is enforced here.
  2. Parse the now-valid input into typed objects through the middlewares below, so handlers read typed schemas instead of raw arrays.

Schemas

A schema turns raw input into a typed object:

A request body that accepts several shapes (e.g. create vs update) exposes one schema per shape; the resolver chooses. Each schema reads its fields with RequiredReader (mandatory) and OptionalReader (partial updates), the latter returning an Optional that distinguishes "absent, leave untouched" from "present, write this value".

Middlewares (PSR-15)

Three middlewares type the parts of a request, after validation:

JSON:API documents

Psr\Messages\JsonApi\Document builds the response side. Every definition takes the minimal identity in its constructor and is composed further through immutable withXxx() methods, so a document is assembled one concern at a time. State is exposed directly as readonly properties — the Has* interfaces declare it with property hooks (public T $x { get; }), not getters — while withXxx() methods derive new instances. The rule across the package: read state through a property, derive copies through a method.

Resource types and relationship names are polymorphic: ResourceTypeInterface and RelationshipNameInterface both extend \BackedEnum, so each application defines its own type and relationship enums while the documents stay generic. Relationships are added with the typed name — withOneRelationship($name, ...) and withManyRelationship($name, ...) take a RelationshipNameInterface — and includes are matched against it (AbstractIncludes::has($name)).

Presenting a resource

ResourcePresenterInterface<TModel> is the fixed shape of the output side: one presenter per resource in the consuming library turns a domain model into a ResourceInterface. It receives the requested fieldset as a FieldsetInterface (the contract the query's AbstractFields implements) and attaches it to the resource with withFieldset(), so the resource trims its own attributes on output. Relationships are passed in by the caller (e.g. a document builder resolving includes), keeping the presenter focused on the primary resource.

JSON:API query parameters

Page has a fixed structure and is the one query object shared as-is: it parses page[number]/page[size] and derives offset(). Everything else is abstract, because each resource allows different includes, fields, sort fields and filters:

A consuming library subclasses these per resource (ArticleIncludes, ArticleFields, ArticleSort, ArticleQuery), reusing the fixed parse format and only typing the result; the endpoint's JSON Schema enforces what is allowed.

Including related resources

Psr\Messages\JsonApi\Include provides the fixed shape for compound documents (?include=). An IncludeInterface<TPrimary> is one composable relationship that can be embedded: it knows its name() and how to resolve() itself for a set of primary models in a single load — loading the related resources (through a query handler, never the repository), presenting them, and computing the linkage per primary model. The consuming library writes one implementation per relationship; nothing else changes when a new one is added.

ResolvedInclude is the outcome of resolving one include: the embedded resources for the document's included section plus the relationship linkage keyed by primary-model id, produced once so it is reused for both the relationships and the included section without querying twice.

AbstractIncludes::select() picks which available includes a request asked for (the pure match); resolving them (the I/O) is the caller's job, since each include loads through a query handler. Pagination produces the standard JSON:API pagination links (self/first/last/prev/next) and page meta from the requested page, the total and the request path.

A resource's concretion

For each resource an application defines a uniform set, all validated before parsing:

JSON

Psr\Messages\Json handles plain application/json and mirrors the JSON:API flow without any JSON:API concepts (no resource type, relationships, includes or sparse fieldsets). JsonParser resolves a request body into a typed schema; JsonResponseFactory builds responses. On the output side, JsonPresenterInterface maps a serializable read model to a document payload — the plain-JSON counterpart of the resource presenter — and JsonDocument / JsonCollectionDocument render a single payload or a list of them. JsonErrorDocument renders errors.

Links

Psr\Messages\Link models hypermedia links: Href, Link, and LinkTypeInterface (a \BackedEnum) with LinkType providing the standard types (self, related, first, last, prev, next, ...). Downstream libraries can define their own link types and stay polymorphic.

Headers

Psr\Messages\Headers parses the Authorization header into typed credentials: BearerToken and BasicCredentials via ParsesAuthorizationHeaderTrait and AuthorizationScheme.

Errors

Psr\Messages\Error provides the error model both document formats render: ErrorInterface / Error, ErrorCodeInterface, and Source (with SourceTypeInterface / SourceType) pointing at the request member that caused the error. JsonErrorDocument and JsonApiErrorDocument render them for each media type.

A complete flow

The pieces fit into one request/response flow. The fixed, abstract parts below live in this package; the typed concretions (ArticleQuery, ArticlePresenter, the type and relationship enums, the JSON Schemas) live in the consuming library.

A read endpoint, GET /articles?include=author&fields[articles]=title&page[number]=1:

  1. Validate, then parse the query. ParseQueryParamsMiddleware first runs QueryParamsValidator against the endpoint's JSON Schema (which enforces the allowed includes, fields and sort), then resolves the typed query:

  2. Drive the read side from the typed query. The handler reads $query->page->offset()/->size, turns $query->sort into an order ($query->sort->directionFor('created')), $query->filters->forField(...) into criteria, and resolves includes with $query->includes->has(Article::AUTHOR) and forName() (whose Path::tail() drives nested includes). It returns serializable read models.

  3. Present each model. ArticlePresenter (an ResourcePresenterInterface) turns a read model into a resource, applying the sparse fieldsets and attaching the relationships the handler resolved:

  4. Build the document and respond. The presented resources go into a ResourceCollectionDocument with pagination links and meta, and JsonApiResponseFactory::collection($document, Status::OK) produces the PSR-7 response.

A write endpoint, POST /articles, is the mirror image on the input side: ParsedBodyMiddleware validates the body with SchemaValidator, then resolves a typed CreateArticleRequest (SchemaInterface, fields read with RequiredReader); the handler executes the command; the created resource is presented and returned with JsonApiResponseFactory::single(...). A PATCH differs only in using OptionalReader / Optional so absent fields are left untouched. Failures anywhere surface as typed exceptions that JsonApiResponseFactory::error(...) renders as a JSON:API error document.

The plain-JSON flow is identical with the Json pieces: JsonParser, JsonPresenterInterface, JsonDocument / JsonCollectionDocument and JsonResponseFactory, minus the JSON:API-only concerns.

License

MIT. See LICENSE.


All versions of psr-messages with dependencies

PHP Build Version
Package Version
Requires php Version ^8.4
ext-json Version *
gilsegura/psr-server Version ^1.0
gilsegura/psr-validator Version ^1.0
gilsegura/serializer Version ^1.0
psr/http-message Version ^2.0
psr/http-server-handler Version ^1.0
psr/http-server-middleware Version ^1.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package gilsegura/psr-messages contains the following files

Loading the files please wait ...