<?php
|
namespace JVBase\managers\SEO\render\Traits\_Helpers;
|
|
if (!defined('ABSPATH')) {
|
exit;
|
}
|
trait arrayHelper {
|
protected function classArray(string $property, array $array, string $className):array
|
{
|
return array_filter($array, function ($item) use ($property, $className) {
|
$test = class_exists($className) && is_a($item, $className);
|
if (!$test) {
|
error_log('[SEO]Item property '.$property.' is not an instance of '.$className);
|
}
|
return $test;
|
});
|
}
|
protected function mixedArray(string $property, array $array, array $allowedTypes):array
|
{
|
return array_filter($array, function($item) use ($property, $allowedTypes) {
|
$test = in_array(gettype($item), $allowedTypes) || $this->testClasses($item, $allowedTypes);
|
if (!$test) {
|
error_log('[SEO]Item property '.$property.' is not an allowed type: '.print_r($item, true));
|
}
|
return $test;
|
});
|
}
|
protected function testClasses(mixed $item, array $allowedTypes):bool
|
{
|
if (gettype($item) !== 'object') return false;
|
foreach ($allowedTypes as $type) {
|
if (!class_exists($type)) continue;
|
$test = is_a($item, $type);
|
if ($test){
|
return true;
|
}
|
}
|
return false;
|
}
|
protected function stringArray(string $property, array $array):array
|
{
|
return array_filter($array, function($item) use($property) {
|
if (!is_string($item)) {
|
error_log('[SEO]Item property '.$property.' is not string: '.print_r($item, true));
|
}
|
return is_string($item);
|
});
|
}
|
protected function urlArray(string $property, array $array):array
|
{
|
return array_filter($array, function($item) use($property) {
|
$test = filter_var($item, FILTER_VALIDATE_URL);
|
if (!$test) {
|
error_log('[SEO]Item property '.$property.' is not URL: '.print_r($item, true));
|
}
|
return $test;
|
});
|
}
|
protected function intArray(string $property, array $array):array
|
{
|
return array_filter($array, function($item) use($property) {
|
if (!is_int($item)) {
|
error_log('[SEO]Item property '.$property.' is not int: '.print_r($item, true));
|
}
|
return is_int($item);
|
});
|
}
|
protected function floatArray(string $property, array $array):array
|
{
|
return array_filter($array, function($item) use($property) {
|
if (!is_float($item)) {
|
error_log('[SEO]Item property '.$property.' is not float: '.print_r($item, true));
|
}
|
return is_float($item);
|
});
|
}
|
|
protected function testAllowed(string $property, array $mappedTypes, array $testTypes):array
|
{
|
return array_map(
|
function($item) use ($mappedTypes) {
|
return $mappedTypes[strtolower($item)];
|
},
|
array_filter($testTypes, function($item) use($property, $mappedTypes) {
|
$test = array_key_exists(strtolower($item), $mappedTypes);
|
if (!$test) {
|
error_log('[SEO]Item property '.$property.' has invalid type: '.$item);
|
}
|
return $test;
|
})
|
);
|
}
|
}
|