ejetar / accept-header-interpreter
Accept Header 工具箱 🧰,用于 Laravel。基于 RFC 7231,第 5.3.1 和 5.3.2 节进行验证和解释。将媒体类型列表转换为按优先级排序的 Laravel 集合(根据 RFC 进行排序)。
1.1.0-alpha
2020-04-03 15:09 UTC
Requires
- php: >=7.2.5
- illuminate/support: ~5.4.0|~5.5.0|~5.6.0|~5.7.0|~5.8.0|~6.0|~7.0
Requires (Dev)
- phpunit/phpunit: ^9.0
- symfony/var-dumper: ^5.0@dev
This package is auto-updated.
Last update: 2024-09-29 05:22:01 UTC
README
目录
关于
Laravel 的 Accept Header 工具箱 🧰。基于 RFC 7231,第 5.3.1 和 5.3.2 节 进行验证和解释。将媒体类型列表转换为按优先级排序的 Laravel 集合(根据 RFC 进行排序)。
功能
- 验证:验证 Accept Header 内容是否符合规范;
- 转换:将媒体类型列表转换为 Laravel 集合(根据规范自动按优先级排序);
安装
composer require ejetar/accept-header-interpreter
示例
第一个示例:输入有效内容
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 */
第二个示例:输入无效内容
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! */
第三个示例:优先级
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. */
第四个示例:显示 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 */
变更日志
目前没有...
贡献
为这个美妙的项目做出贡献,能与你一起合作将是一件愉快的事情。让我们帮助免费软件社区。欢迎你加入新功能的开发、错误修正、报告错误,以及任何其他形式的支持。别忘了在这个仓库中 star!😀
许可证
这个库是一个开源软件,使用 MIT 许可证授权。