Passed
Push — master ( e02ad2...7e0890 )
by Malte
03:08
created

Mask::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 0
dl 0
loc 1
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
* File: Mask.php
4
* Category: Mask
5
* Author: M.Goldenbaum
6
* Created: 14.03.19 20:49
7
* Updated: -
8
*
9
* Description:
10
*  -
11
*/
12
13
namespace Webklex\IMAP\Support\Masks;
14
15
use Webklex\IMAP\Exceptions\MethodNotFoundException;
16
17
/**
18
 * Class Mask
19
 *
20
 * @package Webklex\IMAP\Support\Masks
21
 */
22
class Mask {
23
24
    /**
25
     * @var array $attributes
26
     */
27
    protected $attributes = [];
28
29
    /**
30
     * @var object $parent
31
     */
32
    protected $parent;
33
34
    /**
35
     * Mask constructor.
36
     * @param $parent
37
     */
38
    public function __construct($parent) {
39
        $this->parent = $parent;
40
41
        if(method_exists($this->parent, 'getAttributes')){
42
            $this->attributes = array_merge($this->attributes, $this->parent->getAttributes());
43
        }
44
45
        $this->boot();
46
    }
47
48
    /**
49
     * Boot method made to be used by any custom mask
50
     */
51
    protected function boot(){}
52
53
    /**
54
     * Call dynamic attribute setter and getter methods and inherit the parent calls
55
     * @param string $method
56
     * @param array $arguments
57
     *
58
     * @return mixed
59
     * @throws MethodNotFoundException
60
     */
61
    public function __call($method, $arguments) {
62
        if(strtolower(substr($method, 0, 3)) === 'get') {
63
            $name = snake_case(substr($method, 3));
0 ignored issues
show
Deprecated Code introduced by
The function snake_case() has been deprecated: Str::snake() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

63
            $name = /** @scrutinizer ignore-deprecated */ snake_case(substr($method, 3));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
64
65
            if(isset($this->attributes[$name])) {
66
                return $this->attributes[$name];
67
            }
68
69
        }elseif (strtolower(substr($method, 0, 3)) === 'set') {
70
            $name = snake_case(substr($method, 3));
0 ignored issues
show
Deprecated Code introduced by
The function snake_case() has been deprecated: Str::snake() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

70
            $name = /** @scrutinizer ignore-deprecated */ snake_case(substr($method, 3));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
71
72
            if(isset($this->attributes[$name])) {
73
                $this->attributes[$name] = array_pop($arguments);
74
75
                return $this->attributes[$name];
76
            }
77
78
        }
79
80
        if(method_exists($this->parent, $method) === true){
81
            return call_user_func_array([$this->parent, $method], $arguments);
82
        }
83
84
        throw new MethodNotFoundException("Method ".self::class.'::'.$method.'() is not supported');
85
    }
86
87
    /**
88
     * @param $name
89
     * @param $value
90
     *
91
     * @return mixed
92
     */
93
    public function __set($name, $value) {
94
        $this->attributes[$name] = $value;
95
96
        return $this->attributes[$name];
97
    }
98
99
    /**
100
     * @param $name
101
     *
102
     * @return mixed|null
103
     */
104
    public function __get($name) {
105
        if(isset($this->attributes[$name])) {
106
            return $this->attributes[$name];
107
        }
108
109
        return null;
110
    }
111
112
    /**
113
     * @return mixed
114
     */
115
    public function getParent(){
116
        return $this->parent;
117
    }
118
119
    /**
120
     * @return array
121
     */
122
    public function getAttributes(){
123
        return $this->attributes;
124
    }
125
126
}