Merge attributes

@param Definition $definition
420254
protected function mergeAttributes(self $definition)
    {
        foreach ($definition->getAttributes() as $name => $attribute)
        {
            if (isset($this->attributes[$name])) {
                $this->attributes[$name]->merge($attribute);
                continue;
            }

            $this->attributes[$name] = $attribute;
        }
    }
138990
public function load(\Asgard\Entity\Definition $definition) {
		$this->entityClass = $definition->getClass();

		if(!$definition->has('order_by'))
			$definition->set('order_by', 'id DESC');

		$definition->hook('get', [$this, 'hookGet']);
		$definition->hook('getTranslations', [$this, 'hookgetTranslations']);
		$definition->hook('validation', [$this, 'hookValidation']);
	}
845115
public function addDefinition(Definition $definition)
    {
        $attribute = $definition->getAttribute();
        $key = $definition->getKey();

        $this->mongoIndex[$key] = $attribute;

        $this->definitions[$attribute] = $definition;
    }
111247
private function completeDefinition($id, Definition $definition)
    {
        if (!$reflectionClass = $this->getReflectionClass($id, $definition)) {
            return;
        }

        if ($this->container->isTrackingResources()) {
            $this->container->addResource(static::createResourceForClass($reflectionClass));
        }

        if (!$constructor = $reflectionClass->getConstructor()) {
            return;
        }

        $arguments = $definition->getArguments();
        foreach ($constructor->getParameters() as $index => $parameter) {
            if (array_key_exists($index, $arguments) && '' !== $arguments[$index]) {
                continue;
            }

            try {
                if (!$typeHint = $parameter->getClass()) {
                    // no default value? Then fail
                    if (!$parameter->isOptional()) {
                        throw new RuntimeException(sprintf('Unable to autowire argument index %d ($%s) for the service "%s". If this is an object, give it a type-hint. Otherwise, specify this argument\'s value explicitly.', $index, $parameter->name, $id));
                    }

                    if (!array_key_exists($index, $arguments)) {
                        // specifically pass the default value
                        $arguments[$index] = $parameter->getDefaultValue();
                    }

                    continue;
                }

                if (null === $this->types) {
                    $this->populateAvailableTypes();
                }

                if (isset($this->types[$typeHint->name])) {
                    $value = new Reference($this->types[$typeHint->name]);
                } else {
                    try {
                        $value = $this->createAutowiredDefinition($typeHint, $id);
                    } catch (RuntimeException $e) {
                        if ($parameter->allowsNull()) {
                            $value = null;
                        } elseif ($parameter->isDefaultValueAvailable()) {
                            $value = $parameter->getDefaultValue();
                        } else {
                            throw $e;
                        }
                    }
                }
            } catch (\ReflectionException $e) {
                // Typehint against a non-existing class

                if (!$parameter->isDefaultValueAvailable()) {
                    throw new RuntimeException(sprintf('Cannot autowire argument %s for %s because the type-hinted class does not exist (%s).', $index + 1, $definition->getClass(), $e->getMessage()), 0, $e);
                }

                $value = $parameter->getDefaultValue();
            }

            $arguments[$index] = $value;
        }

        // it's possible index 1 was set, then index 0, then 2, etc
        // make sure that we re-order so they're injected as expected
        ksort($arguments);
        $definition->setArguments($arguments);
    }
1317548
public function mergeAttribute(NodeAttribute $attribute)
    {
        if (! $this->has($attribute->getName())) {
            $this->attributes[$attribute->getName()] = $attribute;
            $this->tracker->logAddition($attribute->getName());
            return;
        }
        $backup = $attribute;
        $attribute = $this->get($attribute->getName());
        $attribute->add($backup->getValues());
        $this->attributes[$attribute->getName()] = $attribute;
    }
249752
public function bind(Definition $definition) {
        $this->arguments = [];
        $this->options = [];
        $this->definition = $definition;

        $this->parse();
    }
755736
private function replaceAttributesWithChange()
    {
        if (($change = static::lookupRequestedChange())
            && $change->key == $this->getKey()) {
            $this->attributes = array_merge(
                $this->attributes,
                $this->attributesAtChange($change)
            );
        }
    }
791336
public function bind(InputDefinition $definition)
    {
        $hasCodeArgument = false;

        if ($definition->getArgumentCount() > 0) {
            $args    = $definition->getArguments();
            $lastArg = \array_pop($args);
            foreach ($args as $arg) {
                if ($arg instanceof CodeArgument) {
                    $msg = \sprintf('Unexpected CodeArgument before the final position: %s', $arg->getName());
                    throw new \InvalidArgumentException($msg);
                }
            }

            if ($lastArg instanceof CodeArgument) {
                $hasCodeArgument = true;
            }
        }

        $this->hasCodeArgument = $hasCodeArgument;

        return parent::bind($definition);
    }
978368
public function mergeAttributes(SimpleXmlElement $node, $replace = true)
    {
        foreach ($node->attributes() as $name => $value) {
            if (isset($this[$name])) {
                if ($replace) {
                    $this[$name] = (string)$value;
                }

                continue;
            }

            $this->addAttribute($name, (string)$value);
        }

        return $this;
    }
