- Renamed std::ordered_map to std::orderedMap following camelCase naming convention - Added new std::object() function to restore concrete object type information - Updated all test files to use the new orderedMap naming - Modified parser to recognize orderedmap in assign operations - Updated documentation to reflect the new naming and function - Added polyfill implementation for std::object() - Updated README and changelog withmaster
parent
055dee01f8
commit
2fe09697fa
40 changed files with 261 additions and 93 deletions
@ -0,0 +1,40 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase; |
||||||
|
|
||||||
|
class StdObjectPolyfillBase |
||||||
|
{ |
||||||
|
} |
||||||
|
|
||||||
|
final class StdObjectPolyfillChild extends StdObjectPolyfillBase |
||||||
|
{ |
||||||
|
} |
||||||
|
|
||||||
|
final class StdObjectPolyfillTest extends TestCase |
||||||
|
{ |
||||||
|
public function testReturnsAnObjectOfTheRequestedClass(): void |
||||||
|
{ |
||||||
|
$object = new StdObjectPolyfillBase(); |
||||||
|
|
||||||
|
self::assertSame($object, std::object($object, StdObjectPolyfillBase::class)); |
||||||
|
|
||||||
|
$child = new StdObjectPolyfillChild(); |
||||||
|
self::assertSame($child, std::object($child, StdObjectPolyfillBase::class)); |
||||||
|
} |
||||||
|
|
||||||
|
public function testRejectsANonObjectValue(): void |
||||||
|
{ |
||||||
|
$this->expectException(TypeError::class); |
||||||
|
$this->expectExceptionMessage('must be an object, int given'); |
||||||
|
|
||||||
|
std::object(42, StdObjectPolyfillBase::class); |
||||||
|
} |
||||||
|
|
||||||
|
public function testRejectsAnUnrelatedObject(): void |
||||||
|
{ |
||||||
|
$this->expectException(TypeError::class); |
||||||
|
$this->expectExceptionMessage('must be an instance of StdObjectPolyfillBase, stdClass given'); |
||||||
|
|
||||||
|
std::object(new stdClass(), StdObjectPolyfillBase::class); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,13 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase; |
||||||
|
|
||||||
|
final class StdOrderedMapPolyfillTest extends TestCase |
||||||
|
{ |
||||||
|
public function testUsesCamelCaseFactoryName(): void |
||||||
|
{ |
||||||
|
self::assertSame([], std::orderedMap(Type::String, Type::Int)); |
||||||
|
self::assertTrue(method_exists(std::class, 'orderedMap')); |
||||||
|
self::assertFalse(method_exists(std::class, 'ordered_map')); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,60 @@ |
|||||||
|
--TEST-- |
||||||
|
std::object restores concrete object types and validates them at runtime |
||||||
|
--FILE-- |
||||||
|
<?php |
||||||
|
|
||||||
|
class StdObjectBase { |
||||||
|
public function name(): string { |
||||||
|
return 'base'; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
class StdObjectChild extends StdObjectBase { |
||||||
|
public function childName(): string { |
||||||
|
return 'child'; |
||||||
|
} |
||||||
|
|
||||||
|
public function fromSelf(mixed $value): self { |
||||||
|
return STD::ObJeCt($value, self::class); |
||||||
|
} |
||||||
|
|
||||||
|
public function fromParent(mixed $value): StdObjectBase { |
||||||
|
return std::object($value, parent::class); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
function restore_child(mixed $value): StdObjectChild { |
||||||
|
return std::object($value, StdObjectChild::class); |
||||||
|
} |
||||||
|
|
||||||
|
function restore_child_by_name(mixed $value): StdObjectChild { |
||||||
|
return std::object($value, 'StdObjectChild'); |
||||||
|
} |
||||||
|
|
||||||
|
function main(): void { |
||||||
|
$child = new StdObjectChild(); |
||||||
|
var_dump(restore_child($child)->childName()); |
||||||
|
var_dump(restore_child_by_name($child)->childName()); |
||||||
|
var_dump($child->fromSelf($child)->childName()); |
||||||
|
var_dump($child->fromParent($child)->name()); |
||||||
|
|
||||||
|
try { |
||||||
|
std::object(new stdClass(), StdObjectChild::class); |
||||||
|
} catch (TypeError $error) { |
||||||
|
echo $error::class, "\n"; |
||||||
|
} |
||||||
|
|
||||||
|
try { |
||||||
|
std::object(42, StdObjectChild::class); |
||||||
|
} catch (TypeError $error) { |
||||||
|
echo $error::class, "\n"; |
||||||
|
} |
||||||
|
} |
||||||
|
?> |
||||||
|
--EXPECT-- |
||||||
|
string(5) "child" |
||||||
|
string(5) "child" |
||||||
|
string(5) "child" |
||||||
|
string(4) "base" |
||||||
|
TypeError |
||||||
|
TypeError |
||||||
Loading…
Reference in new issue