2016-04-28 11 views
2

私は以下のコードを書いていますが、うまくいきますが、もっとうまくいくと感じています。いったん私は10の役割と10の異なる状態(新しい、チェックされた等)を持つと、配列は非常に大きくなります。クリーンな多次元配列を書く

誰も、状態と役割を渡し、ロックとロック解除状態を戻すことについて同じ解決策を持つソリューションをお持ちですか?

public function lockingPermissions($state, $role) 
{ 
    $lockingPermissions = [ 
     'technician' => [ 
      'new' => [ 
       'lock' => true, 
       'unlock' => false 
      ], 
      'checked' => [ 
       'lock' => true, 
       'unlock' => false 
      ] 
     ], 
       ..... 
    ]; 

    return $lockingPermissions[$role][$state]; 
} 

答えて

1

多次元配列は動的に塗りつぶされます。もっと動的

function lockingPermissions($state, $role) 
{ 
    $lockingPermissions = array(); 
    $roles = array('technician','executive','programmer','other'); 
    $states = array('new','checked','old','other'); 
    $options = array('lock' => true, 'unlock' => false); 

    $newStates = array(); 
    foreach($states as $value){ 
     $newStates[$value] = $options; 
    } 
    foreach($roles as $role){ 
     $lockingPermissions[$role] = $newStates; 
    } 
    //var_dump($lockingPermissions); 
    //Check the value 
    if(array_key_exists($role,$lockingPermissions)){ 
     if(array_key_exists($state,$lockingPermissions[$role])){ 
      return $lockingPermissions[$role][$state]; 
     }else{ 
      return false; 
     } 
    }else{ 
     return false; 
    } 


} 

var_dump(lockingPermissions("new","technician")); 
var_dump(lockingPermissions("checked","executive")); 

UPDATE、すべての

<?php 
function lockingPermissions($state, $role) 
{ 

    $lockingPermissions = array(); 
    $roles = array('technician','executive','programmer','other'); 
    $states = array('new' => 'options_type_1','checked'=> 'options_type_1','old'=> 'options_type_2','other'=> 'options_type_2'); 
    $options = array(
     'options_type_1' => array('lock' => true, 'unlock' => false), 
     'options_type_2' => array('lock' => true, 'unlock' => false, 'other' => true) 
    ); 

    $newStates = array(); 
    foreach($states as $key => $value){ 
     //$newStates[$value] = $options; 
     $newStates[$key] = $options[$value]; 
    } 
    foreach($roles as $role){ 
     $lockingPermissions[$role] = $newStates; 
    } 
    var_dump($lockingPermissions); 
    //Check the value 
    if(array_key_exists($role,$lockingPermissions)){ 
     if(array_key_exists($state,$lockingPermissions[$role])){ 
      return $lockingPermissions[$role][$state]; 
     }else{ 
      return false; 
     } 
    }else{ 
     return false; 
    } 


} 

var_dump(lockingPermissions("new","technician")); 
var_dump(lockingPermissions("checked","executive")); 
//var_dump(lockingPermissions("new","technicianx")); 
+0

これは、役割と状態ごとに異なるロックオプションに対応していますか?しかし、それは私が把握しようとしていたものです。私は、それぞれの州と役割ごとに異なるロックとロック解除のオプションが必要です。 – Jonathan

1

まず、あなたは空の値が何の許可を意味しないことを前提としなければなりません。このようにして、権限のないすべてのロール定義を省略できます。次に、フラグを使用できます。私はPHPの標準のように、一定の使用

define('LP_NEW_LOCK',  1);  # 2**0 
define('LP_NEW_UNLOCK',  2);  # 2**1 
define('LP_CHECKED_LOCK', 4);  # 2**2 
define('LP_CHECKED_UNLOCK', 8);  # 2**3 
(...) 
define('LP_LOCK_ALL',  349525 ); # 2**0|2**2|2**4|2**6|2**8|2**10|2**12|2**14|2**16|2**18 
define('LP_UNLOCK_ALL',  699050 ); # 2**1|2**3|2**5|2**7|2**9|2**11|2**13|2**15|2**17|2**19 
define('LP_ALL',   1048575); # 2**20-2**0 

が、あなたは(または直接整数)の変数を使用することができます:まず、フラグを定義します。フラグの規則は、それらがユニークな整数と2の累乗でなければならないということです(1,2,4,8,16,32、...)。 |ビット単位の演算子でグループ化フラグを作成することもできます。すなわち、フル特権の「新規」は3(= 1 | 2)になります。

その後、あなたはこのように配列を作成することができます

$lockingPermissions = [ 
    'admin'  => LP_ALL, 
    'technician' => LP_LOCK_ALL, 
    'role3'  => LP_NEW_LOCK | LP_NEW_UNLOCK | LP_CHECKED_LOCK, 
    'role4'  => LP_LOCK_ALL^LP_NEW_LOCK, # lock all except new 
    'role5'  => 0 # No permissions 
]; 

この時点で、あなたはこのように仮想的な関数を呼び出すことができます。

このように
$perm = $lockingPermissions[$role]; 

とチェック権限:

if($perm & LP_NEW_LOCK) ... 

変数を使用する場合は、関数内でグローバルとして宣言する必要があります。この場合、コードを単純化するために配列を使用することをお勧めします。