<?php
/**
 *
 */

namespace CMSMSExt\core;

/**
 * Class Error
 *
 * A class to manage the error codes
 * a module generates
 * @author Fernando Morgado <Jo Morg>
 *
 * @package CMSMSExt\core
 */
class Error
{
  /**
   * This is a list of errors that can be extended
   * the format is originally a string constant
   * to be used as a lang key
   * but converted to an integer before being passed
   * to the parent's constructor as the code parameter
   */
  
  # this is a common error to all classes unless overridden...
  public const UNKNOWN                         = '000000';      # 'UNKNOWN' - NO WAY TO DETERMINE THE EXACT ERROR CAUSE
  public const GENERIC                         = '000001';      # 'GENERIC' - A CODE TO A GENERIC ERROR: USE THIS FOR INFREQUENT ERRORS
  
  # misc
  public const READ_ONLY_CFG                   = '000010';      # 'READ_ONLY_CFG'
  
  # file system
  public const CREATE_DIR_ERROR                = '000100';      # 'CREATE_DIR_ERROR'
  public const OPEN_DIR_ERROR                  = '000101';      # 'OPEN_DIR_ERROR'
  public const CREATE_FILE_ERROR               = '000102';      # 'CREATE_FILE_ERROR'
  public const COPY_FILE_ERROR                 = '000103';      # 'COPY_FILE_ERROR'
  public const DELETE_FILE_ERROR               = '000104';      # 'DELETE_FILE_ERROR'
  public const OPEN_FILE_ERROR                 = '000105';      # 'OPEN_FILE_ERROR'
  public const READ_FILE_ERROR                 = '000106';      # 'READ_FILE_ERROR'
  public const WRITE_FILE_ERROR                = '000107';      # 'WRITE_FILE_ERROR'
  public const INVALID_DIR_ERROR               = '000200';      # 'INVALID_DIR_ERROR'
  
  #then just add to this list here and in the lang file of the module
  
  /**
   * Returns the current list of error codes constants the class currently handles
   * @return array
   */
  public static function GetErrorsList() : array
  {
    return ( new \ReflectionClass(static::class) )->getConstants();
  }
  
  /**
   * Tests if a code is currently handled by this class
   *
   * @param $code
   *
   * @return bool
   */
  public static function HasCode($code) : bool
  {
    $codes = self::GetErrorsList();
    return \in_array($code, $codes, FALSE);
  }
  
}

?>