[ Index ]

PHP Cross Reference of ACL Module

title

Body

[close]

/models/ -> Util.php (source)

   1  <?php
   2  
   3  /**

   4   * Utility Class 

   5   * @package acl.strategy.nestedSet.pathMaterialization

   6   * @author dispy <dispyfree@googlemail.com>

   7   * @license LGPLv2

   8   */
   9  class Util {
  10      
  11      /**

  12       * Returns the database-type of the given object

  13       * @param AclObject $obj

  14       * @return string

  15       * @throws InvalidArgumentException 

  16       */
  17      public static function getDataBaseType($obj){
  18          $class = get_class($obj);
  19          
  20          switch($class){
  21              case Strategy::getClass('Aro'):
  22              case Strategy::getClass('AroNode'):
  23              case 'RGroup':
  24                  return 'aro';
  25              case Strategy::getClass('Aco'):
  26              case Strategy::getClass('AcoNode'):
  27              case 'AGroup':
  28                  return 'aco';
  29              default:
  30                  throw new InvalidArgumentException('Unexpected Object');
  31          }
  32      }
  33      
  34      /**

  35       * Returns the class-name of the node belonging to the given Object

  36       * @param PmAclObject $obj

  37       * @return string

  38       * @throws InvalidArgumentException 

  39       */
  40      public static function getNodeNameOfObject(AclObject $obj){
  41          $class = get_class($obj);
  42          
  43          switch($class){
  44              case Strategy::getClass('Aro'):
  45              case 'RGroup':
  46                  return Strategy::getClass('AroNode');
  47              case Strategy::getClass('Aco'):
  48              case 'CGroup':
  49                  return Strategy::getClass('AcoNode');
  50              default:
  51                  throw new InvalidArgumentException('Unexpected Object');
  52          }
  53      }
  54      
  55      /**

  56       * Generates a database-IN-statement out of the given options

  57       * @param array[string] $options

  58       * @return string the in-statement 

  59       */
  60      public static function generateInStatement($options){
  61          $ret = ' IN ( ';
  62          
  63          foreach($options as $key =>$option){
  64              if($key > 0)
  65                  $ret.= ' , ';
  66  
  67              $ret.= ctype_digit($option) ? $option : ' "'.$option.'" ';
  68          }
  69          $ret.= ' ) ';
  70          
  71          return $ret;
  72      }
  73      
  74      /**

  75       * Extracts the IDs of the given bunch of objects and returns them in

  76       * an indexed arry

  77       * @param array $objects

  78       * @return array[int]

  79       */
  80      public static function getIdsOfObjects($objects){
  81          $ret = array();
  82          foreach($objects as $obj){
  83              $ret[] = $obj->id;
  84          }
  85          return $ret;
  86      }
  87  
  88      /**

  89       *  Retrieves the requested object by the given identifier (an object)

  90       *  Will throw an exception if the model-name is invalid but return NULL

  91       *  if the identifier doesn't match any instance.

  92       * @param AclObject identifier [model, foreign_key]

  93       */
  94      public function getByIdentifier($identifier){
  95          if($identifier->model == NULL || $identifier->foreign_key == NULL)
  96                  return NULL
  97  
  98          if(!is_subclass_of($identifier->model  "CActiveRecord"))
  99                  throw new RuntimeException('Invalid Identifier (model): '.$identifier->model);
 100  
 101          $class = $idenetifier->model;
 102          return $class::model()->findByPk($identifier->foreign_key);
 103      }
 104  
 105  }
 106  
 107  ?>


Generated: Sun Jul 1 19:24:45 2012 Cross-referenced by PHPXref 0.7.1