Creates a new value object.

If the value is invalid, an invalid-argument-exception is thrown.
@see self::validate()

@param scalar $value
@throws InvalidArgumentException
518758
public static function factory($value)
    {
        
        if ($value instanceof self) {
            $value = $value->getValue();
        }
        
        $value = static::filter($value);
        
        if (!is_scalar($value)) {
            throw new ErrorException("A value object must be created from a scalar value!");
        }
        
        $classname = get_called_class();
        
        if (!isset(self::$instances[$classname])) {
            self::$instances[$classname] = array();
        }
    
        // lazy load the value-object
        if (!isset(self::$instances[$classname][$value])) {
            if (func_num_args()>1) {
                // construct value-object with more then the pure scalar value
                // (not really correct in terms of value-objects, but sometimes needed)
                $arguments = func_get_args();
                $reflection = new \ReflectionClass($classname);
                self::$instances[$classname][$value] = $reflection->newInstanceArgs($arguments);
                
            } else {
                self::$instances[$classname][$value] = new static($value);
            }
        }
    
        return self::$instances[$classname][$value];
    }
65802
public function validate($value): void
    {
        if (in_array($value, $this->values)) {
            return;
        }

        throw new Exception\Invalid(
            $value,
            "Invalid value, expected one of " . implode(",", $this->valuesString())
        );
    }
1161639
public function append($value)
    {
        if (!$value instanceof Argument) {
            throw new \InvalidArgumentException(sprintf('Controller arguments must be valid %s objects.', Argument::class), 1187953786);
        }
        $this->offsetSet(null, $value);
    }
191046
public function addTypeError($value, $type = '') {
        $type = $type ?: $this->getType();

        $this->validation->addError(
            $this->getName(),
            'type',
            [
                'type' => $type,
                'value' => is_scalar($value) ? $value : null,
                'messageCode' => is_scalar($value) ? "{value} is not a valid $type." : "The value is not a valid $type."
            ]
        );

        return $this;
    }
143553
public function setValue($value)
    {
        if (is_scalar($value)) {
            $this->value = new StringValue($value);
        } elseif (is_array($value)) {
            $this->value = new ArrayValue($value);
        } else {
            if (!$value instanceof ValueInterface) {
                throw new \Exception('The value must implement the ValueInterface.');
            } else {
                $this->value = $value;
            }
        }

        return $this;
    }
552939
public function setAmount($value)
    {
        if ($value === null || $value === '') {
            $this->_amount = null;
        } else {
            if (!is_scalar($value)) {
                if (!is_object($value) || !method_exists($value, '__toString')) {
                    throw new InvalidPropertyValueTypeException(
                        'Invalid amount value type', 0, 'amount.value', $value
                    );
                }
                $value = (string)$value;
            }
            if (!is_numeric($value) || $value < 0.0) {
                throw new InvalidPropertyValueException(
                    'Invalid amount value "' . $value . '"', 0, 'amount.value', $value
                );
            } elseif ($value < 0.01) {
                $this->_amount = null;
            } else {
                $this->_amount = number_format($value, 2, '.', '');
            }
        }
    }
488794
public static function set($option, $value)
    {
        if (!array_key_exists($option, self::$conf))
        {
            throw new InvalidArgumentException(sprintf('Unknown option "%s"', $option));
        }

        $expected = gettype(self::$conf[$option]);
        $actual = gettype($value);
        if ($expected != $actual)
        {
            throw new InvalidArgumentException(
                sprintf('Option "%s" has type "%s", but "%s" given',
                    $option, $expected, $actual)
            );
        }

        self::$conf[$option] = $value;
    }
545263
static function ByValue($value)
    {
        $calledClass = get_called_class();
        $allowed = self::AllowedClassValues($calledClass);
        if (!in_array($value, $allowed, true))
            throw new \InvalidArgumentException('Value ' . $value . ' not allowed for enum ' . $calledClass);
        
        return new $calledClass($value);
    }
