3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Number { /** * Assert if a number can be validate using Luhn Checksum * * Applies to : * Can be applid to : Siren, Siret, Credit card number, IMEI Number * Modulo can be overrided for custom control * simpleSum just sum all digits... * * @todo est-ce que l'on garde la somme simple au milieu de luhn... * * @param string|int $number * @param int $modulo * @param boolean $simpleSum * @return bool */ public static function assertLuhn($number, $modulo = 10, $simpleSum = false) { // Must be numeric and not a float if (!is_numeric($number) || is_float($number)) { return false; } // Begin Luhn Check $digits = array_reverse(str_split((string) $number)); $count = count($digits); $total = 0; for ($i = 0; $i < $count; ++$i) { if (($i + 1) % 2 !== 0 || $simpleSum) { $total += (int) $digits[$i]; } else { $total += array_sum(str_split((string) ((int) $digits[$i] * 2))); } } return $total % $modulo === 0; } /** * Assert if a number is a valid siren * * @param string|int $number * @return bool */ public static function assertSiren($number) { if (!self::assertLuhn($number)) { return false; } if (strlen($number) > 9 || strlen($number) < 5) { return false; } if (substr($number, 0, 5) === '00000') { return false; } return true; } /** * Assert if a number is a valid siren * * @param string|int $number * @return bool */ public static function assertSiret($number) { // @todo : extraction siren à mettre ailleurs $siren = substr($number, 0, 9); $modulo = 10; $simpleSum = false; // Le cas de "la poste", il y a plus de 10 000 etablissement, le modulo 10 ne fonctionne pas // Le modulo 5 est utilisé à la place // Sauf pour 35600000000048 qui est un siret validé avec luhn if ((int) $siren === 356000000 && (int) $number !== 35600000000048) { $modulo = 5; $simpleSum = true; } if (!self::assertSiren($siren) || !self::assertLuhn($number, $modulo, $simpleSum)) { return false; } if (strlen($number) > 14 || strlen($number) < 10) { return false; } return true; } } // (FR)?[0-9A-Z]{2}[0-9]{9} preg_match('/(FR)?[0-9A-Z]{2}[0-9]{9}/', 'FR 52 056800659', $matches); var_export(Number::assertSiret(str_replace(' ', '', '056 800 659 00155'))); var_export($matches);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/rPm2Y
function name:  (null)
number of ops:  20
compiled vars:  !0 = $matches
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  100     0  E >   INIT_FCALL                                               'preg_match'
          1        SEND_VAL                                                 '%2F%28FR%29%3F%5B0-9A-Z%5D%7B2%7D%5B0-9%5D%7B9%7D%2F'
          2        SEND_VAL                                                 'FR+52+056800659'
          3        SEND_REF                                                 !0
          4        DO_ICALL                                                 
  102     5        INIT_FCALL                                               'var_export'
          6        INIT_STATIC_METHOD_CALL                                  'Number', 'assertSiret'
          7        INIT_FCALL                                               'str_replace'
          8        SEND_VAL                                                 '+'
          9        SEND_VAL                                                 ''
         10        SEND_VAL                                                 '056+800+659+00155'
         11        DO_ICALL                                         $2      
         12        SEND_VAR                                                 $2
         13        DO_FCALL                                      0  $3      
         14        SEND_VAR                                                 $3
         15        DO_ICALL                                                 
  103    16        INIT_FCALL                                               'var_export'
         17        SEND_VAR                                                 !0
         18        DO_ICALL                                                 
         19      > RETURN                                                   1

Class Number:
Function assertluhn:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 47) Position 1 = 8, Position 2 = 10
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 12
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
2 jumps found. (Code = 44) Position 1 = 49, Position 2 = 25
Branch analysis from position: 49
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
2 jumps found. (Code = 47) Position 1 = 29, Position 2 = 30
Branch analysis from position: 29
2 jumps found. (Code = 43) Position 1 = 31, Position 2 = 35
Branch analysis from position: 31
1 jumps found. (Code = 42) Position 1 = 46
Branch analysis from position: 46
2 jumps found. (Code = 44) Position 1 = 49, Position 2 = 25
Branch analysis from position: 49
Branch analysis from position: 25
Branch analysis from position: 35
2 jumps found. (Code = 44) Position 1 = 49, Position 2 = 25
Branch analysis from position: 49
Branch analysis from position: 25
Branch analysis from position: 30
Branch analysis from position: 10
filename:       /in/rPm2Y
function name:  assertLuhn
number of ops:  53
compiled vars:  !0 = $number, !1 = $modulo, !2 = $simpleSum, !3 = $digits, !4 = $count, !5 = $total, !6 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   20     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      10
          2        RECV_INIT                                        !2      <false>
   24     3        INIT_FCALL                                               'is_numeric'
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $7      
          6        BOOL_NOT                                         ~8      $7
          7      > JMPNZ_EX                                         ~8      ~8, ->10
          8    >   TYPE_CHECK                                   32  ~9      !0
          9        BOOL                                             ~8      ~9
         10    > > JMPZ                                                     ~8, ->12
   25    11    > > RETURN                                                   <false>
   29    12    >   INIT_FCALL                                               'array_reverse'
         13        INIT_FCALL                                               'str_split'
         14        CAST                                          6  ~10     !0
         15        SEND_VAL                                                 ~10
         16        DO_ICALL                                         $11     
         17        SEND_VAR                                                 $11
         18        DO_ICALL                                         $12     
         19        ASSIGN                                                   !3, $12
   30    20        COUNT                                            ~14     !3
         21        ASSIGN                                                   !4, ~14
   31    22        ASSIGN                                                   !5, 0
   32    23        ASSIGN                                                   !6, 0
         24      > JMP                                                      ->47
   33    25    >   ADD                                              ~18     !6, 1
         26        MOD                                              ~19     ~18, 2
         27        IS_NOT_IDENTICAL                                 ~20     ~19, 0
         28      > JMPNZ_EX                                         ~20     ~20, ->30
         29    >   BOOL                                             ~20     !2
         30    > > JMPZ                                                     ~20, ->35
   34    31    >   FETCH_DIM_R                                      ~21     !3, !6
         32        CAST                                          4  ~22     ~21
         33        ASSIGN_OP                                     1          !5, ~22
         34      > JMP                                                      ->46
   36    35    >   INIT_FCALL                                               'array_sum'
         36        INIT_FCALL                                               'str_split'
         37        FETCH_DIM_R                                      ~24     !3, !6
         38        CAST                                          4  ~25     ~24
         39        MUL                                              ~26     ~25, 2
         40        CAST                                          6  ~27     ~26
         41        SEND_VAL                                                 ~27
         42        DO_ICALL                                         $28     
         43        SEND_VAR                                                 $28
         44        DO_ICALL                                         $29     
         45        ASSIGN_OP                                     1          !5, $29
   32    46    >   PRE_INC                                                  !6
         47    >   IS_SMALLER                                               !6, !4
         48      > JMPNZ                                                    ~32, ->25
   40    49    >   MOD                                              ~33     !5, !1
         50        IS_IDENTICAL                                     ~34     ~33, 0
         51      > RETURN                                                   ~34
   41    52*     > RETURN                                                   null

End of function assertluhn

Function assertsiren:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 7
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
2 jumps found. (Code = 47) Position 1 = 10, Position 2 = 13
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 15
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 23
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
filename:       /in/rPm2Y
function name:  assertSiren
number of ops:  25
compiled vars:  !0 = $number
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   49     0  E >   RECV                                             !0      
   51     1        INIT_STATIC_METHOD_CALL                                  'assertLuhn'
          2        SEND_VAR                                                 !0
          3        DO_FCALL                                      0  $1      
          4        BOOL_NOT                                         ~2      $1
          5      > JMPZ                                                     ~2, ->7
   52     6    > > RETURN                                                   <false>
   55     7    >   STRLEN                                           ~3      !0
          8        IS_SMALLER                                       ~4      9, ~3
          9      > JMPNZ_EX                                         ~4      ~4, ->13
         10    >   STRLEN                                           ~5      !0
         11        IS_SMALLER                                       ~6      ~5, 5
         12        BOOL                                             ~4      ~6
         13    > > JMPZ                                                     ~4, ->15
   56    14    > > RETURN                                                   <false>
   59    15    >   INIT_FCALL                                               'substr'
         16        SEND_VAR                                                 !0
         17        SEND_VAL                                                 0
         18        SEND_VAL                                                 5
         19        DO_ICALL                                         $7      
         20        IS_IDENTICAL                                             $7, '00000'
         21      > JMPZ                                                     ~8, ->23
   60    22    > > RETURN                                                   <false>
   63    23    > > RETURN                                                   <true>
   64    24*     > RETURN                                                   null

End of function assertsiren

Function assertsiret:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 12, Position 2 = 15
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 18
Branch analysis from position: 16
2 jumps found. (Code = 47) Position 1 = 23, Position 2 = 30
Branch analysis from position: 23
2 jumps found. (Code = 43) Position 1 = 31, Position 2 = 32
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 32
2 jumps found. (Code = 47) Position 1 = 35, Position 2 = 38
Branch analysis from position: 35
2 jumps found. (Code = 43) Position 1 = 39, Position 2 = 40
Branch analysis from position: 39
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 40
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 38
Branch analysis from position: 30
Branch analysis from position: 18
Branch analysis from position: 15
filename:       /in/rPm2Y
function name:  assertSiret
number of ops:  42
compiled vars:  !0 = $number, !1 = $siren, !2 = $modulo, !3 = $simpleSum
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   72     0  E >   RECV                                             !0      
   75     1        INIT_FCALL                                               'substr'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 0
          4        SEND_VAL                                                 9
          5        DO_ICALL                                         $4      
          6        ASSIGN                                                   !1, $4
   76     7        ASSIGN                                                   !2, 10
   77     8        ASSIGN                                                   !3, <false>
   82     9        CAST                                          4  ~8      !1
         10        IS_IDENTICAL                                     ~9      ~8, 356000000
         11      > JMPZ_EX                                          ~9      ~9, ->15
         12    >   CAST                                          4  ~10     !0
         13        IS_NOT_IDENTICAL                                 ~11     ~10, 35600000000048
         14        BOOL                                             ~9      ~11
         15    > > JMPZ                                                     ~9, ->18
   83    16    >   ASSIGN                                                   !2, 5
   84    17        ASSIGN                                                   !3, <true>
   87    18    >   INIT_STATIC_METHOD_CALL                                  'assertSiren'
         19        SEND_VAR                                                 !1
         20        DO_FCALL                                      0  $14     
         21        BOOL_NOT                                         ~15     $14
         22      > JMPNZ_EX                                         ~15     ~15, ->30
         23    >   INIT_STATIC_METHOD_CALL                                  'assertLuhn'
         24        SEND_VAR                                                 !0
         25        SEND_VAR                                                 !2
         26        SEND_VAR                                                 !3
         27        DO_FCALL                                      0  $16     
         28        BOOL_NOT                                         ~17     $16
         29        BOOL                                             ~15     ~17
         30    > > JMPZ                                                     ~15, ->32
   88    31    > > RETURN                                                   <false>
   91    32    >   STRLEN                                           ~18     !0
         33        IS_SMALLER                                       ~19     14, ~18
         34      > JMPNZ_EX                                         ~19     ~19, ->38
         35    >   STRLEN                                           ~20     !0
         36        IS_SMALLER                                       ~21     ~20, 10
         37        BOOL                                             ~19     ~21
         38    > > JMPZ                                                     ~19, ->40
   92    39    > > RETURN                                                   <false>
   95    40    > > RETURN                                                   <true>
   96    41*     > RETURN                                                   null

End of function assertsiret

End of class Number.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
151.29 ms | 953 KiB | 30 Q