File size: 3,508 Bytes
07c3cdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
namespace ProcessMaker\Project;

use ProcessMaker\Core\System;
use ProcessMaker\Util\Logger;

/**
 * Class Handler
 *
 * @package ProcessMaker\Project
 * @author Erik Amaru Ortiz <aortiz.erik@gmail.com, erik@colosa.com>
 */
abstract class Handler
{
    public static function load($uid)
    {   // This method must be implemented on children classes, this is not declared abstract since PHP 5.3.x
        // don't allow any more static abstract methods.
        return null;
    }

    public abstract function create($data);
    //public abstract function update();
    public abstract function remove();

    protected static function diffArrayByKey($key, $list, $targetList)
    {
        $uid = array();
        $diff = array();

        foreach ($list as $item) {
            if (array_key_exists($key, $item)) {
                $uid[] = $item[$key];
            }
        }

        foreach ($targetList as $item) {
            if (! in_array($item[$key], $uid)) {
                $diff[] = $item[$key];
            }
        }

        return $diff;
    }

    protected static function getArrayChecksum($list, $key = null)
    {
        $checksum = array();

        foreach ($list as $k => $item) {
            if (empty($key)) {
                $checksum[$k] = self::getChecksum($item);
            } else {
                $checksum[$item[$key]] = self::getChecksum($item);
            }
        }

        return $checksum;
    }

    protected static function getChecksum($data)
    {
        if (! is_string($data)) {
            ksort($data);
            $data = print_r($data, true);
        }

        return sha1($data);
    }

    public static function filterCollectionArrayKeys($data, $filter = array())
    {
        $result = array();

        foreach ($data as $row) {
            $result[] = self::filterArrayKeys($row, $filter);
        }

        return $result;
    }

    public static function filterArrayKeys($data, $filter = array())
    {
        $result = array();

        foreach ($data as $key => $value) {
            if (! in_array($key, $filter)) {
                $result[$key] = $value;
            }
        }

        return $result;
    }

    public static function isEquals($array, $arrayCompare)
    {
        //self::log("ONE: ", $array, "TWO: ", $arrayCompare);
        //$ret = array_diff_assoc("ONE: ", $array, "TWO: ", $arrayCompare);
        return (self::getChecksum($array) === self::getChecksum($arrayCompare));
    }

    /**
     * Log in ProcessMaker Standard Output if debug mode is enabled.
     *
     * @author Erik Amaru Ortiz <aortiz.erik at icloud dot com>
     * @internal param $args this method receives N-Arguments dynamically with any type, string, array, object, etc
     *                       it means that you ca use it by example:
     *
     * self::log("Beginning transaction");
     * self::log("Method: ", __METHOD__, 'Returns: ', $result);
     *
     */
    public static function logstr($str)
    {
        if (System::isDebugMode()) {
            Logger::getInstance()->setLog($str);
        }
    }

    public static function logInline()
    {
        if (System::isDebugMode()) {
            call_user_func_array(array(Logger::getInstance(), 'setLogInline'), func_get_args());
        }
    }

    public static function log()
    {
        if (System::isDebugMode()) {
            $logger = Logger::getInstance();
            call_user_func_array(array($logger, 'setLogLine'), func_get_args());
        }
    }
}