File size: 2,081 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
<?php

namespace ProcessMaker\Util;
use G;

/**
 * Constructs the name of the variable starting from a string representing the
 * depth of the array.
 */
class ParseSoapVariableName
{
    /**
     * Constructs the name of the variable starting from a string representing
     * the depth of the array.
     *
     * @param array  $field
     * @param string $name
     * @param object $value
     * @return void
     */
    public function buildVariableName(&$field, $name, $value)
    {
        if (!$this->isValidVariableName($name)) {
            $context = \Bootstrap::getDefaultContextLog();
            $context['action'] = 'soap2';
            $context['exception'] = 'Invalid param: '.G::json_encode($name);
            \Bootstrap::registerMonolog('soap2', 400, 'NewCase', $context, $context['workspace'], 'processmaker.log');

            return;
        }

        $brackets = $this->searchBrackets($name);
        if (empty($brackets)) {
            $field[$name] = $value;
        } else {
            $current = &$field;
            foreach ($brackets as $extension) {
                if (!isset($current[$extension])) {
                    $current[$extension] = [];
                }
                $current = &$current[$extension];
            }
            $current = $value;
        }
    }

    /**
     * Analysis of string representing the depth of the array, represented by a
     * valid index name and brackets as separators.
     *
     * @param type $string
     *
     * @return array
     */
    private function searchBrackets($string)
    {
        $stringClean = str_replace(' ', '', $string);
        $explode = explode('][', $stringClean);

        return $explode;
    }

    /**
     * Verify if the index name of the array is valid.
     *
     * @param string $name
     *
     * @return bool
     */
    public function isValidVariableName($name)
    {
        if (is_string($name) === true) {
            if (preg_match("/^[0-9a-zA-Z\_\[\]]+$/", $name)) {
                return true;
            }
        }

        return false;
    }
}