pepebe
6/24/2015 - 3:58 PM

Snippet to handle simple switch case conditions. Only supports "=="

Snippet to handle simple switch case conditions. Only supports "=="

<?php
/*
  ppb_switch snippet
  
  Simple snippet to handle simple switch/case conditions. Note: Only supports "=="

  switch (string/int)
  --------------------------------
  Some value returned by a tag (for example[[*id]])
  or by another snippet (for example [[UltimateParent]]).

  case (string)
  --------------------------------
  One or more condition sets. The syntax is the same as the one used by select or radio TVs. 
  Use || to divide condition sets
  and == to divide key value pairs.
   
  Example: condition_1==return_1||condition_2==return_2||etc...
  
  On top of this you can set a default value that is returned if none of the keys are found:
  ...||default==return_default

  Example:
  Use this snippet to display a chunk dependent on the current resource id:
  
  [[[[!ppb_switch?
    &switch=`[[*id]]`
    &case=`1==$chunk1||2==$chunk2||default==$chunk3`
  ]]]]
  
  If the current resource id is 1, this snippet will return $chunk1.
  If it is 2, it will return $chunk2.
  For everything else it will return $chunk3.
  
  The additional brackets around the snippet call will result in this: [[$chunk1]].

*/
    
    $case = explode ('||', $case);
    
    foreach ($case as $conditions)
    {
        
        $condition = explode('==', $conditions);
    
        if ($switch == $condition[0])
        {
            return $condition[1];
        }
        if ($condition[0] == 'default')
        {
            $default = $condition[1];
        }
    }
    return $default;