Parse provided value into new data

@param $value
@param $providers
@param array $references
@param array $additional

@return mixed

@throws \Exception
429149
public function parse($value, $providers, $references = [], $additional = [])
    {
        preg_match_all(self::REGEX, $value, $matches);

        foreach ($matches[0] as $pattern) {
            preg_match_all('/[^,\(\)<>]+/', $pattern, $arguments);
            $arguments = $arguments[0];
            $method = array_shift($arguments);
            $arguments = array_map(function ($arg) {
                return trim(trim($arg), '\'""');
            }, $arguments);

            foreach ($providers as $provider) {
                /*
                 * Call method from provider with/without arguments
                 * 1: Arguments are passed through from fixture
                 * 2: Search if method needs arguments en find them through typehint and additional params
                 * 3: Magic methods without arguments
                 */
                if (method_exists($provider, $method)) {
                    $refl = new \ReflectionMethod($provider, $method);

                    if (count($arguments) < $refl->getNumberOfRequiredParameters()) {
                        $parameters = $refl->getParameters();
                        $parametersNeeded = array_slice($parameters, count($arguments));
                        $arguments = array_merge($arguments, $this->findArguments($parametersNeeded, $additional));

                        if (count($parameters) !== count($arguments)) {
                            throw new \Exception('Can not match all arguments for provider ' . get_class($provider));
                        }
                    }

                    $value = $this->processValue($pattern, $refl->invokeArgs($provider, $arguments), $value, $matches[0]);

                    break;
                } elseif (is_callable([$provider, $method])) {
                    $value = $this->processValue($pattern, call_user_func_array(array($provider, $method), $arguments), $value, $matches[0]);

                    break;
                }
            }
        }

        return $value;
    }
55961
public function performStoreAfter(Model $model, $source, $value)
    {
        $relation = $this->resolveModelSource($model, $source);

        if ( ! ($relation instanceof Relation)) {
            throw new UnexpectedValueException("{$source} did not resolve to relation");
        }

        // Any singular relations are unexpected
        if (    $relation instanceof BelongsTo
            ||  $relation instanceof HasOne
            ||  $relation instanceof MorphOne
        ) {
            throw new UnexpectedValueException("{$source} is a single relation, expecting plural");
        }

        if (null === $value) {
            $value = [];
        }

        // Should not be used, since this is for singular relations..
        if ($relation instanceof BelongsToMany) {
            $relation->sync($value);
            return;
        }

        // For *One and *Many relations, we need to handle detachment for currently related models.
        if (    $relation instanceof HasOne
            ||  $relation instanceof HasMany
            ||  $relation instanceof MorphOne
            ||  $relation instanceof MorphMany
        ) {
            // Detach only the models that shouldn't stay attached (ie. aren't in the new value array)

            /** @var Collection|Model[] $currentlyRelated */
            $currentlyRelated = $relation->get();
            $currentlyRelated = $currentlyRelated->filter(function (Model $model) use ($value) {
                return ! in_array($model->getKey(), $value);
            });

            $this->detachRelatedModelsForOneOrMany($currentlyRelated, $relation);
        }

        // For HasMany, the related models must be found,
        // and then they should be saved on the relation.
        $relatedModels = $this->getModelsByKey($relation->getRelated(), $value);

        if ( ! count($relatedModels)) return;

        if (    $relation instanceof HasOne
            ||  $relation instanceof HasMany
            ||  $relation instanceof MorphOne
            ||  $relation instanceof MorphMany
        ) {
            foreach ($relatedModels as $relatedModel) {
                $relation->save($relatedModel);
            }
            return;
        }

        throw new UnexpectedValueException(
            'Unexpected relation class ' . get_class($relation) . " for {$source}"
        );
    }
1161553
public function setResult($result = self::RESULT_SUCCESS,
                              $message = null,
                              array $data = [],
                              array $additional = [])
    {
        $this->headers->set('X-Phlexible-Response', 'result');

        $values = [
            'success' => $result,
            'msg' => $message,
            'data' => $data,
        ];

        if (is_array($additional) && count($additional)) {
            foreach ($additional as $key => $value) {
                $values += $additional;
            }
        }

        $this->setData($values);
    }
540973
protected function parseScope($value)
    {
        switch ($value) {
            case 'singleton':
                return Configuration::SCOPE_SINGLETON;
            case 'prototype':
                return Configuration::SCOPE_PROTOTYPE;
            case 'session':
                return Configuration::SCOPE_SESSION;
            default:
                throw new InvalidObjectConfigurationException('Invalid scope "' . $value . '"', 1167574991);
        }
    }
