Elgg\Database\SubtypeTable::getClassFromId PHP Method

getClassFromId() public method

Returns the class name for a subtype id.
See also: get_subtype_class()
See also: get_subtype_from_id()
public getClassFromId ( integer $subtype_id ) : string | null
$subtype_id integer The subtype id
return string | null
    public function getClassFromId($subtype_id)
    {
        if (!$subtype_id) {
            return null;
        }
        $cache = $this->getPopulatedCache();
        return isset($cache[$subtype_id]) ? $cache[$subtype_id]->class : null;
    }

Usage Example

Example #1
0
 /**
  * Create an Elgg* object from a given entity row.
  *
  * Handles loading all tables into the correct class.
  *
  * @see get_entity_as_row()
  * @see add_subtype()
  * @see get_entity()
  *
  * @access private
  *
  * @param stdClass $row The row of the entry in the entities table.
  * @return ElggEntity|false
  * @throws ClassException
  * @throws InstallationException
  */
 public function rowToElggStar($row)
 {
     if (!$row instanceof stdClass) {
         return $row;
     }
     if (!isset($row->guid) || !isset($row->subtype)) {
         return $row;
     }
     $class_name = $this->subtype_table->getClassFromId($row->subtype);
     if ($class_name && !class_exists($class_name)) {
         $this->logger->error("Class '{$class_name}' was not found, missing plugin?");
         $class_name = '';
     }
     if (!$class_name) {
         $map = ['object' => ElggObject::class, 'user' => ElggUser::class, 'group' => ElggGroup::class, 'site' => ElggSite::class];
         if (isset($map[$row->type])) {
             $class_name = $map[$row->type];
         } else {
             throw new InstallationException("Entity type {$row->type} is not supported.");
         }
     }
     $entity = new $class_name($row);
     if (!$entity instanceof ElggEntity) {
         throw new ClassException("{$class_name} must extend " . ElggEntity::class);
     }
     return $entity;
 }