webmozarts / assert

Assertions to validate method input/output with nice error messages.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Assert::alpha() only works with Latin characters

rossbearman opened this issue · comments

Because of the reliance on ctype_alpha, in most locales Unicode characters will be rejected, despite being very common in strings that you might want to check only contain letters.

It would be great to see a new assertion that accepts common letters like Å or Ö. Perhaps using the regex /^\p{L}*$/ would be most appropriate?

\p{L} will match any valid Unicode letters, in either case.

Confirmed, but should the validation also pass with Chinese characters and be independent of the active locale? As this will going to happen after the suggested change?

Thank you for opening this issue.

Currently, the locale is 'forced', so the ctype based asserts are independent of locale.

If this new assertion gets added, what do you think it should be named?
And how should it handle other cases like Chinese characters as @pablothedude mentioned?

I'd also like to point out that the regex you linked may not work as intended: https://3v4l.org/7M1LZ

Ah, the regex will need the u flag in order to enable PHP's Unicode support.

See: https://3v4l.org/sW6Z8

The name Assert::letter() would probably be most appropriate, as it is checking against Unicode's letter character class. As such, supporting Chinese letters would be consistent and clear.

Ah, the regex will need the u flag in order to enable PHP's Unicode support.

Guess i missed that, it does look more promising now. I think this fits a common enough use case to add this to the libary, so feel free to open a PR.