59 lines
1.5 KiB
PHP
59 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace Notes\Common;
|
|
|
|
use Notes\Attribute\Ignore;
|
|
|
|
abstract class Reflected
|
|
{
|
|
public function hasIgnoreAttribute() : bool
|
|
{
|
|
return [] !== array_filter($this->attributes, fn($e) => $e->object instanceof Ignore);
|
|
}
|
|
|
|
public function allowsNull() : bool
|
|
{
|
|
return empty($this->type) || $this->expectType("null");
|
|
}
|
|
|
|
public function expectType(string $type) : bool
|
|
{
|
|
$type = strtolower($type);
|
|
|
|
foreach($this->getTypes() as $item) {
|
|
|
|
if ($type === "null") {
|
|
if ($item->type === "null" || $item->nullable) {
|
|
return true;
|
|
}
|
|
}
|
|
elseif ($type === $item->type) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function getTypes() : array
|
|
{
|
|
return is_array($this->type) ? $this->type : [ $this->type ];
|
|
}
|
|
|
|
public function typeFromReflection(\ReflectionProperty|\ReflectionParameter $property) : void
|
|
{
|
|
if ( $property->hasType() ) {
|
|
$type = $property->getType();
|
|
|
|
if ($type instanceof \ReflectionUnionType ) {
|
|
foreach($type->getTypes() as $item) {
|
|
$this->type[] = new ReflectedPropertyType($item->getName(), $item->isBuiltIn(), $item->allowsNull());
|
|
}
|
|
}
|
|
else {
|
|
$this->type = new ReflectedPropertyType($type->getName(), $type->isBuiltIn(), $type->allowsNull());
|
|
}
|
|
}
|
|
}
|
|
|
|
} |