コード例 #1
0
 /**
  * Method creates an Inspector instance and a utility function ($first) to return the
  * first item in a metadata array. Next, it loops through all the properties in the model, 
  * and sifts out all that have an @column flag. Any other properties are ignored at this point.
  * The column’s @type flag is checked to make sure it is valid, raising a 
  * Model\Exception\Type in the event that it is not. If the column’s type is valid, 
  * it is added to the $_columns property. Every valid $primary column leads to the 
  * incrementing of the $primaries variable, which is checked at the end 
  * of the method to make sure that exactly one primary column has been defined. 
  * In essence, this method takes the User model definition and returns an associative array of column data
  * 
  * @return array
  * @throws Exception\Type
  * @throws Exception\Primary
  */
 public function getColumns()
 {
     if (empty($this->_columns)) {
         $primaries = 0;
         $columns = array();
         $class = get_class($this);
         $types = $this->_types;
         $inspector = new Inspector($this);
         $properties = $inspector->getClassProperties();
         $first = function ($array, $key) {
             if (!empty($array[$key]) && count($array[$key]) == 1) {
                 return $array[$key][0];
             }
             return null;
         };
         foreach ($properties as $property) {
             $propertyMeta = $inspector->getPropertyMeta($property);
             if (!empty($propertyMeta['@column'])) {
                 $name = preg_replace('#^_#', '', $property);
                 $primary = !empty($propertyMeta['@primary']);
                 $type = $first($propertyMeta, '@type');
                 $length = $first($propertyMeta, '@length');
                 $index = !empty($propertyMeta['@index']);
                 $unique = !empty($propertyMeta['@unique']);
                 $readwrite = !empty($propertyMeta['@readwrite']);
                 $read = !empty($propertyMeta['@read']) || $readwrite;
                 $write = !empty($propertyMeta['@write']) || $readwrite;
                 $validate = !empty($propertyMeta['@validate']) ? $propertyMeta['@validate'] : false;
                 $label = $first($propertyMeta, '@label');
                 if (!in_array($type, $types)) {
                     throw new Exception\Type(sprintf('%s is not a valid type', $type));
                 }
                 if ($primary) {
                     $primaries++;
                 }
                 $columns[$name] = array('raw' => $property, 'name' => $name, 'primary' => $primary, 'type' => $type, 'length' => $length, 'index' => $index, 'unique' => $unique, 'read' => $read, 'write' => $write, 'validate' => $validate, 'label' => $label);
             }
         }
         if ($primaries !== 1) {
             throw new Exception\Primary(sprintf('%s must have exactly one @primary column', $primary));
         }
         $this->_columns = $columns;
     }
     return $this->_columns;
 }