Skip to content
This repository has been archived by the owner on Jul 30, 2020. It is now read-only.

Commit

Permalink
+ bind
Browse files Browse the repository at this point in the history
  • Loading branch information
rez1dent3 committed Sep 27, 2017
1 parent 85bdcbc commit 9c9bdee
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
14 changes: 14 additions & 0 deletions demo/bind.php
@@ -0,0 +1,14 @@
<?php

require_once dirname(__DIR__) . '/vendor/autoload.php';

class Obj
{
protected $a = 123;
}

$obj = new Obj();

\Bavix\Tests\Bind::setProperty($obj, 'a', 321);

var_dump(\Bavix\Tests\Bind::getProperty($obj, 'a'));
39 changes: 39 additions & 0 deletions src/Tests/Bind.php
@@ -0,0 +1,39 @@
<?php

namespace Bavix\Tests;

abstract class Bind
{

/**
* @param object $object
* @param string $attr
*
* @return mixed
*/
public static function getProperty($object, $attr)
{
$closure = \Closure::bind(function () use ($attr) {
return $this->$attr;
}, $object, \get_class($object));

return $closure();
}

/**
* @param object $object
* @param string $attr
* @param mixed $value
*
* @return mixed
*/
public static function setProperty($object, $attr, $value)
{
$closure = \Closure::bind(function ($value) use ($attr) {
$this->$attr = $value;
}, $object, \get_class($object));

return $closure($value);
}

}
30 changes: 30 additions & 0 deletions tests/BindTest.php
@@ -0,0 +1,30 @@
<?php

namespace Tests;

use Bavix\Tests\Bind;
use Bavix\Tests\Unit;

class BindTest extends Unit
{

public function testGetProperty()
{
$this->assertSame(
(string)$this->tmp,
Bind::getProperty($this->tmp, 'file')
);
}

public function testSetProperty()
{
$file = __FILE__ . '.tmp';
Bind::setProperty($this->tmp, 'file', $file);

$this->assertSame(
(string)$this->tmp,
$file
);
}

}

0 comments on commit 9c9bdee

Please sign in to comment.