295695
public function marshal($value)
    {
        if ($this->options()->allowed && !in_array($value, $this->options()->allowed)) {
            throw new \InvalidArgumentException(
                sprintf(
                    'Allowed values for this field are %s.',
                    implode(', ', $this->options()->allowed)
                )
            );
        }
        return new TypedValue((string) $value);
    }
248946
public function isValid($value)
    {
        if ( ! array_key_exists('fields', $this->getOptions())) {

            throw new \InvalidArgumentException(
                'Required option "property" not set for NoOtherObjectExists Validator.'
            );
        }

        if ( ! array_key_exists('class', $this->getOptions())) {

            throw new \InvalidArgumentException(
                'Required option "class" not set for NoOtherObjectExists Validator.'
            );
        }

        if ( ! array_key_exists('comparison_object', $this->getOptions())) {

            throw new \InvalidArgumentException(
                'Required option "comparison_object" not set for NoOtherObjectExists Validator.'
            );
        }

        if ( ! array_key_exists('identifier', $this->getOption('comparison_object'))) {

            throw new \InvalidArgumentException(
                'Required option "identifier" not set for NoOtherObjectExists Validator "comparison_object".'
            );
        }

        if ( ! array_key_exists('id', $this->getOption('comparison_object'))) {

            throw new \InvalidArgumentException(
                'Required option "id" not set for NoOtherObjectExists Validator "comparison_object".'
            );
        }

        $propertyName = $this->getOption('fields');
        $propertyClass = $this->getOption('class');

        $getterMethod = 'get' . ucfirst($propertyName);
        $cleanedValue = $this->cleanSearchValue($value);

        $comparisonObjectProperties = $this->getOption('comparison_object');
        $getIdentifier = 'get' . ucfirst($comparisonObjectProperties['identifier']);

        /** @var \object $comparisonObject */
        $comparisonObject = $this->objectRepository->findOneBy(
            [
                $comparisonObjectProperties['identifier'] => $comparisonObjectProperties['id'],
            ]
        );

        if ( ! method_exists($comparisonObject, $getterMethod)) {

            throw new \Exception(sprintf('Method %s() doesn\'t exist in class %s!', $getterMethod, $propertyClass));
        }

        $matches = $this->objectRepository->findBy($cleanedValue);
        if ($matches) {
            foreach ($matches as $index => $match) {
                // Check if the match has the same identifier as the object being edited. If it does continue, this is allowed.
                if (is_object($match)
                    && $match instanceof $propertyClass
                    && $match->$getIdentifier() === $comparisonObject->$getIdentifier()
                ) {

                    continue;
                }

                $this->error(self::ERROR_OBJECT_FOUND, $value);

                return false;
            }
        }

        return true;
    }
1338788
public function setValue($value) {
		if(!in_array($value, self::VALID_VALUES)) {
			throw new InvalidArgumentException('Value is not valid.');
		}

		$this->value = $value;

		return $this;
	}
102721
public function append($value)
    {
        if (!$value instanceof $this->currencyClass) {
            throw new \InvalidArgumentException(sprintf('$newval must be an instance of Currency, instance of "%s" given', get_class($value)));
        }

        parent::append($value);
    }
1162019
public function setValue($value) {
		if (!$this->isPrimitive($value)) {
			throw new \InvalidArgumentException('Use setValue() only for primitives and PhpConstant, anyway use setExpression() instead.');
		}
		$this->value = $value;
		$this->hasValue = true;

		return $this;
	}
998494
protected function checkValueStructure(BaseValue $value)
    {
        if (!is_array($value->tags)) {
            throw new InvalidArgumentType(
                '$value->tags',
                'array',
                $value->tags
            );
        }

        foreach ($value->tags as $tag) {
            if (!$tag instanceof Tag) {
                throw new InvalidArgumentType(
                    "${tag}",
                    Value::class,
                    $tag
                );
            }
        }
    }
