<?php

namespace Planaday\Utility\DataGrouping;

use ArrayAccess;
use Countable;
use Planaday\Utility\DataGrouping\ArrayFunctions\InArray;
use Traversable;

class ArrayFunctions {
	public static function is_arrayType(mixed $value): bool {
		return
			is_array( $value ) || (
				$value instanceof ArrayAccess &&
				$value instanceof Traversable &&
				$value instanceof Countable
			);
	}

	public static function in_array(
		mixed $needle,
		ArrayAccess&Traversable&Countable $haystack,
		bool $strict = false
	): bool {
		if ( $haystack instanceof InArray ) return $haystack->in_array( $needle, $strict );

		foreach ( $haystack as $item ) {
			if (
				$strict ?
					$item === $needle :
					( $item == $needle || ((is_string($needle) || is_object($needle)) && $item instanceof $needle) )
			) {
				return true;
			}
		}

		return false;
	}

}