Files
zhamao-framework/src/ZM/Annotation/AnnotationBase.php

69 lines
1.6 KiB
PHP
Raw Normal View History

2020-03-02 16:14:20 +08:00
<?php
declare(strict_types=1);
2020-03-02 16:14:20 +08:00
namespace ZM\Annotation;
abstract class AnnotationBase implements \IteratorAggregate, \Stringable
2020-03-02 16:14:20 +08:00
{
2022-12-19 01:45:27 +08:00
/** @var array|\Closure|string 方法名或闭包 */
public \Closure|string|array $method = '';
2020-03-02 16:14:20 +08:00
public $class = '';
2020-03-02 16:14:20 +08:00
2022-08-21 20:15:55 +08:00
public array $group = [];
public function __toString(): string
{
$str = self::class . ': ';
2020-03-02 16:14:20 +08:00
foreach ($this as $k => $v) {
$str .= "\n\t" . $k . ' => ';
if (is_string($v)) {
$str .= "\"{$v}\"";
} elseif (is_numeric($v)) {
$str .= $v;
} elseif (is_bool($v)) {
$str .= ($v ? 'TRUE' : 'FALSE');
} elseif (is_array($v)) {
$str .= json_encode($v, JSON_UNESCAPED_UNICODE);
} elseif ($v instanceof \Closure) {
$str .= '@AnonymousFunction';
} elseif (is_null($v)) {
$str .= 'NULL';
} else {
$str .= '@Unknown';
}
2020-03-02 16:14:20 +08:00
}
return $str;
}
2022-04-03 01:47:38 +08:00
2022-08-13 17:00:29 +08:00
/**
* InstantPlugin 下调用,设置回调或匿名函数
*/
public function on(\Closure|callable|string $method): AnnotationBase
2022-08-13 17:00:29 +08:00
{
$this->method = $method;
return $this;
}
public function getIterator(): \Traversable
2022-04-03 01:47:38 +08:00
{
return new \ArrayIterator($this);
2022-04-03 01:47:38 +08:00
}
2022-08-21 20:15:55 +08:00
public function isInGroup(string $name): bool
{
return in_array($name, $this->group);
}
public function addGroup(string $name)
{
$this->group[] = $name;
}
public function getGroups(): array
{
return $this->group;
}
}