Utilities > Reflection > setPropertyValue() method > sets value of given property

This commit is contained in:
Meritoo
2018-07-29 16:00:13 +02:00
parent 60d7b03cd7
commit 535ae65e5e
4 changed files with 154 additions and 0 deletions

View File

@@ -6,6 +6,7 @@ Common and useful classes, methods, exceptions etc.
1. Documentation > Value Objects 1. Documentation > Value Objects
2. Docker > improve performance 2. Docker > improve performance
3. Utilities > Reflection > setPropertyValue() method > sets value of given property
# 0.1.1 # 0.1.1

View 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);
}
}

View File

@@ -13,6 +13,7 @@ use Doctrine\Common\Util\Inflector;
use Meritoo\Common\Collection\Collection; use Meritoo\Common\Collection\Collection;
use Meritoo\Common\Exception\Reflection\CannotResolveClassNameException; use Meritoo\Common\Exception\Reflection\CannotResolveClassNameException;
use Meritoo\Common\Exception\Reflection\MissingChildClassesException; use Meritoo\Common\Exception\Reflection\MissingChildClassesException;
use Meritoo\Common\Exception\Reflection\NotExistingPropertyException;
use Meritoo\Common\Exception\Reflection\TooManyChildClassesException; use Meritoo\Common\Exception\Reflection\TooManyChildClassesException;
/** /**
@@ -659,4 +660,36 @@ class Reflection
return $parentClass->getName(); 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);
}
}
} }

View File

@@ -13,6 +13,7 @@ use Generator;
use Meritoo\Common\Collection\Collection; use Meritoo\Common\Collection\Collection;
use Meritoo\Common\Exception\Reflection\CannotResolveClassNameException; use Meritoo\Common\Exception\Reflection\CannotResolveClassNameException;
use Meritoo\Common\Exception\Reflection\MissingChildClassesException; use Meritoo\Common\Exception\Reflection\MissingChildClassesException;
use Meritoo\Common\Exception\Reflection\NotExistingPropertyException;
use Meritoo\Common\Exception\Reflection\TooManyChildClassesException; use Meritoo\Common\Exception\Reflection\TooManyChildClassesException;
use Meritoo\Common\Test\Base\BaseTestCase; use Meritoo\Common\Test\Base\BaseTestCase;
use Meritoo\Common\Test\Utilities\Reflection\A; use Meritoo\Common\Test\Utilities\Reflection\A;
@@ -474,6 +475,37 @@ class ReflectionTest extends BaseTestCase
static::assertEquals('name', $property->getName()); static::assertEquals('name', $property->getName());
} }
/**
* @param mixed $object Object that should contains given property
* @param string $property Name of the property
*
* @dataProvider provideObjectAndNotExistingProperty
*/
public function testSetPropertyValueUsingNotExistingProperty($object, $property)
{
$this->setExpectedException(NotExistingPropertyException::class);
$object = new \stdClass();
Reflection::setPropertyValue($object, 'test', 'test test test');
}
/**
* @param mixed $object Object that should contains given property
* @param string $property Name of the property
* @param mixed $value Value of the property
*
* @dataProvider provideObjectPropertyAndValue
*/
public function testSetPropertyValue($object, $property, $value)
{
$oldValue = Reflection::getPropertyValue($object, $property);
Reflection::setPropertyValue($object, $property, $value);
$newValue = Reflection::getPropertyValue($object, $property);
static::assertNotSame($oldValue, $value);
static::assertSame($newValue, $value);
}
/** /**
* Provides invalid class and trait * Provides invalid class and trait
* *
@@ -501,4 +533,59 @@ class ReflectionTest extends BaseTestCase
[], [],
]; ];
} }
/**
* Provides object and name of not existing property
*
* @return Generator
*/
public function provideObjectAndNotExistingProperty()
{
yield[
new \stdClass(),
'test',
];
yield[
new A(),
'test',
];
yield[
new B(),
'firstName',
];
}
/**
* Provides object, name of property and value of the property
*
* @return Generator
*/
public function provideObjectPropertyAndValue()
{
yield[
new A(),
'count',
123,
];
yield[
new B(),
'name',
'test test',
];
yield[
new G(),
'firstName',
'Jane',
];
yield[
new G(),
'lastName',
'Smith',
];
}
} }