Validating UIC for BULSTAT (Bulgaria)

Recently I had to validate an UIC for BULSTAT for a project of mine, but I couldn't find an implementation in PHP that would satisfy my need. And since the algorithm is described here (in Bulgarian, sorry), I decided to exercise a bit and write it out?