482139
public function resolveReference($reference, $values)
    {
        foreach (explode('.', $reference) as $leaf) {
            if ((!is_array($values) && !($values instanceof ArrayAccess))
                || (is_array($values) && !array_key_exists($leaf, $values))
                || (($values instanceof ArrayAccess) && !$values->offsetExists($leaf))) {
                throw InvalidReferenceException::format(
                    'The reference "%s" could not be resolved (failed at "%s").',
                    "%$reference%",
                    $leaf
                );
            }

            $values = $values[$leaf];
        }

        return $values;
    }
493498
public function validate_provider( $value, $options ) {

		// Check if we need to validate.
		if ( 0 === (int) pods_v( static::$type . '_restrict_providers', $options ) ) {
			return true;
		}

		$providers = $this->get_providers();

		// Filter existing providers.
		foreach ( $providers as $key => $provider ) {
			$fieldname = static::$type . '_enabled_providers_' . tag_escape( $provider['host'] );

			/**
			 * @todo Future compat to enable serialised strings as field options
			 */

			/**
			 * Current solution: all separate field options.
			 */
			if ( empty( $options[ $fieldname ] ) ) {
				unset( $providers[ $key ] );
			}
		}

		// Value validation.
		$provider_match = $this->get_provider( $value );

		foreach ( $providers as $match => $provider ) {
			if ( $provider_match === $match ) {
				return true;
			}
		}

		return false;
	}
1002341
protected function parsevalue($value)
    {
        //make sure the whole value can be mapped
        if (!is_array($value) && !$value instanceof \ArrayAccess && !is_object($value)) {
            return false;
        }

        return $this->rParseValue($value);
    }
1282987
public function addAdditionalInfos(\AgentSIB\Diadoc\Api\Proto\Invoicing\AdditionalInfo $value)
    {
        if ($this->AdditionalInfos === null) {
            $this->AdditionalInfos = new \Protobuf\MessageCollection();
        }

        $this->AdditionalInfos->add($value);
    }
547823
public function store(Model $model, $source, $value)
    {
        if ( ! $this->isTranslated()) {
            $this->performStore($model, $source, $value);
            return;
        }

        /** @var Model|Translatable $model */

        if ( ! is_array($value)) {
            throw new UnexpectedValueException("Value should be in per-locale array format for translatable data");
        }

        foreach ($value as $locale => $singleValue) {

            // Safeguard: use empty string if non-nullable translated field is null
            if (null === $singleValue && ! $this->isNullable()) {
                $singleValue = '';
            }

            $this->performStore($model->translateOrNew($locale), $source, $singleValue);
        }
    }
1332862
public function specifyAdditionalParameters(array $data, array $additionalParams): array
    {
        foreach ($additionalParams as $param) {
            $method = 'get' . ucfirst($param);
            if (method_exists($this, $method)) {
                $value = $this->{$method}();
                if ($value) {
                    $data[$param] = $value;
                }
            }
        }
        return $data;
    }
671385
protected function fetch($value)
    {
        if (empty($value)) {
            if (true === $this->null) {
                return;
            }
            throw new Exception('Value in fetch method of PrimaryKeyField cannot be empty');
        }

        return $this->fetchModel($value);
    }
134462
public function importProvider(ResourceProviderInterface $provider, array $values = [])
    {
        $provider->populate($this);
        $this->importValues($values);
    }
1308385
public static function getInitializedConditionals($value)
    {
        $services = array();

        if (is_array($value)) {
            foreach ($value as $v) {
                $services = array_unique(array_merge($services, self::getInitializedConditionals($v)));
            }
        } elseif ($value instanceof Reference && ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $value->getInvalidBehavior()) {
            $services[] = (string) $value;
        }

        return $services;
    }
565633
public function createReference($value)
    {
        $ref = $this->generateNextReference();
        $this->references[$ref] = $value;

        return $ref;
    }
394329
public function resolve($value, array $args, $context, ResolveInfo $info)
    {
        $result = call_user_func_array(
            $this->connectionResolver,
            func_get_args()
        );

        if (!$result instanceof SS_List) {
            throw new \Exception('Connection::resolve() must resolve to a SS_List instance.');
        }

        return $this->resolveList($result, $args, $context, $info);
    }
501787
public function writeAdditionalConfiguration(array $additionalConfigurationLines)
    {
        return GeneralUtility::writeFile(
            Environment::getPublicPath() . '/' . $this->additionalConfigurationFile,
            '<?php' . LF .
                implode(LF, $additionalConfigurationLines) . LF
        );
    }
416381
private function renderReference($value, $reference, &$isRendered)
    {
        if ((true === $isRendered) || !isset($reference[$value])) {
            return $value;
        }

        $isRendered = true;

        if (\is_array($reference[$value])) {
            return $reference[$value][0];
        }

        return $reference[$value];
    }