911187
protected function parseDefinition($definition = null)
    {
        if (is_null($definition)) {
            if (!is_null($this->definition)) {
                throw new \InvalidArgumentException(
                    'Cannot parse rule definitions for rule "'. $this->name. '", attrib "'
                    . $this->attrib->getName(). '" without definition!'
                );
            }
            $definition = $this->definition;
            $this->definition = null;
        }

        // required, simple
        if (is_string($definition) || is_callable($definition)) {
            $definition = array('constraint' => $definition);
        }

        // init empty to reduce isset checks..
        $definition = array_merge(self::$DEFAULT_ATTRIBS, $definition);

        // set attribs
        $this->sufficient = $definition['sufficient'];
        $this->skipEmpty  = $definition['skipEmpty'];
        $this->error      = $definition['error'];

        // having old style callable constraint
        if (is_callable($definition['constraint']) && is_array($definition['constraint'])) { // !($definition['constraint'] instanceof \Closure)) {
            $cb = $definition['constraint'];
            $definition['constraint'] = function () use ($cb) {
                $args = func_get_args();
                return call_user_func_array($cb, $args);
            };
        }

        // from string -> check predefined
        elseif (is_string($definition['constraint'])) {
            $args = preg_split('/:/', $definition['constraint']);
            $method = 'rule'. array_shift($args);
            $found = false;
            foreach ($this->dataFilter->getPredefinedRuleClasses() as $className) {
                if (is_callable(array($className, $method)) && method_exists($className, $method)) {
                    $definition['constraint'] = call_user_func_array(array($className, $method), $args);
                    $found = true;
                    break;
                }
            }
            if (!$found) {
                throw new \InvalidArgumentException(
                    'Could not use constraint "'. $definition['constraint']. '" for rule "'
                    . $this->name. '", attrib "'. $this->attrib->getName(). '" because no '
                    . 'predefined rule class found implementing "'. $method. '()"'
                );
            }
        }

        // determine class
        $constraintClass = is_object($definition['constraint'])
            ? get_class($definition['constraint'])
            : '(Scalar)';

        // at this point: it has to be a closure!
        if ($constraintClass !== 'Closure') {
            throw new \InvalidArgumentException(
                'Definition for rule "'. $this->name. '", attrib "'. $this->attrib->getName(). '"'
                . ' has an invalid constraint of class '. $constraintClass
            );
        }
        $this->constraint = $definition['constraint'];
    }
1048777
protected function _mergeAttributes($attrs, $merge)
    {
        $appendable = array(
        'class' => array('separator' => ' ', 'match' => 'full'),
        'style' => array('separator' => ';', 'match' => 'part'),
        );

        foreach ((array)$merge as $attr => $values) {
            if (
            !array_key_exists($attr, $attrs)
            || empty($attrs[$attr]) && false !== $attrs[$attr]
            ) {
                $attrs[$attr] = $values;
                continue;
            } elseif (!in_array($attr, array_keys($appendable))) {
                continue;
            }

            if (!is_array($attrs[$attr])) {
                $attrs[$attr] = explode($appendable[$attr]['separator'], $attrs[$attr]);
                $implode = true;
            }

            if (!is_array($values)) {
                $values = explode($appendable[$attr]['separator'], $attrs[$attr]);
            }

            switch ($appendable[$attr]['match']) {

                case 'full':
                    foreach ($values as $value) {
                        if (!in_array($value, $attrs[$attr])) {
                            $attrs[$attr][] = $value;
                        }
                    }
                    break;

                case 'part':
                    foreach ($attrs[$attr] as $k => $haystack) {
                        if (empty($haystack)) {
                            unset($attrs[$attr][$k]);
                            continue;
                        }

                        if (false === strpos($haystack, $appendable[$attr]['separator'])) {
                            $attrs[$attr][$k] = $haystack . $appendable[$attr]['separator'];
                        }

                        foreach ($values as $n => $value) {
                            $needle = current(explode(':', $value)) . ':';
                            if (0 === strpos($haystack, $needle)
                                || false !== strpos($haystack, ';' . $needle)
                                || false !== strpos($haystack, '; ' . $needle)
                            ) {
                                unset($values[$n]);
                            }
                        }
                    }

                    foreach (array_keys($values) as $key) {
                        if (false === strpos($values[$key], $appendable[$attr]['separator'])) {
                            $values[$key] = $values[$key] . $appendable[$attr]['separator'];
                        }
                        $attrs[$attr][] = $values[$key];
                    }
                    break;

                default:
            }

            if (isset($implode)) {
                $attrs[$attr] = implode(' ', $attrs[$attr]);
            }
        }

        return $attrs;
    }
754158
public function merge(array $attributes)
    {
        foreach ($attributes as $name => $value) {
            $this->add($name, $value);
        }
    }
1093803
public function createDictionary(DictionaryDefinition $definition): DictionaryInterface
    {
        $builder = $this->getDictionaryBuilder($definition);

        /** @var DictionaryBuilderInterface $builder */
        return $builder->build($this, $definition);
    }
213856
protected function mergeAttribute(string $key, $mergeValue)
    {
        $current = $this[$key];

        if ($current instanceof DataObjectInterface && method_exists($current, 'merge')) {

            $class = get_class($current);

            if (is_array($mergeValue)) {
                $mergeValue = new $class($mergeValue);
            }

            // If we have nothing to merge with, don't bother
            if (null === $mergeValue) {
                return;
            }

            $mergeValue = $current->merge($mergeValue);
        }

        if (null === $mergeValue) {
            return;
        }

        $this[$key] = $mergeValue;
    }
79673
public function add(DefinitionInterface $definition)
    {
        $index = count($this->_names);
        $this->_definitionSource[$index] = $definition;
        $this->_names[$definition->getName()] = $index;
        return $this;
    }
328667
private static function define($domainId, $definition) {
		$definition = ConfigHelper::normalizeItemConfig($domainId, $definition);
		if(!\App::$domain->has($domainId)) {
			\App::$domain->set($domainId, $definition);
		}
	}
1342167
public function setDefinition(array $definition) {
        $arguments = [];
        $options = [];
        foreach ($definition as $item) {
            if ($item instanceof Option) {
                $options[] = $item;
            }
            else {
                $arguments[] = $item;
            }
        }

        $this->setArguments($arguments);
        $this->setOptions($options);
    }