public static function renderObjectProperty($object, $property) { if (is_array($object)) { return isset($object[$property]) ? $object[$property] : ''; } else { if (is_object($object) && isset($object->{$property})) { return isset($object->{$property}) ? $object->{$property} : ''; } } $getterCamelCased = 'get' . StaticConfigurator::toPascalCased($property); if (method_exists($object, $getterCamelCased)) { return $object->{$getterCamelCased}(); } $getter_underscored = 'get_' . StaticConfigurator::toUnderscored($property); if (method_exists($object, $getter_underscored)) { return $object->{$getter_underscored}(); } return ''; }
/** * * This function sorts all data by default $sortBy = 'id', and default sorting * direction $direction = 'asc' (ascending). * Returns sorted array of $data. * * @param array $data * @param \NetCore\FileSystem\type|string $sortBy * @param \NetCore\FileSystem\type|string $direction * @return array */ public static function sort(array $data, $sortBy = 'id', $direction = 'asc') { $sortFunction = function ($a, $b) use($sortBy, $direction) { $methodName = 'get' . StaticConfigurator::toPascalCased($sortBy); $aVal = $a->{$methodName}(); $bVal = $b->{$methodName}(); if (is_int($aVal) && is_int($bVal)) { if ($aVal > $bVal) { return $direction == 'asc' ? 1 : -1; } else { if ($aVal < $bVal) { return $direction == 'asc' ? -1 : 1; } else { return 0; } } } if ($direction == 'asc') { return strcmp($aVal, $bVal); } else { return strcmp($aVal, $bVal); } }; usort($data, $sortFunction); return $data; }