0

I am writing a rostering app. This statement checks that by booking an extra shift the user doesn't violate a rule whereby they have booked more than 7 night shifts in a row. This code runs fine but I am trying to find a more elegant way to write it, for instance using a for loop within the if statement. This snippet exists within a bigger while loop.

if (
    $original_shift->night_shift==true &&
    $p_lookback_night_7===[1,1,1,1,1,1,1] || $p_lookforward_night_7===[1,1,1,1,1,1,1] ||
    ($p_lookback_night_1===[1] && $p_lookforward_night_6===[1,1,1,1,1,1]) ||
    ($p_lookback_night_2===[1,1] && $p_lookforward_night_5===[1,1,1,1,1]) ||
    ($p_lookback_night_3===[1,1,1] && $p_lookforward_night_4===[1,1,1,1]) ||
    ($p_lookback_night_4===[1,1,1,1] && $p_lookforward_night_3===[1,1,1]) ||
    ($p_lookback_night_5===[1,1,1,1,1] && $p_lookforward_night_2===[1,1]) ||
    ($p_lookback_night_6===[1,1,1,1,1,1] && $p_lookforward_night_1===[1])
) {
    return 'You can\'t do more than 7 night shifts in a row'; 
    break;
}

The $p_look variables get populated by a loop looking either back of forward the specified number of days at the end of the variable name and returning an array of true or false for that number of days dependent on whether those are night shifts or not.

1
  • 2
    How do all the $p_lookback_night_7 (and $p_lookforward_night_7) variables get populated? Commented May 31, 2018 at 7:35

2 Answers 2

1

As an alternative to building several arrays and complex comparisons, this alternative just uses 2 arrays, one with the days prior and one looking forward. I'm not 100% sure if this includes the day they are trying to book off, but hopefully the idea is easy enough to adjust to your needs.

The basic concept is to look backwards through the $p_lookback_night list and count the 1's, stopping when it reaches a 0. It then does a similar thing through the $p_lookforward_night list. The end result is the number of 1's in a row...

$p_lookback_night = [0,0,0,0,1,1];
$p_lookforward_night = [1,1,1,1,0,0];
$run = 0;
foreach (array_reverse($p_lookback_night) as $test )  {
    if ( $test == 1 )    {
        $run++;
    }
    else    {
        break;
    }
}

foreach ($p_lookforward_night as $test )  {
    if ( $test == 1 )    {
        $run++;
    }
    else    {
        break;
    }
}

echo $run;

With the test data it gives 6, so you can use this to decide if they are trying to book 7 in a row.

Sign up to request clarification or add additional context in comments.

Comments

0

Assuming all those arrays can only contain 1 in this case you can simply just count the values

&& count($p_lookback_night_7)===7 || ...

Maybe even use the int at the end dynamically but this will be probably more trouble that it is worth. Something like

for($i=1;$i<8;$i++){
  if(count(${"p_lookback_night_".$i}) == $i && count(${"p_lookforward_night_".$i}) == $i ){
  ..wahtever
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.