Giter Site home page Giter Site logo

chubbyphp-negotiation's Introduction

chubbyphp-negotiation

CI Coverage Status Mutation testing badge Latest Stable Version Total Downloads Monthly Downloads

bugs code_smells coverage duplicated_lines_density ncloc sqale_rating alert_status reliability_rating security_rating sqale_index vulnerabilities

Description

A simple negotiation library.

Requirements

  • php: ^8.1
  • psr/http-message: ^1.1|^2.0

Suggest

  • chubbyphp/chubbyphp-container: ^2.2
  • chubbyphp/chubbyphp-http-exception: ^1.1
  • chubbyphp/chubbyphp-laminas-config-factory: ^1.3
  • pimple/pimple: ^3.5
  • psr/http-server-middleware: ^1.0

Installation

Through Composer as chubbyphp/chubbyphp-negotiation.

composer require chubbyphp/chubbyphp-negotiation "^2.1"

Usage

AcceptLanguageNegotiator

<?php

use Chubbyphp\Negotiation\AcceptLanguageNegotiator;

$request = ...;
$request->withHeader('Accept-Language', 'de,en-US;q=0.7,en;q=0.3');

$negotiator = new AcceptLanguageNegotiator(['en', 'de']);
$value = $negotiator->negotiate($request); // NegotiatedValue
$value->getValue(); // de
$value->getAttributes(); // ['q' => '1.0']

AcceptLanguageMiddleware

<?php

use Chubbyphp\Negotiation\Middleware\AcceptLanguageMiddleware;

$request = ...;
$request->withHeader('Accept-Language', 'de,en-US;q=0.7,en;q=0.3');

$middleware = new AcceptLanguageMiddleware($acceptLanguageNegotiator);
$response = $negotiator->process($request, $handler);

AcceptNegotiator

<?php

use Chubbyphp\Negotiation\AcceptNegotiator;

$request = ...;
$request->withHeader('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q =0.8');

$negotiator = new AcceptNegotiator(['application/json', 'application/xml', 'application/x-yaml']);
$value = $negotiator->negotiate($request); // NegotiatedValue
$value->getValue(); // application/xml
$value->getAttributes(); // ['q' => '0.9']

AcceptMiddleware

<?php

use Chubbyphp\Negotiation\Middleware\AcceptMiddleware;

$request = ...;
$request->withHeader('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q =0.8');

$middleware = new AcceptMiddleware($acceptNegotiator);
$response = $negotiator->process($request, $handler);

ContentTypeNegotiator

<?php

use Chubbyphp\Negotiation\ContentTypeNegotiator;

$request = ...;
$request->withHeader('Content-Type', 'application/xml; charset=UTF-8');

$negotiator = new ContentTypeNegotiator(['application/json', 'application/xml', 'application/x-yaml']);
$value = $negotiator->negotiate($request); // NegotiatedValue
$value->getValue(); // application/xml
$value->getAttributes(); // ['charset' => 'UTF-8']

ContentTypeMiddleware

<?php

use Chubbyphp\Negotiation\Middleware\ContentTypeMiddleware;

$request = ...;
$request->withHeader('Content-Type', 'application/xml; charset=UTF-8');

$middleware = new ContentTypeMiddleware($contentTypeNegotiator);
$response = $negotiator->process($request, $handler);

NegotiationServiceFactory

<?php

use Chubbyphp\Container\Container;
use Chubbyphp\Negotiation\ServiceFactory\NegotiationServiceFactory;
use Psr\Http\Message\ServerRequestInterface;

$container = new Container();
$container->factories((new NegotiationServiceFactory())());

$request = ...;

$container->get('negotiator.acceptNegotiator')
    ->negotiate($request);

$container->get('negotiator.acceptMiddleware')
    ->process($request, $handler);

$container->get('negotiator.acceptLanguageNegotiator')
    ->negotiate($request);

$container->get('negotiator.acceptLanguageMiddleware')
    ->process($request, $handler);

$container->get('negotiator.contentTypeNegotiator')
    ->negotiate($request);

$container->get('negotiator.contentTypeMiddleware')
    ->process($request, $handler);

NegotiationServiceProvider

<?php

use Chubbyphp\Negotiation\ServiceProvider\NegotiationServiceProvider;
use Pimple\Container;
use Psr\Http\Message\ServerRequestInterface;

$container = new Container();
$container->register(new NegotiationServiceProvider);

$request = ...;

$container['negotiator.acceptNegotiator']
    ->negotiate($request);

$container['negotiator.acceptMiddleware']
    ->process($request, $handler);

$container['negotiator.acceptLanguageNegotiator']
    ->negotiate($request);

$container['negotiator.acceptLanguageMiddleware']
    ->process($request, $handler);

$container['negotiator.contentTypeNegotiator']
    ->negotiate($request);

$container['negotiator.contentTypeMiddleware']
    ->process($request, $handler);

ServiceFactory

Copyright

2024 Dominik Zogg

chubbyphp-negotiation's People

Contributors

dominikzogg avatar haeber avatar mavimo avatar timesplinter avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

chubbyphp-negotiation's Issues

BC in the NegotiatorInterface

In the version 1.8.1 BC was introduced with changing the return type of the method negotiate

-    public function negotiate(Request $request);
+    public function negotiate(Request $request): ?NegotiatedValueInterface;

AcceptNegotiator.php string handling

chubbyphp-negotiation/src/AcceptNegotiator.php:98 has an unsafe explode which can throw notices if the accept header string does not contain any slash

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.