1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-06 06:07:39 +02:00

DibiObject: fixed compatibility with PHP 7

This commit is contained in:
David Grudl
2015-10-06 12:43:45 +02:00
parent d7885921e6
commit ac0ab9041b
2 changed files with 76 additions and 10 deletions

View File

@@ -209,8 +209,8 @@ abstract class DibiObject
} }
// property getter support // property getter support
$name[0] = $name[0] & "\xDF"; // case-sensitive checking, capitalize first character $uname = ucfirst($name);
$m = 'get' . $name; $m = 'get' . $uname;
if (self::hasAccessor($class, $m)) { if (self::hasAccessor($class, $m)) {
// ampersands: // ampersands:
// - uses & __get() because declaration should be forward compatible (e.g. with Nette\Web\Html) // - uses & __get() because declaration should be forward compatible (e.g. with Nette\Web\Html)
@@ -219,13 +219,12 @@ abstract class DibiObject
return $val; return $val;
} }
$m = 'is' . $name; $m = 'is' . $uname;
if (self::hasAccessor($class, $m)) { if (self::hasAccessor($class, $m)) {
$val = $this->$m(); $val = $this->$m();
return $val; return $val;
} }
$name = func_get_arg(0);
throw new LogicException("Cannot read an undeclared property $class::\$$name."); throw new LogicException("Cannot read an undeclared property $class::\$$name.");
} }
@@ -246,20 +245,18 @@ abstract class DibiObject
} }
// property setter support // property setter support
$name[0] = $name[0] & "\xDF"; // case-sensitive checking, capitalize first character $uname = ucfirst($name);
if (self::hasAccessor($class, 'get' . $name) || self::hasAccessor($class, 'is' . $name)) { if (self::hasAccessor($class, 'get' . $uname) || self::hasAccessor($class, 'is' . $uname)) {
$m = 'set' . $name; $m = 'set' . $name;
if (self::hasAccessor($class, $m)) { if (self::hasAccessor($class, $m)) {
$this->$m($value); $this->$m($value);
return; return;
} else { } else {
$name = func_get_arg(0);
throw new LogicException("Cannot assign to a read-only property $class::\$$name."); throw new LogicException("Cannot assign to a read-only property $class::\$$name.");
} }
} }
$name = func_get_arg(0);
throw new LogicException("Cannot assign to an undeclared property $class::\$$name."); throw new LogicException("Cannot assign to an undeclared property $class::\$$name.");
} }
@@ -271,8 +268,7 @@ abstract class DibiObject
*/ */
public function __isset($name) public function __isset($name)
{ {
$name[0] = $name[0] & "\xDF"; return $name !== '' && self::hasAccessor(get_class($this), 'get' . ucfirst($name));
return $name !== '' && self::hasAccessor(get_class($this), 'get' . $name);
} }

View File

@@ -0,0 +1,70 @@
<?php
use Tester\Assert;
require __DIR__ . '/bootstrap.php';
class TestClass extends DibiObject
{
public function getBar()
{
return 123;
}
public function isFoo()
{
return 456;
}
}
// calling
Assert::exception(function () {
$obj = new TestClass;
$obj->undeclared();
}, 'LogicException', 'Call to undefined method TestClass::undeclared().');
Assert::exception(function () {
TestClass::undeclared();
}, 'LogicException', 'Call to undefined static method TestClass::undeclared().');
// writing
Assert::exception(function () {
$obj = new TestClass;
$obj->undeclared = 'value';
}, 'LogicException', 'Cannot assign to an undeclared property TestClass::$undeclared.');
// property getter
$obj = new TestClass;
Assert::true(isset($obj->bar));
Assert::same(123, $obj->bar);
Assert::false(isset($obj->foo));
Assert::same(456, $obj->foo);
// reading
Assert::exception(function () {
$obj = new TestClass;
$val = $obj->undeclared;
}, 'LogicException', 'Cannot read an undeclared property TestClass::$undeclared.');
// unset/isset
Assert::exception(function () {
$obj = new TestClass;
unset($obj->undeclared);
}, 'LogicException', 'Cannot unset the property TestClass::$undeclared.');
Assert::false(isset($obj->undeclared));
// extension method
TestClass::extensionMethod('join', $func = function (TestClass $that, $separator) {
return $that->foo . $separator . $that->bar;
});
$obj = new TestClass;
Assert::same('456*123', $obj->join('*'));