<?php

namespace App\Traits;

trait IPTrait
{
  public function isIPAddressTrait(string $ip, $type = false): bool
  {
    switch ($type) {
      case 'ipv4':
        $result = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
        break;
      case 'ipv6':
        $result = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
        break;
      case 'all':
        $result = filter_var($ip, FILTER_VALIDATE_IP);
        break;
      default:
        $result = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE);
        break;
    }
    return $result;
  }
}
