2020-03-02 16:14:20 +08:00
|
|
|
<?php
|
|
|
|
|
|
2022-03-15 18:05:33 +08:00
|
|
|
declare(strict_types=1);
|
2020-03-02 16:14:20 +08:00
|
|
|
|
|
|
|
|
namespace ZM\Annotation;
|
|
|
|
|
|
2022-12-19 20:22:47 +08:00
|
|
|
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
|
|
|
|
2022-03-20 01:53:36 +08:00
|
|
|
public $class = '';
|
2020-03-02 16:14:20 +08:00
|
|
|
|
2022-08-21 20:15:55 +08:00
|
|
|
public array $group = [];
|
|
|
|
|
|
2022-12-19 20:22:47 +08:00
|
|
|
public function __toString(): string
|
2022-03-15 18:05:33 +08:00
|
|
|
{
|
2022-12-19 20:22:47 +08:00
|
|
|
$str = self::class . ': ';
|
2020-03-02 16:14:20 +08:00
|
|
|
foreach ($this as $k => $v) {
|
2022-03-15 18:05:33 +08:00
|
|
|
$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);
|
2022-11-03 10:18:17 +08:00
|
|
|
} elseif ($v instanceof \Closure) {
|
2022-03-15 18:05:33 +08:00
|
|
|
$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 下调用,设置回调或匿名函数
|
|
|
|
|
*/
|
2022-12-19 20:22:47 +08:00
|
|
|
public function on(\Closure|callable|string $method): AnnotationBase
|
2022-08-13 17:00:29 +08:00
|
|
|
{
|
|
|
|
|
$this->method = $method;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-03 10:18:17 +08:00
|
|
|
public function getIterator(): \Traversable
|
2022-04-03 01:47:38 +08:00
|
|
|
{
|
2022-11-03 10:18:17 +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;
|
|
|
|
|
}
|
2022-03-15 18:05:33 +08:00
|
|
|
}
|