目的

  • 為了檢查自定義合格的 Node Type (String)

紀錄

程式

<?php
namespace Core\Constant;
 
class NodeType
{
    public const USER = 'user';
    
    public static function isAllowedNodeType( string $nodeType ) : bool
    {
        $class = new \ReflectionClass( __CLASS__ );
        
        $allowNodeTypes = array_values( $class->getConstants() );
        
        return in_array( $nodeType, $allowNodeTypes);
    }
}

使用方式

if( ! \Core\Constant\NodeType::isAllowedNodeType( $_POST['ndoeType'] )
{
      throw new \Exception('Error Type');
}