mirror of
https://github.com/wiosna-dev/common-library.git
synced 2026-03-12 09:31:51 +01:00
Utilities > Reflection > setPropertyValue() method > sets value of given property
This commit is contained in:
33
src/Exception/Reflection/NotExistingPropertyException.php
Normal file
33
src/Exception/Reflection/NotExistingPropertyException.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* (c) Meritoo.pl, http://www.meritoo.pl
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Meritoo\Common\Exception\Reflection;
|
||||
|
||||
/**
|
||||
* An exception used while property does not exist in instance of class
|
||||
*
|
||||
* @author Meritoo <github@meritoo.pl>
|
||||
* @copyright Meritoo <http://www.meritoo.pl>
|
||||
*/
|
||||
class NotExistingPropertyException extends \Exception
|
||||
{
|
||||
/**
|
||||
* Creates exception
|
||||
*
|
||||
* @param mixed $object Object that should contains given property
|
||||
* @param string $property Name of the property
|
||||
* @return NotExistingPropertyException
|
||||
*/
|
||||
public static function create($object, $property)
|
||||
{
|
||||
$template = 'Property \'%s\' does not exist in instance of class \'%s\'. Did you use proper name of property?';
|
||||
$message = sprintf($template, $property, get_class($object));
|
||||
|
||||
return new static($message);
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ use Doctrine\Common\Util\Inflector;
|
||||
use Meritoo\Common\Collection\Collection;
|
||||
use Meritoo\Common\Exception\Reflection\CannotResolveClassNameException;
|
||||
use Meritoo\Common\Exception\Reflection\MissingChildClassesException;
|
||||
use Meritoo\Common\Exception\Reflection\NotExistingPropertyException;
|
||||
use Meritoo\Common\Exception\Reflection\TooManyChildClassesException;
|
||||
|
||||
/**
|
||||
@@ -659,4 +660,36 @@ class Reflection
|
||||
|
||||
return $parentClass->getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets value of given property
|
||||
*
|
||||
* @param mixed $object Object that should contains given property
|
||||
* @param string $property Name of the property
|
||||
* @param mixed $value Value of the property
|
||||
* @throws NotExistingPropertyException
|
||||
*/
|
||||
public static function setPropertyValue($object, $property, $value)
|
||||
{
|
||||
$reflectionProperty = self::getProperty($object, $property);
|
||||
|
||||
/*
|
||||
* Oops, property does not exist
|
||||
*/
|
||||
if (null === $reflectionProperty) {
|
||||
throw NotExistingPropertyException::create($object, $property);
|
||||
}
|
||||
|
||||
$notAccessible = $reflectionProperty->isPrivate() || $reflectionProperty->isProtected();
|
||||
|
||||
if ($notAccessible) {
|
||||
$reflectionProperty->setAccessible(true);
|
||||
}
|
||||
|
||||
$reflectionProperty->setValue($object, $value);
|
||||
|
||||
if ($notAccessible) {
|
||||
$reflectionProperty->setAccessible(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user