Giter Site home page Giter Site logo

badfaith's Introduction

Build Status

In Brief

BadFaith is a content negotiation framework which uses the HTTP spec to map variants that the web site supplies to what the user agent says it can use, similar to Apache's mod_negotiation functionality.

For example, a handset in Hong Kong wants a different pic format, different compression, different language, different charset than a desktop browser in the US.

BadFaith content (/kənˈtɛnt/) negotiation has no assumptions about your environment/framework: it takes an array of the Accept and Accept-* request-header fields you care about, keyed by name, parses them, and tells you which of the supplied variants you should use. If you don't supply that, it assumes you want to use the $_SERVER variable. If you want to use your framework request object, then you can naturally extend the class.

The Ideal API

<?php
  // If you wish to use $_SERVER
  $negotiator = new \BadFaith\Negotiator();

  // If that is not desirable, furnish these header fields some other way
  $acceptHeaders = array(
    'accept' => 'text/html;level=2;q=0.7,text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'accept_encoding' => 'gzip,deflate,sdch',
    'accept_language' => 'en-US,en;q=0.8',
    'accept_charset' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
  );
  $negotiator = new \BadFaith\Negotiator($acceptHeaders);

  //Get information on what that user-agent wants for one dimension of variation:
  $best_encoding = $negotiator->getPreferred('encoding')
  echo $best_encoding; //Outputs 'gzip'.

  //Get what would suit the UA best in all dimensions:
  $best_all = $negotatiator->getPreferred();
  var_export($best_all)
  //Outputs
  array(
    'accept' => 'text/html',
    'accept_encoding' => 'gzip',
    'accept_language' => 'en-US',
    'accept_charset' => 'ISO-8859-1',
  );

  // Provide the information on the variants you have for the resource
  // ...using a string:
  $negotiator->variants['charset'] = "UTF-8,iso-8859-1;q=0.9,UTF-16;q=0.5"
  // ...using an array:
  $negotiator->variants['charset'] = array(
    array(
      'type' => 'UTF-8',
      'q' => 1.0,
    ),
    array(
      'type' => 'iso-8859-1',
      'q' => 0.9,
    ),
    array(
      'type' => 'UTF-16',
      'q' => 0.5,
    ),
  );

  // Use that information to do real Content Negotiation:
  $negotiator->getBestVariant()

  // Or, if you're in a hurry:
  $negotiator new \BadFaith\Negotiator($headers, $variants);

  // Supply your own negotiation algorithm on the fly if you like
  $best = negotiator->getBestVariant('algoCallable');
?>

badfaith's People

Contributors

cboden avatar mikealmond avatar webchick avatar winmillwill avatar ygerasimov avatar

Watchers

 avatar

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.