3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Cart { protected array $cart = []; protected array $fees = []; public function __construct() { // generate test Cart $testProducts = [ ['id' => 1 , 'nb' => 22, 'price' => 23], ['id' => 383 , 'nb' => 38, 'price' => 19], ['id' => 412, 'nb' => 11, 'price' => 34], ]; foreach ($testProducts as $testProduct) { while ($testProduct['nb']-- > 0) { $this->addProduct($testProduct['id'], $testProduct['price']); } } } public function cart_contents(): array { return $this->cart; } public function addProduct(int $id, int $price) { $this->cart[] = ['product_id' => $id, 'price' => $price]; } public function add_fee(string $txt, float $price) { $this->fees[] = ['txt' => $txt, 'price' => $price]; } function apply_discount_for_quantity() { // For sample, I let you choose of to define the rules and send it here $discount_rules = [ ['eligible_product_ids' => [383, 411], 'required_count' => 12, 'discount_percent' => 4], ['eligible_product_ids' => [412], 'required_count' => 10, 'discount_percent' => 5], ]; // Will use counter per lligible product $discounts_counter_cache = []; // cycle through the products in the cart to count thecantidad de productos elegibles foreach ($this->cart_contents() as $product) { foreach ($discount_rules as $rule) { // if product is eligible to one of our discount rule if (!in_array($product['product_id'], $rule['eligible_product_ids'])) { // product not eligible for a discount continue; } // let's count of many product whe got $discounts_counter_cache[ $product['product_id'] ] = ($discounts_counter_cache[ $product['product_id'] ] ?? 0) + 1; // Apply per elligible product only when it's reach the limit if ($discounts_counter_cache[ $product['product_id'] ] == $rule['required_count']) { echo sprintf(' -- Found discount of %d%% to apply on the batch of %d product (id %d) in the cart' . PHP_EOL, $rule['discount_percent'], $rule['required_count'], $product['product_id'] ); // We trigger the discount $this->add_fee( 'Descuento por cantidad: ' . $rule['discount_percent'] .'% por '. $rule['required_count'] .'x'. $product['product_id'], // You can also set the value in the rule -($product['price'] * $rule['required_count'] * ($rule['discount_percent'] / 100)) ); // Reset counter $discounts_counter_cache[ $product['product_id'] ] = 0; } } } } public function show_cart() { $products = []; foreach ($this->cart as $entry) { if (!isset($products[$entry['product_id']])) { $products[$entry['product_id']] = [ 'id' => $entry['product_id'], 'nb' => 0, 'price' => $entry['price'], 'total' => $entry['price'], ]; } $products[$entry['product_id']]['nb']++; $products[$entry['product_id']]['price'] += $entry['price']; } echo 'Cart:'. PHP_EOL; foreach ($products as $product) { echo sprintf( '> Product Id:"%d" - %d$ x %d = %d$'.PHP_EOL, $product['id'], $product['price'], $product['nb'], $product['total'] ); } echo PHP_EOL; if (!empty($this->fees)) { echo 'Fees:'.PHP_EOL; $totalFees = ['nb' => 0, 'total' => 0]; foreach ($this->fees as $fee) { $totalFees['nb']++; $totalFees['total']+= $fee['price']; echo sprintf( '> %d$ - %s' . PHP_EOL, $fee['price'], $fee['txt'] ); } echo sprintf( '>> Total of %d discounts for %d$' . PHP_EOL, $totalFees['nb'], $totalFees['total'] ); } } } $cart = new Cart(); echo 'Before discount:'.PHP_EOL; echo $cart->show_cart(); $cart->apply_discount_for_quantity(); echo PHP_EOL.'After discount:'.PHP_EOL; echo $cart->show_cart();
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ot8A5h
function name:  (null)
number of ops:  14
compiled vars:  !0 = $cart
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  137     0  E >   NEW                                              $1      'Cart'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $1
  138     3        ECHO                                                     'Before+discount%3A%0A'
  139     4        INIT_METHOD_CALL                                         !0, 'show_cart'
          5        DO_FCALL                                      0  $4      
          6        ECHO                                                     $4
  140     7        INIT_METHOD_CALL                                         !0, 'apply_discount_for_quantity'
          8        DO_FCALL                                      0          
  141     9        ECHO                                                     '%0AAfter+discount%3A%0A'
  142    10        INIT_METHOD_CALL                                         !0, 'show_cart'
         11        DO_FCALL                                      0  $6      
         12        ECHO                                                     $6
         13      > RETURN                                                   1

Class Cart:
Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 17
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 17
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
2 jumps found. (Code = 44) Position 1 = 16, Position 2 = 4
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 4
2 jumps found. (Code = 44) Position 1 = 16, Position 2 = 4
Branch analysis from position: 16
Branch analysis from position: 4
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
filename:       /in/ot8A5h
function name:  __construct
number of ops:  19
compiled vars:  !0 = $testProducts, !1 = $testProduct
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E >   ASSIGN                                                   !0, <array>
   16     1      > FE_RESET_R                                       $3      !0, ->17
          2    > > FE_FETCH_R                                               $3, !1, ->17
   17     3    > > JMP                                                      ->12
   18     4    >   INIT_METHOD_CALL                                         'addProduct'
          5        CHECK_FUNC_ARG                                           
          6        FETCH_DIM_FUNC_ARG                               $4      !1, 'id'
          7        SEND_FUNC_ARG                                            $4
          8        CHECK_FUNC_ARG                                           
          9        FETCH_DIM_FUNC_ARG                               $5      !1, 'price'
         10        SEND_FUNC_ARG                                            $5
         11        DO_FCALL                                      0          
   17    12    >   FETCH_DIM_RW                                     $7      !1, 'nb'
         13        POST_DEC                                         ~8      $7
         14        IS_SMALLER                                               0, ~8
         15      > JMPNZ                                                    ~9, ->4
   16    16    > > JMP                                                      ->2
         17    >   FE_FREE                                                  $3
   21    18      > RETURN                                                   null

End of function __construct

Function cart_contents:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ot8A5h
function name:  cart_contents
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   FETCH_OBJ_R                                      ~0      'cart'
          1        VERIFY_RETURN_TYPE                                       ~0
          2      > RETURN                                                   ~0
   26     3*       VERIFY_RETURN_TYPE                                       
          4*     > RETURN                                                   null

End of function cart_contents

Function addproduct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ot8A5h
function name:  addProduct
number of ops:  8
compiled vars:  !0 = $id, !1 = $price
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   28     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   30     2        INIT_ARRAY                                       ~4      !0, 'product_id'
          3        ADD_ARRAY_ELEMENT                                ~4      !1, 'price'
          4        FETCH_OBJ_W                                      $2      'cart'
          5        ASSIGN_DIM                                               $2
          6        OP_DATA                                                  ~4
   31     7      > RETURN                                                   null

End of function addproduct

Function add_fee:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ot8A5h
function name:  add_fee
number of ops:  8
compiled vars:  !0 = $txt, !1 = $price
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   35     2        INIT_ARRAY                                       ~4      !0, 'txt'
          3        ADD_ARRAY_ELEMENT                                ~4      !1, 'price'
          4        FETCH_OBJ_W                                      $2      'fees'
          5        ASSIGN_DIM                                               $2
          6        OP_DATA                                                  ~4
   36     7      > RETURN                                                   null

End of function add_fee

Function apply_discount_for_quantity:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 65
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 65
Branch analysis from position: 6
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 63
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 63
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 17
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 30, Position 2 = 62
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 62
Branch analysis from position: 63
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 63
Branch analysis from position: 65
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 65
filename:       /in/ot8A5h
function name:  apply_discount_for_quantity
number of ops:  67
compiled vars:  !0 = $discount_rules, !1 = $discounts_counter_cache, !2 = $product, !3 = $rule
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   41     0  E >   ASSIGN                                                   !0, <array>
   47     1        ASSIGN                                                   !1, <array>
   50     2        INIT_METHOD_CALL                                         'cart_contents'
          3        DO_FCALL                                      0  $6      
          4      > FE_RESET_R                                       $7      $6, ->65
          5    > > FE_FETCH_R                                               $7, !2, ->65
   52     6    > > FE_RESET_R                                       $8      !0, ->63
          7    > > FE_FETCH_R                                               $8, !3, ->63
   54     8    >   INIT_FCALL                                               'in_array'
          9        FETCH_DIM_R                                      ~9      !2, 'product_id'
         10        SEND_VAL                                                 ~9
         11        FETCH_DIM_R                                      ~10     !3, 'eligible_product_ids'
         12        SEND_VAL                                                 ~10
         13        DO_ICALL                                         $11     
         14        BOOL_NOT                                         ~12     $11
         15      > JMPZ                                                     ~12, ->17
   56    16    > > JMP                                                      ->7
   60    17    >   FETCH_DIM_R                                      ~13     !2, 'product_id'
         18        FETCH_DIM_R                                      ~15     !2, 'product_id'
         19        FETCH_DIM_IS                                     ~16     !1, ~15
         20        COALESCE                                         ~17     ~16
         21        QM_ASSIGN                                        ~17     0
         22        ADD                                              ~18     ~17, 1
         23        ASSIGN_DIM                                               !1, ~13
         24        OP_DATA                                                  ~18
   63    25        FETCH_DIM_R                                      ~19     !2, 'product_id'
         26        FETCH_DIM_R                                      ~20     !1, ~19
         27        FETCH_DIM_R                                      ~21     !3, 'required_count'
         28        IS_EQUAL                                                 ~20, ~21
         29      > JMPZ                                                     ~22, ->62
   64    30    >   INIT_FCALL                                               'sprintf'
         31        SEND_VAL                                                 '+--+Found+discount+of+%25d%25%25+to+apply+on+the+batch+of+%25d+product+%28id+%25d%29+in+the+cart%0A'
   65    32        FETCH_DIM_R                                      ~23     !3, 'discount_percent'
         33        SEND_VAL                                                 ~23
   66    34        FETCH_DIM_R                                      ~24     !3, 'required_count'
         35        SEND_VAL                                                 ~24
   67    36        FETCH_DIM_R                                      ~25     !2, 'product_id'
         37        SEND_VAL                                                 ~25
   64    38        DO_ICALL                                         $26     
   67    39        ECHO                                                     $26
   70    40        INIT_METHOD_CALL                                         'add_fee'
   71    41        FETCH_DIM_R                                      ~27     !3, 'discount_percent'
         42        CONCAT                                           ~28     'Descuento+por+cantidad%3A+', ~27
         43        CONCAT                                           ~29     ~28, '%25+por+'
         44        FETCH_DIM_R                                      ~30     !3, 'required_count'
         45        CONCAT                                           ~31     ~29, ~30
         46        CONCAT                                           ~32     ~31, 'x'
         47        FETCH_DIM_R                                      ~33     !2, 'product_id'
         48        CONCAT                                           ~34     ~32, ~33
         49        SEND_VAL_EX                                              ~34
   73    50        FETCH_DIM_R                                      ~35     !2, 'price'
         51        FETCH_DIM_R                                      ~36     !3, 'required_count'
         52        MUL                                              ~37     ~35, ~36
         53        FETCH_DIM_R                                      ~38     !3, 'discount_percent'
         54        DIV                                              ~39     ~38, 100
         55        MUL                                              ~40     ~37, ~39
         56        MUL                                              ~41     ~40, -1
         57        SEND_VAL_EX                                              ~41
   70    58        DO_FCALL                                      0          
   77    59        FETCH_DIM_R                                      ~43     !2, 'product_id'
         60        ASSIGN_DIM                                               !1, ~43
         61        OP_DATA                                                  0
   52    62    > > JMP                                                      ->7
         63    >   FE_FREE                                                  $8
   50    64      > JMP                                                      ->5
         65    >   FE_FREE                                                  $7
   83    66      > RETURN                                                   null

End of function apply_discount_for_quantity

Function show_cart:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 28
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 28
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 18
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 18
Branch analysis from position: 28
2 jumps found. (Code = 77) Position 1 = 31, Position 2 = 45
Branch analysis from position: 31
2 jumps found. (Code = 78) Position 1 = 32, Position 2 = 45
Branch analysis from position: 32
1 jumps found. (Code = 42) Position 1 = 31
Branch analysis from position: 31
Branch analysis from position: 45
2 jumps found. (Code = 43) Position 1 = 50, Position 2 = 78
Branch analysis from position: 50
2 jumps found. (Code = 77) Position 1 = 54, Position 2 = 69
Branch analysis from position: 54
2 jumps found. (Code = 78) Position 1 = 55, Position 2 = 69
Branch analysis from position: 55
1 jumps found. (Code = 42) Position 1 = 54
Branch analysis from position: 54
Branch analysis from position: 69
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 69
Branch analysis from position: 78
Branch analysis from position: 45
Branch analysis from position: 28
filename:       /in/ot8A5h
function name:  show_cart
number of ops:  79
compiled vars:  !0 = $products, !1 = $entry, !2 = $product, !3 = $totalFees, !4 = $fee
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   88     0  E >   ASSIGN                                                   !0, <array>
   89     1        FETCH_OBJ_R                                      ~6      'cart'
          2      > FE_RESET_R                                       $7      ~6, ->28
          3    > > FE_FETCH_R                                               $7, !1, ->28
   90     4    >   FETCH_DIM_R                                      ~8      !1, 'product_id'
          5        ISSET_ISEMPTY_DIM_OBJ                         0  ~9      !0, ~8
          6        BOOL_NOT                                         ~10     ~9
          7      > JMPZ                                                     ~10, ->18
   91     8    >   FETCH_DIM_R                                      ~11     !1, 'product_id'
   92     9        FETCH_DIM_R                                      ~13     !1, 'product_id'
         10        INIT_ARRAY                                       ~14     ~13, 'id'
   93    11        ADD_ARRAY_ELEMENT                                ~14     0, 'nb'
   94    12        FETCH_DIM_R                                      ~15     !1, 'price'
         13        ADD_ARRAY_ELEMENT                                ~14     ~15, 'price'
   95    14        FETCH_DIM_R                                      ~16     !1, 'price'
         15        ADD_ARRAY_ELEMENT                                ~14     ~16, 'total'
   91    16        ASSIGN_DIM                                               !0, ~11
   95    17        OP_DATA                                                  ~14
   98    18    >   FETCH_DIM_R                                      ~17     !1, 'product_id'
         19        FETCH_DIM_RW                                     $18     !0, ~17
         20        FETCH_DIM_RW                                     $19     $18, 'nb'
         21        PRE_INC                                                  $19
   99    22        FETCH_DIM_R                                      ~21     !1, 'product_id'
         23        FETCH_DIM_R                                      ~24     !1, 'price'
         24        FETCH_DIM_RW                                     $22     !0, ~21
         25        ASSIGN_DIM_OP                +=               1          $22, 'price'
         26        OP_DATA                                                  ~24
   89    27      > JMP                                                      ->3
         28    >   FE_FREE                                                  $7
  101    29        ECHO                                                     'Cart%3A%0A'
  102    30      > FE_RESET_R                                       $25     !0, ->45
         31    > > FE_FETCH_R                                               $25, !2, ->45
  103    32    >   INIT_FCALL                                               'sprintf'
  104    33        SEND_VAL                                                 '%3E+Product+Id%3A%22%25d%22+-+%25d%24+x+%25d+%3D+%25d%24%0A'
  105    34        FETCH_DIM_R                                      ~26     !2, 'id'
         35        SEND_VAL                                                 ~26
  106    36        FETCH_DIM_R                                      ~27     !2, 'price'
         37        SEND_VAL                                                 ~27
  107    38        FETCH_DIM_R                                      ~28     !2, 'nb'
         39        SEND_VAL                                                 ~28
  108    40        FETCH_DIM_R                                      ~29     !2, 'total'
         41        SEND_VAL                                                 ~29
  103    42        DO_ICALL                                         $30     
  108    43        ECHO                                                     $30
  102    44      > JMP                                                      ->31
         45    >   FE_FREE                                                  $25
  112    46        ECHO                                                     '%0A'
  113    47        ISSET_ISEMPTY_PROP_OBJ                           ~31     'fees'
         48        BOOL_NOT                                         ~32     ~31
         49      > JMPZ                                                     ~32, ->78
  114    50    >   ECHO                                                     'Fees%3A%0A'
  115    51        ASSIGN                                                   !3, <array>
  116    52        FETCH_OBJ_R                                      ~34     'fees'
         53      > FE_RESET_R                                       $35     ~34, ->69
         54    > > FE_FETCH_R                                               $35, !4, ->69
  117    55    >   FETCH_DIM_RW                                     $36     !3, 'nb'
         56        PRE_INC                                                  $36
  118    57        FETCH_DIM_R                                      ~39     !4, 'price'
         58        ASSIGN_DIM_OP                +=               1          !3, 'total'
         59        OP_DATA                                                  ~39
  119    60        INIT_FCALL                                               'sprintf'
  120    61        SEND_VAL                                                 '%3E+%25d%24+-+%25s%0A'
  121    62        FETCH_DIM_R                                      ~40     !4, 'price'
         63        SEND_VAL                                                 ~40
  122    64        FETCH_DIM_R                                      ~41     !4, 'txt'
         65        SEND_VAL                                                 ~41
  119    66        DO_ICALL                                         $42     
  122    67        ECHO                                                     $42
  116    68      > JMP                                                      ->54
         69    >   FE_FREE                                                  $35
  125    70        INIT_FCALL                                               'sprintf'
  126    71        SEND_VAL                                                 '%3E%3E+Total+of+%25d+discounts+for+%25d%24%0A'
  127    72        FETCH_DIM_R                                      ~43     !3, 'nb'
         73        SEND_VAL                                                 ~43
  128    74        FETCH_DIM_R                                      ~44     !3, 'total'
         75        SEND_VAL                                                 ~44
  125    76        DO_ICALL                                         $45     
  128    77        ECHO                                                     $45
  132    78    > > RETURN                                                   null

End of function show_cart

End of class Cart.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
264.88 ms | 1032 KiB | 16 Q