AttributesMassAssignable   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 0
dl 0
loc 55
c 0
b 0
f 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A massAssignAttributes() 0 18 4
1
<?php
2
 
3
4
namespace PatricPoba\MtnMomo\Utilities;
5
6
/**
7
 * MassAssignable Trait
8
 *
9
 * looks through fillable fields and matches them with provided
10
 * to enable mass assignment option using a constructor
11
 * or a make function.
12
 */
13
trait AttributesMassAssignable
14
{
15
    /**
16
     * This method is used to mass assign the properties required in a class.
17
     *  
18
     * It loops through the fields marked as required and optional
19
     * and then assisngs values to those fields using accessors
20
     * available in the class for those required options.
21
     *
22
     * @param   array $data
23
     * @example [
24
     *              'walletId' => 'some-existing-walletId',
25
     *              'transactionId' => 'someExistingTransactionId'
26
     *          ]
27
     * @return  static
28
     */
29
    protected function massAssignAttributes($data = [])
30
    {
31
        if ( ! is_array($data)) {
32
            return $this;
33
        }
34
35
        foreach ($data as $key => $value) {
36
            if ( method_exists($this, 'set' . ucfirst($key))
37
                // && in_array($key, array_merge($this->parametersRequired, $this->parametersOptional))
38
            ) {
39
                $this->{'set' . $key}($value);
40
            }else{
41
                $this->$key = $value;
42
            }
43
        }
44
45
        return $this;
46
    }
47
    
48
    /**
49
     * Assign static variables in a dynamic way
50
     *
51
     * @param array $data
52
     * @return void
53
     */
54
    // public static function massAssignStaticAttributes($data = []) 
55
    // {
56
    //     if (is_array($data)) {
57
    //         foreach ($data as $key => $value) {
58
    //             if (method_exists($this, 'set' . $key)
59
    //                 && in_array($key, array_merge($this->parametersRequired, $this->parametersOptional))
60
    //             ) {
61
    //                 self::$key = $value; 
62
    //             }
63
    //         }
64
    //     }
65
 
66
    // }
67
}
68