我一直在致力於實現PSR-7UriInterface,並且有關何時某個實現應該爲某些組件引發InvalidArgumentException的規範有點令人費解。PSR-7 UriInterface - 編碼百分比無效的組件
例如,UriInterface::withPath specifies throwing such an exception given an invalid path,但非常相同的docblock注意到,「用戶可以提供編碼和解碼的路徑字符」,因爲「實現確保正確的編碼」。
/**
* ...
*
* Users can provide both encoded and decoded path characters.
* Implementations ensure the correct encoding as outlined in getPath().
*
* @param string $path The path to use with the new instance.
* @return static A new instance with the specified path.
* @throws \InvalidArgumentException for invalid paths.
*/
該實現負責管理編碼在整個規範的其餘部分都被證明。
由於實現確保了正確的編碼,所以似乎遵循的是,實現的用戶可以將任意數量的其他無效字符傳遞到函數中,例如withPath,然後該函數將被正確編碼而不是觸發異常。
我能想到的唯一情況是,如果withPath傳遞了非字符串(無論它的值是什麼,似乎是Guzzle's interpretation of the specification),將會發生InvalidArgumentException。
真正嚴格閱讀PHP's brief introduction of InvalidArgumentException似乎可以避免這種「嚴格類型」的解釋,但我不禁想知道PHP-FIG是否有任何其他的想法,特別是考慮到URI語法的複雜性一般來說。
是否有任何情況下,UriInterface方法(如withPath在傳遞字符串時應引發異常?