name = $name; $required = ['type', 'label']; foreach ($required as $r) { if (!array_key_exists($r, $config)) { error_log('[JVBase\registrar\Field] Missing required '.$r.' for field'); return; } } $current = get_class($this); $class = match($config['type']) { 'group' => ($current !== GroupedField::class) ? new GroupedField($name, $config) : $this, 'select', 'radio', 'checkbox' => ($current !== OptionsField::class) ? new OptionsField($name, $config) : $this, 'repeater' => ($current !== RepeaterField::class) ? new RepeaterField($name, $config) : $this, 'taglist' => ($current !== TagListField::class) ? new TagListField($name, $config) : $this, 'taxonomy', 'post', 'user', 'selector' => ($current !== SelectorField::class) ? new SelectorField($name, $config) : $this, 'upload' => ($current !== UploadField::class) ? new UploadField($name, $config) : $this, default => $this }; foreach ($config as $key => $value) { if (property_exists($class, $key)) { $method = 'set' . ucfirst($key); $class->$method($value); } else { error_log('Instance: '.print_r($class, true)); error_log('[JVBase\registrar\Field] Invalid key for '.$name.': '.$key); } } } public function setDescription(string $description):void { $this->description = $description; } public function getDescription():string { return $this->description; } public function setHint(string $hint):void { $this->hint = $hint; } public function getHint():string { return $this->hint; } protected function setType(string $type):void{ $allowed = array_keys(MetaTypeManager::getTypes()); if (!in_array($type, $allowed)) { error_log('[JVBase\registrar\Field] Invalid type attempted '.$type); return; } $this->type = $type; } protected function setLabel(string $label):void{ $this->label = $label; } protected function setRequired(bool $required):void{ $this->required = $required; } protected function setHidden(bool $hidden):void{ $this->hidden = $hidden; } protected function setQuickEdit(bool $quickEdit):void{ $this->quickEdit = $quickEdit; } protected function setDefault(mixed $default):void { $this->default = $default; } protected function setQuill(bool $quill):void { $this->quill = $quill; } public function setForAll(bool $set):void { $this->for_all = $set; } public function getForAll():?bool { return $this->for_all??null; } public function setSection(string $section):void { $this->section = $section; } public function getSection():?string { return $this->section??null; } protected function setMaxLength(int $maxLength):void { $this->maxLength = $maxLength; } public function getConfig():array{ $config = get_object_vars($this); return array_map(function ($item) { if (is_a($item, Field::class)) { return $item->getConfig(); } else if (is_array($item)) { $temp = []; foreach ($item as $k => $v) { if (is_a($v, Field::class)) { $temp[$k] = $v->getConfig(); } else { $temp[$k] = $v; } } return $temp; } else { return $item; } }, $config); } }