/** * Returns the class name in the class hierarchy which contains a given * field column for a {@link DataObject}. If the field does not exist, this * will return null. * * @param string $candidateClass * @param string $fieldName * @return string */ public function classForField($candidateClass, $fieldName) { // normalise class name $candidateClass = ClassInfo::class_name($candidateClass); if ($candidateClass === DataObject::class) { return null; } // Short circuit for fixed fields $fixed = DataObject::config()->get('fixed_fields'); if (isset($fixed[$fieldName])) { return $this->baseDataClass($candidateClass); } // Find regular field while ($candidateClass) { $fields = $this->databaseFields($candidateClass, false); if (isset($fields[$fieldName])) { return $candidateClass; } $candidateClass = get_parent_class($candidateClass); } return null; }
/** * Safely get a DataObject from a client-supplied ID and ClassName, checking: argument * validity; existence; and canView permissions. * * @param int $id The ID of the DataObject * @param string $class The Class of the DataObject * @return DataObject The referenced DataObject * @throws HTTPResponse_Exception */ protected function getObject($id, $class) { $id = (int) $id; $class = ClassInfo::class_name($class); if (!$class || !is_subclass_of($class, 'SilverStripe\\ORM\\DataObject') || !Object::has_extension($class, 'SilverStripe\\ORM\\Versioning\\Versioned')) { $this->controller->httpError(400, _t('AddToCampaign.ErrorGeneral', 'We apologise, but there was an error')); return null; } $object = DataObject::get($class)->byID($id); if (!$object) { $this->controller->httpError(404, _t('AddToCampaign.ErrorNotFound', 'That {Type} couldn\'t be found', '', ['Type' => $class])); return null; } if (!$object->canView()) { $this->controller->httpError(403, _t('AddToCampaign.ErrorItemPermissionDenied', 'It seems you don\'t have the necessary permissions to add {ObjectTitle} to a campaign', '', ['ObjectTitle' => $object->Title])); return null; } return $object; }
public function testNonClassName() { $this->setExpectedException('ReflectionException', 'Class IAmAClassThatDoesNotExist does not exist'); $this->assertEquals('IAmAClassThatDoesNotExist', ClassInfo::class_name('IAmAClassThatDoesNotExist')); }