550071
public function setParam($key, $value = null)
    {
        if (!$this->hasParam($key)) {
            throw new \InvalidArgumentException(sprintf('Cannot set %s as parameter cause this key does not exist.', $key));
        }

        if (is_object($value)) {
            throw new \InvalidArgumentException('Parameter\'s value cannot be type of object.');
        }

        $currentValue = $this->getParamValue($key);
        if (
            (null !== $value && null !== $currentValue)
            && (
                gettype($value) !== gettype($currentValue)
                && (
                    !(is_string($value) || is_integer($value))
                    || !(is_string($currentValue) || is_integer($currentValue))
                )
            )
        ) {
            throw new \InvalidArgumentException(sprintf(
                'Cannot replace %s\'s value, %s expected and %s given.',
                $key,
                gettype($currentValue),
                gettype($value)
            ));
        }

        if (null === $value) {
            unset($this->_parameters[$key]);
        } else {
            $this->_parameters[$key] = ['value' => $value];
        }

        return $this->getContentInstance();
    }
1276189
public function validateValueType($value)
    {
        if ($this->hasOption(Entity\Reflection\Property\Option\Primary::KEY)
            && (Entity\Reflection\Property\Option\Primary::isEmpty($value))
        ) {
            throw new Exception\InvalidArgumentException(
                "Primary value can not be empty string or null!",
                $value
            );
        }

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

        // Enumeration
        if ($this->hasOption(Entity\Reflection\Property\Option\Enum::KEY)
            && !$this->getOption(Entity\Reflection\Property\Option\Enum::KEY)->isValid($value)
        ) {
            throw new Exception\InvalidArgumentException(
                "Value " . $value . " is not from defined entity enumeration "
                . "range on property " . $this->name . "!",
                $value
            );
        }

        // Scalar or array type
        if (self::isScalarType($this->type) || $this->type === self::TYPE_ARRAY) {

            if (gettype($value) === $this->type) {
                return;
            }
            throw new Exception\InvalidArgumentException(
                "Expected " . $this->type . " but " . gettype($value)
                . " given on property " . $this->name . "!",
                $value
            );
        }

        // Object validation
        $givenType = gettype($value);
        if ($givenType === "object") {
            $givenType = get_class($value);
        }

        if ($this->type === self::TYPE_ENTITY) {
            // Entity

            $expectedEntityClass = Convention::nameToClass($this->typeOption, Convention::ENTITY_MASK);
            if ($value instanceof $expectedEntityClass) {
                return;
            } else {
                throw new Exception\InvalidArgumentException(
                    "Expected entity " . $this->typeOption . " but " . $givenType
                    . " given on property " . $this->name . "!",
                    $value
                );
            }
        } elseif ($this->type === self::TYPE_COLLECTION) {
            // Collection

            $entityClass = Convention::nameToClass($this->typeOption, Convention::ENTITY_MASK);
            if (!$value instanceof Entity\Collection) {

                throw new Exception\InvalidArgumentException(
                    "Expected entity collection but " . $givenType . " given on"
                    . " property " . $this->name . "!",
                    $value
                );
            } elseif ($value->getEntityClass() !== $entityClass) {
                throw new Exception\InvalidArgumentException(
                    "Expected collection of entity " . $entityClass
                    . " but collection of entity " . $value->getEntityClass()
                    . " given on property " . $this->name . "!",
                    $value
                );
            } else {
                return;
            }
        } elseif ($this->type === self::TYPE_DATETIME) {
            // DateTime

            if ($value instanceof \DateTime) {
                return;
            } else {
                throw new Exception\InvalidArgumentException(
                    "Expected DateTime but " . $givenType . " given on"
                    . " property " . $this->name . "!",
                    $value
                );
            }
        } elseif ($this->type === self::TYPE_DATE) {
            // Date

            if ($value instanceof \DateTime) {
                return;
            } else {
                throw new Exception\InvalidArgumentException(
                    "Expected date as DateTime object but " . $givenType
                    . " given on property " . $this->name . "!",
                    $value
                );
            }
        } else {
            // Unexpected

            throw new \Exception("Unsupported type " . $this->type . "!");
        }
    }
1056266
public function setWhen($value)
    {
        if (!is_callable($value) && $value !== null) {
            throw new InvalidArgumentException('When parameter must be callable type');
        }
        $this->when = $value;

        return $this;
    }