3v4l.org

run code in 300+ PHP versions simultaneously
<?php class ValidationException extends \Exception { } interface Validator { /** * @param mixed $data * * @throws ValidationException */ public function validate($data); } class ValidatorComposite { /** * @var Validator[] */ protected $validators; public function addValidator(Validator $validator) { $this->validators[] = $validator; } public function validate($data) { $exceptions = []; foreach ($this->validators as $validator) { try { $validator->validate($data); } catch (ValidationException $e) { $exceptions[] = $e->getMessage(); } /** Now you have a list of exceptions that were thrown in $exceptions **/ return $exceptions; } } } class ArrayKeyExistsValidator implements Validator { protected $requiredKeys = []; public function __construct(array $requiredKeys) { $this->requiredKeys = $requiredKeys; } public function validate($data) { if (!is_array($data)) { // either try to convert to array or throw exception } foreach ($this->requiredKeys as $requiredKey) { /** recursively search your array for each key**/ // I'm doing non recursively here if (!array_key_exists($requiredKey, $data)) { throw new ValidationException( sprintf('Key: "%s" not found in array: "%s"', $requiredKey, print_r($data, true)) ); } } } } // Object Usage $composite = new ValidatorComposite; $validator = new ArrayKeyExistsValidator(array('key1', 'key2')); $composite->addValidator($validator); $dataToValidate = array('LOL', 'key1', 'thereIsntAKey2'); $composite->validate($dataToValidate);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/s4MSY
function name:  (null)
number of ops:  16
compiled vars:  !0 = $composite, !1 = $validator, !2 = $dataToValidate
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   48     0  E >   DECLARE_CLASS                                            'arraykeyexistsvalidator'
   79     1        NEW                                              $3      'ValidatorComposite'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $3
   80     4        NEW                                              $6      'ArrayKeyExistsValidator'
          5        SEND_VAL_EX                                              <array>
          6        DO_FCALL                                      0          
          7        ASSIGN                                                   !1, $6
   81     8        INIT_METHOD_CALL                                         !0, 'addValidator'
          9        SEND_VAR_EX                                              !1
         10        DO_FCALL                                      0          
   83    11        ASSIGN                                                   !2, <array>
   84    12        INIT_METHOD_CALL                                         !0, 'validate'
         13        SEND_VAR_EX                                              !2
         14        DO_FCALL                                      0          
         15      > RETURN                                                   1

Class ValidationException: [no user functions]
Class Validator:
Function validate:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/s4MSY
function name:  validate
number of ops:  2
compiled vars:  !0 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   RECV                                             !0      
          1      > RETURN                                                   null

End of function validate

End of class Validator.

Class ValidatorComposite:
Function addvalidator:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/s4MSY
function name:  addValidator
number of ops:  5
compiled vars:  !0 = $validator
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   RECV                                             !0      
   24     1        FETCH_OBJ_W                                      $1      'validators'
          2        ASSIGN_DIM                                               $1
          3        OP_DATA                                                  !0
   25     4      > RETURN                                                   null

End of function addvalidator

Function validate:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 17
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 17
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
Found catch point at position: 9
Branch analysis from position: 9
2 jumps found. (Code = 107) Position 1 = 10, Position 2 = -2
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/s4MSY
function name:  validate
number of ops:  19
compiled vars:  !0 = $data, !1 = $exceptions, !2 = $validator, !3 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   RECV                                             !0      
   29     1        ASSIGN                                                   !1, <array>
   31     2        FETCH_OBJ_R                                      ~5      'validators'
          3      > FE_RESET_R                                       $6      ~5, ->17
          4    > > FE_FETCH_R                                               $6, !2, ->17
   35     5    >   INIT_METHOD_CALL                                         !2, 'validate'
          6        SEND_VAR_EX                                              !0
          7        DO_FCALL                                      0          
          8      > JMP                                                      ->14
   37     9  E > > CATCH                                       last         'ValidationException'
   39    10    >   INIT_METHOD_CALL                                         !3, 'getMessage'
         11        DO_FCALL                                      0  $9      
         12        ASSIGN_DIM                                               !1
         13        OP_DATA                                                  $9
   43    14    >   FE_FREE                                                  $6
         15      > RETURN                                                   !1
   31    16*       JMP                                                      ->4
         17    >   FE_FREE                                                  $6
   45    18      > RETURN                                                   null

End of function validate

End of class ValidatorComposite.

Class ArrayKeyExistsValidator:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/s4MSY
function name:  __construct
number of ops:  4
compiled vars:  !0 = $requiredKeys
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   52     0  E >   RECV                                             !0      
   54     1        ASSIGN_OBJ                                               'requiredKeys'
          2        OP_DATA                                                  !0
   55     3      > RETURN                                                   null

End of function __construct

Function validate:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 4
Branch analysis from position: 4
2 jumps found. (Code = 77) Position 1 = 6, Position 2 = 24
Branch analysis from position: 6
2 jumps found. (Code = 78) Position 1 = 7, Position 2 = 24
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 23
Branch analysis from position: 10
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 23
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
Branch analysis from position: 4
filename:       /in/s4MSY
function name:  validate
number of ops:  26
compiled vars:  !0 = $data, !1 = $requiredKey
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   57     0  E >   RECV                                             !0      
   59     1        TYPE_CHECK                                  128  ~2      !0
          2        BOOL_NOT                                         ~3      ~2
          3      > JMPZ                                                     ~3, ->4
   64     4    >   FETCH_OBJ_R                                      ~4      'requiredKeys'
          5      > FE_RESET_R                                       $5      ~4, ->24
          6    > > FE_FETCH_R                                               $5, !1, ->24
   68     7    >   ARRAY_KEY_EXISTS                                 ~6      !1, !0
          8        BOOL_NOT                                         ~7      ~6
          9      > JMPZ                                                     ~7, ->23
   70    10    >   NEW                                              $8      'ValidationException'
   71    11        INIT_FCALL                                               'sprintf'
         12        SEND_VAL                                                 'Key%3A+%22%25s%22+not+found+in+array%3A+%22%25s%22'
         13        SEND_VAR                                                 !1
         14        INIT_FCALL                                               'print_r'
         15        SEND_VAR                                                 !0
         16        SEND_VAL                                                 <true>
         17        DO_ICALL                                         $9      
         18        SEND_VAR                                                 $9
         19        DO_ICALL                                         $10     
         20        SEND_VAR_NO_REF_EX                                       $10
         21        DO_FCALL                                      0          
         22      > THROW                                         0          $8
   64    23    > > JMP                                                      ->6
         24    >   FE_FREE                                                  $5
   75    25      > RETURN                                                   null

End of function validate

End of class ArrayKeyExistsValidator.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
170.1 ms | 1409 KiB | 17 Q