定制 ejetar/accept-header-interpreter 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ejetar/accept-header-interpreter

Composer 安装命令:

composer require ejetar/accept-header-interpreter

包简介

Accept Header toolbox 🧰 to Laravel. Validation and interpretation based on RFC 7231, section 5.3.1 and 5.3.2. Conversion of the list of media types to ordered Laravel Collections (by priority, according to RFC).

README 文档

README

Table of Contents

About

Accept Header toolbox 🧰 to Laravel. Validation and interpretation based on RFC 7231, section 5.3.1 and 5.3.2. Conversion of the list of media types to ordered Laravel Collections (by priority, according to RFC).

Features

  • Validation: Validates if Accept Header content is valid, according to specification;
  • Conversion: Converts the list of media types to a Laravel Collection (automatically sorted by priority according to specification);

Installation

composer require ejetar/accept-header-interpreter

Examples

First Example: Entering valid content

use Ejetar\AcceptHeaderInterpreter\AcceptHeaderInterpreter;

try {
  $content = request()->headers->get('Accept'); //return accept header content
  //let's assume that $content is now: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
  
  $acceptHeaderInterpreter = new AcceptHeaderInterpreter($content);
  dd($acceptHeaderInterpreter->toCollection());
  
} catch (\Exception $ex) {
  echo $ex->getMessage();
}

/* the code above will print
Collection {#756 ▼
  #items: array:6 [▼
    0 => array:3 [▼
      "type" => "text"
      "subtype" => "html"
      "parameters" => array:1 [▼
        "q" => 1
      ]
    ]
    1 => array:3 [▼
      "type" => "application"
      "subtype" => "xhtml+xml"
      "parameters" => array:1 [▼
        "q" => 1
      ]
    ]
    3 => array:3 [▼
      "type" => "image"
      "subtype" => "webp"
      "parameters" => array:1 [▼
        "q" => 1
      ]
    ]
    4 => array:3 [▼
      "type" => "image"
      "subtype" => "apng"
      "parameters" => array:1 [▼
        "q" => 1
      ]
    ]
    2 => array:3 [▼
      "type" => "application"
      "subtype" => "xml"
      "parameters" => array:1 [▼
        "q" => "0.9"
      ]
    ]
    5 => array:3 [▼
      "type" => "*"
      "subtype" => "*"
      "parameters" => array:1 [▼
        "q" => "0.8"
      ]
    ]
  ]
}

REALIZE THAT THE ORDER OF MEDIA TYPES HAS BEEN MODIFIED
*/

Second Example: Entering invalid content

use Ejetar\AcceptHeaderInterpreter\AcceptHeaderInterpreter;

try {
  $content = request()->headers->get('Accept'); //return accept header content
  //let's assume that $content is now: text/html,application/xhtml+xml,application/xml;q=1.1,image/webp,image/apng,*/*;q=0.8
  //It is only allowed to inform values from 0 to 1 for the parameter Q, that is, the contents of this Accept Header is invalid
  
  $acceptHeaderInterpreter = new AcceptHeaderInterpreter($content);
  dd($acceptHeaderInterpreter->toCollection());
  
} catch (\Exception $ex) {
  echo $ex->getMessage();
}

/* the code above will print
Accept header value is invalid!
*/

Third Example: Priority

use Ejetar\AcceptHeaderInterpreter\AcceptHeaderInterpreter;

try {
  $content = request()->headers->get('Accept'); //return accept header content
  //let's assume that $content is now: */*, application/*, text/html, application/xhtml+xml
  
  $acceptHeaderInterpreter = new AcceptHeaderInterpreter($content);
  dd($acceptHeaderInterpreter->toCollection());
  
} catch (\Exception $ex) {
  echo $ex->getMessage();
}

/* the code above will print
Collection {#756 ▼
  #items: array:4 [▼
    2 => array:3 [▼
      "type" => " text"
      "subtype" => "html"
      "parameters" => array:1 [▼
        "q" => 1
      ]
    ]
    3 => array:3 [▼
      "type" => " application"
      "subtype" => "xhtml+xml"
      "parameters" => array:1 [▼
        "q" => 1
      ]
    ]
    1 => array:3 [▼
      "type" => " application"
      "subtype" => "*"
      "parameters" => array:1 [▼
        "q" => 1
      ]
    ]
    0 => array:3 [▼
      "type" => "*"
      "subtype" => "*"
      "parameters" => array:1 [▼
        "q" => 1
      ]
    ]
  ]
}

NOTE THAT ORDER OF THE MEDIA TYPES IS NO LONGER THE SAME, IT WAS MODIFIED AS A PRIORITY, ACCORDING TO THE SPECIFICATION.
*/

Fourth Example: Displaying the original content of the Accept Header

use Ejetar\AcceptHeaderInterpreter\AcceptHeaderInterpreter;

try {
  $content = request()->headers->get('Accept'); //return accept header content
  //let's assume that $content is now: application/json
  
  $acceptHeaderInterpreter = new AcceptHeaderInterpreter($content);
  echo $acceptHeaderInterpreter->getOriginalContent();
  
} catch (\Exception $ex) {
  echo $ex->getMessage();
}

/* the code above will print
application/json
*/

Changelog

Nothing for now...

Contributing

Contribute to this wonderful project, it will be a pleasure to have you with us. Let's help the free software community. You are invited to incorporate new features, make corrections, report bugs, and any other form of support. Don't forget to star in this repository! 😀

License

This library is a open-source software licensed under the MIT license.

统计信息

  • 总下载量: 20
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-07-23

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固