Apply nested definitions

@param array $definitions array of FabricateDefinition
@param array $record data
@param FabricateContext $world context
@return array record applied nested definitions
17097
private function applyNestedDefinitions($definitions, $record, $world)
    {
        foreach ($definitions as $definition) {
            $result = $definition->run($record, $world);
            $record = $this->applyTraits($record, $world);
            $record = array_merge($record, $result);
        }
        return $record;
    }
817019
protected function processDefinitions(array $definitions)
    {
        foreach ($definitions['subdivisions'] as $code => &$definition) {
            // Add common keys from the root level.
            $definition['country_code'] = $definitions['country_code'];
            if (isset($definitions['locale'])) {
                $definition['locale'] = $definitions['locale'];
            }
            // Ensure the presence of code and name.
            $definition['code'] = $code;
            if (!isset($definition['name'])) {
                $definition['name'] = $code;
            }
            if (isset($definition['local_code']) && !isset($definition['local_name'])) {
                $definition['local_name'] = $definition['local_code'];
            }
        }

        return $definitions;
    }
721138
public function addDefinitions(array $definitions)
    {
        foreach ($definitions as $name => $definition) {
            if (!($definition instanceof DefinitionInterface)) {
                throw new DcGeneralInvalidArgumentException(
                    'Definition ' . $name . ' does not implement DefinitionInterface.'
                );
            }

            $this->setDefinition($name, $definition);
        }

        return $this;
    }
792488
public function import(array $definitions, array $records, array $defaultAttributes = [], $persist = true): array
    {
        $imported = [];
        $recordsByHandle = $this->getRecordsByHandle($records);
        foreach ($definitions as $handle => $definition) {
            $modelClass = $definition['class'];
            $converter = Craft::$app->controller->module->getConverter($modelClass);
            if ($converter) {
                $record = $this->findOrNewRecord($recordsByHandle, $definition, $handle);

                if ($converter->getRecordDefinition($record) === $definition) {
                    Schematic::info('- Skipping '.get_class($record).' '.$handle);
                } else {
                    $converter->setRecordAttributes($record, $definition, $defaultAttributes);
                    if ($persist) {
                        Schematic::info('- Saving '.get_class($record).' '.$handle);
                        if (!$converter->saveRecord($record, $definition)) {
                            Schematic::importError($record, $handle);
                        }
                    }
                }

                $imported[] = $record;
            }
            unset($recordsByHandle[$handle]);
        }

        if (Schematic::$force && $persist) {
            // Delete records not in definitions
            foreach ($recordsByHandle as $handle => $record) {
                $modelClass = get_class($record);
                Schematic::info('- Deleting '.get_class($record).' '.$handle);
                $converter = Craft::$app->controller->module->getConverter($modelClass);
                $converter->deleteRecord($record);
            }
        }

        return $imported;
    }
438779
public function get_document($record, $options = array()) {
        global $CFG;
        require_once($CFG->dirroot . '/course/lib.php');

        // Get the context, modinfo, and section.
        try {
            $context = \context_course::instance($record->course);
        } catch (\moodle_exception $ex) {
            // Notify it as we run here as admin, we should see everything.
            debugging('Error retrieving ' . $this->areaid . ' ' . $record->id .
                    ' document, not all required data is available: ' . $ex->getMessage(),
                    DEBUG_DEVELOPER);
            return false;
        }

        // Title - use default if none given.
        $title = get_section_name($record->course, $record->section);

        // Prepare associative array with data from DB.
        $doc = \core_search\document_factory::instance($record->id, $this->componentname, $this->areaname);
        $doc->set('title', content_to_text($title, false));
        $doc->set('content', content_to_text($record->summary, $record->summaryformat));
        $doc->set('contextid', $context->id);
        $doc->set('courseid', $record->course);
        $doc->set('owneruserid', \core_search\manager::NO_OWNER_ID);
        $doc->set('modified', $record->timemodified);

        return $doc;
    }
1272041
public static function set_context_instance($record) {
        if ($instance = context_instance::get_record_by_contextid($record->contextid, false)) {
            // Update.
            $instance->from_record($record);

            if (empty($record->purposeid) && empty($record->categoryid)) {
                // We accept one of them to be null but we delete it if both are null.
                self::unset_context_instance($instance);
                return;
            }

        } else {
            // Add.
            $instance = new context_instance(0, $record);
        }
        $instance->save();

        return $instance;
    }
673035
public static function set_contextlevel($record) {
        global $DB;

        if ($record->contextlevel != CONTEXT_SYSTEM && $record->contextlevel != CONTEXT_USER) {
            throw new \coding_exception('Only context system and context user can set a contextlevel ' .
                'purpose and retention');
        }

        if ($contextlevel = contextlevel::get_record_by_contextlevel($record->contextlevel, false)) {
            // Update.
            $contextlevel->from_record($record);
        } else {
            // Add.
            $contextlevel = new contextlevel(0, $record);
        }
        $contextlevel->save();

        // We sync with their defaults as we removed these options from the defaults page.
        $classname = \context_helper::get_class_for_level($record->contextlevel);
        list($purposevar, $categoryvar) = data_registry::var_names_from_context($classname);
        set_config($purposevar, $record->purposeid, 'tool_dataprivacy');
        set_config($categoryvar, $record->categoryid, 'tool_dataprivacy');

        return $contextlevel;
    }
