Skip to content

Dgame/php-object

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

php-object

Scrutinizer Code Quality codecov Build Status StyleCI Build Status


An intelligent facade around your object to protect it from any harm.

hasMethod

$facade = new ObjectFacade(new Exception());
$this->assertTrue($facade->hasMethod('getMessage'));
$this->assertFalse($facade->hasMethod('foo'));

hasProperty

$facade = new ObjectFacade(new Exception());
foreach (['message', 'code', 'file', 'line'] as $property) {
    $this->assertTrue($facade->hasProperty($property));
}
$this->assertFalse($facade->hasProperty('foo'));

getPropertyByName

$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

$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

$facade = new ObjectFacade(new Exception());
$method = $facade->getGetterMethod('message');

$this->assertNotNull($method);
$this->assertEquals('getMessage', $method->getName());

getSetterMethod

$facade = new ObjectFacade(
    new class() {
        public function setFoo()
        {
        }
    }
);

$method = $facade->getSetterMethod('foo');

$this->assertNotNull($method);
$this->assertEquals('setFoo', $method->getName());

getValueByMethod

$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

$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

$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

$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

$facade = new ObjectFacade(
    new class() {
        public $foo = 42;
    }
);

$facade->setValue('foo', 3537);
$this->assertEquals(3537, $facade->getValue('foo'));
new class() {
    private $foo = 42;

    public function setFoo(int $foo)
    {
        $this->foo = $foo;
    }
}

$facade->setValue('foo', 3537);
$this->assertEquals(3537, $facade->getValue('foo'));

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages