public static function setProperty(&$subject, $propertyName, $propertyValue, bool $forceDirectAccess = false): bool
    {
        if (is_array($subject)) {
            $subject[$propertyName] = $propertyValue;
            return true;
        }

        if (!is_object($subject)) {
            throw new \InvalidArgumentException('subject must be an object or array, ' . gettype($subject) . ' given.', 1237301368);
        }
        if (!is_string($propertyName) && !is_int($propertyName)) {
            throw new \InvalidArgumentException('Given property name/index is not of type string or integer.', 1231178878);
        }

        if ($forceDirectAccess === true) {
            $className = TypeHandling::getTypeForValue($subject);
            if (property_exists($className, $propertyName)) {
                $propertyReflection = new \ReflectionProperty($className, $propertyName);
                $propertyReflection->setAccessible(true);
                $propertyReflection->setValue($subject, $propertyValue);
            } else {
                $subject->$propertyName = $propertyValue;
            }
        } elseif (is_callable([$subject, $setterMethodName = self::buildSetterMethodName($propertyName)])) {
            $subject->$setterMethodName($propertyValue);
        } elseif ($subject instanceof \ArrayAccess) {
            $subject[$propertyName] = $propertyValue;
        } elseif (array_key_exists($propertyName, get_object_vars($subject))) {
            $subject->$propertyName = $propertyValue;
        } else {
            return false;
        }
        return true;
    }
public static function setProperty(&$subject, $propertyName, $propertyValue, bool $forceDirectAccess = false): bool
    {
        if (is_array($subject)) {
            $subject[$propertyName] = $propertyValue;
            return true;
        }

        if (!is_object($subject)) {
            throw new \InvalidArgumentException('subject must be an object or array, ' . gettype($subject) . ' given.', 1237301368);
        }
        if (!is_string($propertyName) && !is_int($propertyName)) {
            throw new \InvalidArgumentException('Given property name/index is not of type string or integer.', 1231178878);
        }

        if ($forceDirectAccess === true) {
            $className = TypeHandling::getTypeForValue($subject);
            if (property_exists($className, $propertyName)) {
                $propertyReflection = new \ReflectionProperty($className, $propertyName);
                $propertyReflection->setAccessible(true);
                $propertyReflection->setValue($subject, $propertyValue);
            } else {
                $subject->$propertyName = $propertyValue;
            }
        } elseif (is_callable([$subject, $setterMethodName = self::buildSetterMethodName($propertyName)])) {
            $subject->$setterMethodName($propertyValue);
        } elseif ($subject instanceof \ArrayAccess) {
            $subject[$propertyName] = $propertyValue;
        } elseif (array_key_exists($propertyName, get_object_vars($subject))) {
            $subject->$propertyName = $propertyValue;
        } else {
            return false;
        }
        return true;
    }
public static function setProperty ( & $ subject , $ propertyName , $ propertyValue , bool $ forceDirectAccess = false ) : bool { if ( is_array ( $ subject ) ) { $ subject [ $ propertyName ] = $ propertyValue ; return true ; } if ( ! is_object ( $ subject ) ) { throw new \ InvalidArgumentException ( 'subject must be an object or array, ' . gettype ( $ subject ) . ' given.' , 1237301368 ) ; } if ( ! is_string ( $ propertyName ) && ! is_int ( $ propertyName ) ) { throw new \ InvalidArgumentException ( 'Given property name/index is not of type string or integer.' , 1231178878 ) ; } if ( $ forceDirectAccess === true ) { $ className = TypeHandling :: getTypeForValue ( $ subject ) ; if ( property_exists ( $ className , $ propertyName ) ) { $ propertyReflection = new \ ReflectionProperty ( $ className , $ propertyName ) ; $ propertyReflection -> setAccessible ( true ) ; $ propertyReflection -> setValue ( $ subject , $ propertyValue ) ; } else { $ subject -> $ propertyName = $ propertyValue ; } } elseif ( is_callable ( [ $ subject , $ setterMethodName = self :: buildSetterMethodName ( $ propertyName ) ] ) ) { $ subject -> $ setterMethodName ( $ propertyValue ) ; } elseif ( $ subject instanceof \ ArrayAccess ) { $ subject [ $ propertyName ] = $ propertyValue ; } elseif ( array_key_exists ( $ propertyName , get_object_vars ( $ subject ) ) ) { $ subject -> $ propertyName = $ propertyValue ; } else { return false ; } return true ; }
Set a property for a given object.

Tries to set the property the following ways:

- if target is an array, set value
- if super cow powers should be used, set value through reflection
- if public setter method exists, call it.
- if public property exists, set it directly.
- if the target object is an instance of ArrayAccess, it sets the property
on it without checking if it existed.
- else, return false

@param mixed $subject The target object or array
@param string|integer $propertyName Name or index of the property to set
@param mixed $propertyValue Value of the property
@param boolean $forceDirectAccess directly access property using reflection(!)
@return boolean true if the property could be set, false otherwise
@throws \InvalidArgumentException in case $object was not an object or $propertyName was not a string
Set a property for a given object .