337239
protected function write(array $record): void
    {
        $this->hub->withScope(
            function (Scope $scope) use ($record) {
                if (!empty($record['context']['extra'])) {
                    foreach ($record['context']['extra'] as $key => $tag) {
                        $scope->setExtra($key, $tag);
                    }
                    unset($record['context']['extra']);
                }

                if (!empty($record['context']['tags'])) {
                    foreach ($record['context']['tags'] as $key => $tag) {
                        $scope->setTag($key, $tag);
                    }
                    unset($record['context']['tags']);
                }

                if (!empty($record['extra'])) {
                    foreach ($record['extra'] as $key => $extra) {
                        $scope->setExtra($key, $extra);
                    }
                }

                if (!empty($record['context']['fingerprint'])) {
                    $scope->setFingerprint($record['context']['fingerprint']);
                    unset($record['context']['fingerprint']);
                }

                if (!empty($record['context']['user'])) {
                    $scope->setUser($record['context']['user']);
                    unset($record['context']['user']);
                }

                $scope->addEventProcessor(
                    function (Event $event) use ($record) {
                        if (!empty($record['context']['logger'])) {
                            $event->setLogger($record['context']['logger']);
                            unset($record['context']['logger']);
                        } else {
                            $event->setLogger($record['channel']);
                        }

                        if (!empty($this->environment) && !$event->getEnvironment()) {
                            $event->setEnvironment($this->environment);
                        }

                        if (!empty($this->release) && !$event->getRelease()) {
                            $event->setRelease($this->release);
                        }

                        if (isset($record['context']['exception']) && $record['context']['exception'] instanceof \Throwable) {
                            $event->setMessage($record['formatted']);
                        }

                        return $event;
                    }
                );

                if (isset($record['context']['exception']) && $record['context']['exception'] instanceof \Throwable) {
                    $this->hub->captureException($record['context']['exception']);
                } else {
                    $this->hub->captureMessage($record['formatted'], $this->getLogLevel($record['level']));
                }
            }
        );
    }
576552
public function build(array $definitions)
    {
        $criteria = [];

        foreach ($definitions as $definition) {
            $criterion = $this->dispatchBuild($definition);

            if ($criterion instanceof Criterion) {
                $criteria[] = $criterion;
            }
        }

        return $criteria;
    }
572455
protected function prepare( Record_Interface $record ): array {

		$data = [
			'description' => $record->get_description(),
			'category'    => $record->get_category(),
			'offset'      => round( ( $record->get_origin() - $this->origin ) / $this->total * 100, 2 ),
			'width'       => round( $record->get_duration() / $this->total * 100, 2 ),
		];

		return $data;
	}
449892
private function hydrateDefinitionMethodCalls(Definition $definition, array &$definitionArray)
    {
        foreach ($definition->getMethodCalls() as $methodToCall) {
            $method_call_array = [];

            // retrieving method to call name
            $method_name = array_shift($methodToCall);
            $method_call_array[] = $method_name;

            // retrieving method to call arguments
            $methodArgs = [];
            foreach (array_shift($methodToCall) as $arg) {
                $methodArgs[] = $this->convertArgument($arg);
            }

            $method_call_array[] = $methodArgs;

            // finally add method call to definition array
            $definitionArray['calls'][] = $method_call_array;
        }
    }
1372299
private function transformRecords(RecordCollection $records, array $transformers, $context)
    {
        foreach ($transformers as $transformer) {
            if ($transformer instanceof PorterAware) {
                $transformer->setPorter($this);
            }

            $records = $transformer->transform($records, $context);
        }

        return $records;
    }
400232
protected function processPrefix($name, $definition, array &$prefices, array &$vocabularies)
    {
        $prefices[$name] = $definition;

        // Register the vocabulary
        if (!isset($vocabularies[$definition])) {
            $vocabularies[$definition] = [];
        }
    }
235037
public function setDefinitions($definitions)
    {
        if (\is_string($definitions)) {
            $definitions = [$definitions];
        }

        if ($definitions instanceof \Traversable) {
            $definitions = \iterator_to_array($definitions);
        }

        if (!\is_array($definitions)) {
            throw new \InvalidArgumentException(
                \sprintf('Definitions must be a string or traversable. %s given', \gettype($definitions))
            );
        }

        \array_walk(
            $definitions,
            function ($definition) {
                if (!\is_array($definition) && !\is_string($definition)) {
                    throw new \InvalidArgumentException(
                        \sprintf(
                            'A definition must be an array or a file or directory path. %s given',
                            \gettype($definition)
                        )
                    );
                }
            }
        );

        $this->definitions = $definitions;

        return $this;
    }
267460
private function translateRecordIntoEventMessage(array $record)
    {
        $identifier = new MessageIdentifier($record['event_id']);

        $payload = $this->eventSerializer->deserializePayload(
            $record['event_payload'],
            Contract::createFromClassName($record['event_payload_type'])
        );

        $metadata = $this->eventSerializer->deserializeMetadata(
            $record['event_metadata'],
            Contract::createFromClassName($record['event_metadata_type'])
        );

        $sequenceNumber = (int) $record['aggregate_version'];

        $eventMessage = new GenericEvent($this->aggregateRootIdentifier, $sequenceNumber, $identifier, $payload, $metadata);

        return $eventMessage;
    }
596747
private static function parseDefinitions(array $definitions): array
    {
        if (\count($definitions) === 0) {
            return $definitions;
        }

        return \array_map(
            function ($definition) {
                if (\is_array($definition)) {
                    return $definition;
                }

                return self::loadDefinitionsFromPath($definition);
            },
            $definitions
        );
    }
748724
protected function columnMapRecord($record)
    {
        $adjustedmap = $this->columnMap;
        $newrecord = array();
        foreach ($record as $field => $value) {
            if (isset($adjustedmap[$field])) {
                $newrecord[$adjustedmap[$field]] = $value;
            } else {
                $newrecord[$field] = $value;
            }
        }

        return $newrecord;
    }