This repository has been archived by the owner on Jul 30, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
); | ||
} | ||
|
||
} |