Helpers

The 'Helpers' folder can be used to include classes that don't belong to certain controller or model. A sample of a helper class could be 'Util' where you put some useful functions that can be used by all other objects in your program, like some reoccuring calculations or response functions.

Sample of a helper: Util.php

Please notice that this is an abstract class, so all functions are static functions

<?php

namespace app\Helpers;

use Slim\Http\Response;

abstract class Util
{
    static function Response($fb, Response $response)
    {
        $response = $response->write(json_encode($fb))
                             ->withHeader('Content-type', 'application/javascript');
        return $response;
    }

    static function ParameterError($request)
    {

        $errors = $request->getAttribute('input.errors');
        $good = $request->getAttribute('input.inputValues');

        return [
            'success' => false,
            'message' => _('Parameter Error'),
            'errors' => $errors,
            'good' => $good
        ];
    }

    static function removeCRLF($text)
    {
        $text = str_replace("\x0D", "", $text);
        $text = str_replace("\x0A", "", $text);

        return $text;
    }

    static function cvDate($date, $format = null)
    {

        $fmt = ($format == null) ? 'Y-m-d' : $format;
        $time = strtotime($date);

        return date($fmt, $time);
    }

    static function cvTime($date, $format = null)
    {

        $fmt = ($format == null) ? 'H:i:s' : $format;
        $time = strtotime($date);

        return date($fmt, $time);
    }

    static function takeNotNull($v1 = null, $fallback = null)
    {
        if (!is_null($v1)) {
            return $v1;
        }

        return $fallback;
    }

    static function takeBoolean($v1 = null, $fallback = null)
    {
        $booleanTrue = [true, 'on', 'true', '1', 1, 'true'];
        $booleanFalse = [false, 'off', '0', 0, 'false'];

        if ($v1 !== null) {

            if (in_array($v1, $booleanTrue)) {
                return true;
            }
            if (in_array($v1, $booleanFalse)) {
                return false;
            }

        }

        return $fallback;

    }

    static function ArraySearch($array, $key, $value)
    {
        $results = [];

        if (is_array($array)) {
            if (isset($array[$key]) && $array[$key] == $value) {
                $results[] = $array;
            }

            foreach ($array as $subarray) {
                $results = array_merge($results, self::ArraySearch($subarray, $key, $value));
            }
        }

        return $results;
    }

    static function ArraySort(&$array, $subkey = "id", $sort_ascending = false)
    {

        $temp_array = [];

        if (count($array)) {
            $temp_array[key($array)] = array_shift($array);
        }

        foreach ($array as $key => $val) {
            $offset = 0;
            $found = false;
            foreach ($temp_array as $tmp_key => $tmp_val) {
                if (!$found and strtolower($val[$subkey]) > strtolower($tmp_val[$subkey])) {
                    $temp_array = array_merge((array)array_slice($temp_array, 0, $offset), [$key => $val], array_slice($temp_array, $offset));
                    $found = true;
                }
                $offset++;
            }
            if (!$found) {
                $temp_array = array_merge($temp_array, [$key => $val]);
            }
        }

        if ($sort_ascending) {
            $array = array_reverse($temp_array);
        } else {
            $array = $temp_array;
        }

        return $array;
    }
}
Last update: Tue, 13 Sep 2022 14:32:15