/** * Creates a new CMS event object for a given event name and subject. The following arguments must be given: * subject object The subject of the event. This is the core object you are going to manipulate. * eventClass string The Event class name. If you do not provide it Joomla\Cms\Events\<eventNameWithoutOnPrefix> * will be used. * * @param string $eventName The name of the event, e.g. onTableBeforeLoad * @param array $arguments Additional arguments to pass to the event * * @return static * * @since __DEPLOY_VERSION__ * @throws BadMethodCallException If you do not provide a subject argument */ public static function create($eventName, $arguments = []) { // Get the class name from the arguments, if specified $eventClassName = ''; if (isset($arguments['eventClass'])) { $eventClassName = $arguments['eventClass']; unset($arguments['eventClass']); } /** * If the class name isn't set/found determine it from the event name, e.g. TableBeforeLoadEvent from * the onTableBeforeLoad event name. */ if (empty($eventClassName) || !class_exists($eventClassName, true)) { $bareName = strpos($eventName, 'on') === 0 ? substr($eventName, 2) : $eventName; $parts = Normalise::fromCamelCase($bareName, true); $eventClassName = __NAMESPACE__ . '\\' . ucfirst(array_shift($parts)) . '\\'; $eventClassName .= implode('', $parts); $eventClassName .= 'Event'; } // Make sure a non-empty subject argument exists and that it is an object if (!isset($arguments['subject']) || empty($arguments['subject']) || !is_object($arguments['subject'])) { throw new BadMethodCallException("No subject given for the {$eventName} event"); } // Create and return the event object if (class_exists($eventClassName, true)) { return new $eventClassName($eventName, $arguments); } return new GenericEvent($eventName, $arguments); }
/** * Get the field for the primary key. * * @return string The name of the field */ public function key() { if (empty($this->key) && !empty($this->definition->primary)) { $keys = array_values(array_filter(preg_split('~[\\s,]+~', $this->definition->primary))); $keys = array_map(function ($key) { return Normalise::toVariable($key); }, $keys); $this->key = count($keys) > 1 ? $keys : array_shift($keys); } if (empty($this->key) && $this->has('id')) { $this->key = 'id'; } return $this->key; }
/** * Method to instantiate the form field object. * * @param Form $form The form to attach to the form field object. * * @since 1.0 */ public function __construct(Form $form = null) { // If there is a form passed into the constructor set the form and form control properties. if ($form instanceof Form) { $this->form = $form; $this->formControl = $form->getFormControl(); } // Detect the field type if not set if (!isset($this->type)) { $parts = Normalise::fromCamelCase(get_called_class(), true); if ($parts[0] == 'J') { $this->type = String::ucfirst($parts[count($parts) - 1], '_'); } else { $this->type = String::ucfirst($parts[0], '_') . String::ucfirst($parts[count($parts) - 1], '_'); } } }
/** * Load view class * * @param string $class Class name */ private function view($class) { if (!strstr(strtolower($class), 'view')) { return; } $scope = Factory::getApplication()->scope; // Load component specific files if ($this->appName($class) === $scope) { $parts = Normalise::fromCamelCase($class, true); $type = array_pop($parts); $path = JPATH_SITE . '/libraries/fabble/Views/' . JString::ucfirst($type) . '.php'; $original = $type; if (file_exists($path)) { require_once $path; class_alias('\\Fabble\\Views\\' . $original, $class); return; } } }
/** * Gets the identifier for an entity * * @param object $entity The entity * * @return array The identifier as key-value pair(s) */ protected function getIdentifier($entity) { $entityId = json_decode($this->entityRegistry->getEntityId($entity), true); if (is_array($entityId)) { $identifier = []; foreach ($entityId as $key => $value) { $key = strtolower(Normalise::toUnderscoreSeparated(Normalise::fromCamelCase($key))); $identifier[$key] = $value; } return $identifier; } $primary = $this->builder->getMeta(get_class($entity))->primary; return [$primary => $entityId]; }
/** * Adds an entity to the repo * * @param object $entity The entity to add * * @return void * * @throws OrmException if the entity could not be added */ public function add($entity) { foreach ($this->restrictions as $preset) { if ($preset['op'] == Operator::EQUAL) { $property = Normalise::toVariable($preset['field']); $entity->{$property} = $preset['value']; } } $this->unitOfWork->scheduleForInsertion($entity); }
/** * Gets the property name for an identifier * * @param string $identifier The identifier * * @return string */ public function propertyName($identifier) { return Normalise::toVariable($identifier); }
/** * Method to test Normalise::toKey(). * * @param string $expected The expected value from the method. * @param string $input The input value for the method. * * @return void * * @covers Joomla\String\Normalise::toKey * @dataProvider seedTestToKey * @since 1.0 */ public function testToKey($expected, $input) { $this->assertEquals($expected, Normalise::toKey($input)); }
/** * @param string $word A word * * @return string */ private function normalise($word) { return Normalise::toUnderscoreSeparated(strtolower(Normalise::fromCamelCase($word))); }