https://github.com/dgame/php-object
https://github.com/dgame/php-object
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/dgame/php-object
- Owner: Dgame
- Created: 2017-04-06T21:16:51.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2019-07-31T20:30:36.000Z (almost 7 years ago)
- Last Synced: 2025-03-10T17:55:39.299Z (over 1 year ago)
- Language: PHP
- Size: 49.8 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# php-object
[](https://scrutinizer-ci.com/g/Dgame/php-object/?branch=master)
[](https://codecov.io/gh/Dgame/php-object)
[](https://scrutinizer-ci.com/g/Dgame/php-object/build-status/master)
[](https://styleci.io/repos/87475805)
[](https://travis-ci.org/Dgame/php-object)
----
An intelligent facade around your object to protect it from any harm.
----
### hasMethod
```php
$facade = new ObjectFacade(new Exception());
$this->assertTrue($facade->hasMethod('getMessage'));
$this->assertFalse($facade->hasMethod('foo'));
```
### hasProperty
```php
$facade = new ObjectFacade(new Exception());
foreach (['message', 'code', 'file', 'line'] as $property) {
$this->assertTrue($facade->hasProperty($property));
}
$this->assertFalse($facade->hasProperty('foo'));
```
### getPropertyByName
```php
$facade = new ObjectFacade(new Exception());
foreach (['message', 'code', 'file', 'line'] as $name) {
$property = $facade->getPropertyByName($name);
$this->assertNotNull($property);
$this->assertEquals($name, $property->getName());
$this->assertNotEquals(0, ReflectionProperty::IS_PROTECTED & $property->getModifiers());
}
```
### getMethodByName
```php
$facade = new ObjectFacade(new Exception());
$method = $facade->getMethodByName('getMessage');
$this->assertNotNull($method);
$this->assertEquals('getMessage', $method->getName());
$this->assertNotEquals(0, (ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_FINAL) & $method->getModifiers());
```
### getGetterMethod
```php
$facade = new ObjectFacade(new Exception());
$method = $facade->getGetterMethod('message');
$this->assertNotNull($method);
$this->assertEquals('getMessage', $method->getName());
```
### getSetterMethod
```php
$facade = new ObjectFacade(
new class() {
public function setFoo()
{
}
}
);
$method = $facade->getSetterMethod('foo');
$this->assertNotNull($method);
$this->assertEquals('setFoo', $method->getName());
```
### getValueByMethod
```php
$exception = new Exception('Test');
$facade = new ObjectFacade($exception);
$this->assertEquals($exception, $facade->getObject());
$this->assertEquals('Test', $facade->getValueByMethod('message'));
$this->assertEquals($exception->getMessage(), $facade->getValueByMethod('message'));
$this->assertEquals($exception->getFile(), $facade->getValueByMethod('file'));
$this->assertEquals($exception->getLine(), $facade->getValueByMethod('line'));
$this->assertNull($facade->getValueByMethod('unknown'));
```
### getValueByProperty
```php
$facade = new ObjectFacade(
new class() {
public $foo = 42;
public $bar = Exception::class;
}
);
$this->assertEquals(42, $facade->getValueByProperty('foo'));
$this->assertEquals(Exception::class, $facade->getValueByProperty('bar'));
$this->assertNull($facade->getValueByProperty('unknown'));
$facade = new ObjectFacade(new Exception());
$this->assertNull($facade->getValueByProperty('line')); // not a public property
$this->assertNull($facade->getValueByProperty('file')); // not a public property
```
### setValueByMethod
```php
$facade = new ObjectFacade(
new class() {
private $foo = 42;
private $bar;
public function setFoo(int $foo)
{
$this->foo = $foo;
}
public function setFooBar()
{
$this->foo = 1;
$this->bar = 2;
}
public function getFoo(): int
{
return $this->foo;
}
public function setBar(int $bar = null)
{
$this->bar = $bar;
}
public function getBar()
{
return $this->bar;
}
}
);
$this->assertEquals(42, $facade->getValueByMethod('foo'));
$facade->setValueByMethod('foo', 23);
$this->assertEquals(23, $facade->getValueByMethod('foo'));
$facade->setValueByMethod('foo', null); // "setFoo" does not accept null => keep the old value
$this->assertEquals(23, $facade->getValueByMethod('foo'));
$facade->setValueByMethod('foo', 'abc'); // "setFoo" does not accept a string => keep the old value
$this->assertEquals(23, $facade->getValueByMethod('foo'));
$this->assertNull($facade->getValueByMethod('bar'));
$facade->setValueByMethod('bar', 1337);
$this->assertEquals(1337, $facade->getValueByMethod('bar'));
$facade->setValueByMethod('bar', null);
$this->assertNull($facade->getValueByMethod('bar'));
$facade->setValueByMethod('foobar', uniqid());
$this->assertEquals(1, $facade->getValueByMethod('foo'));
$this->assertEquals(2, $facade->getValueByMethod('bar'));
```
### setValueByProperty
```php
$facade = new ObjectFacade(
new class() {
public $foo = 42;
}
);
$this->assertEquals(42, $facade->getValueByProperty('foo'));
$this->assertEquals($facade->getObject()->foo, $facade->getValueByProperty('foo'));
$facade->setValueByProperty('foo', 23);
$this->assertEquals(23, $facade->getValueByProperty('foo'));
$this->assertEquals($facade->getObject()->foo, $facade->getValueByProperty('foo'));
$facade->setValueByProperty('foo', null);
$this->assertNull($facade->getValueByProperty('foo'));
$this->assertEquals($facade->getObject()->foo, $facade->getValueByProperty('foo'));
$facade->setValueByProperty('foo', 1337);
$this->assertEquals(1337, $facade->getValueByProperty('foo'));
$this->assertEquals($facade->getObject()->foo, $facade->getValueByProperty('foo'));
```
### setValue / getValue
```php
$facade = new ObjectFacade(
new class() {
public $foo = 42;
}
);
$facade->setValue('foo', 3537);
$this->assertEquals(3537, $facade->getValue('foo'));
```
```php
new class() {
private $foo = 42;
public function setFoo(int $foo)
{
$this->foo = $foo;
}
}
$facade->setValue('foo', 3537);
$this->assertEquals(3537, $facade->getValue('foo'));
```