forked from Webklex/laravel-imap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMask.php
126 lines (102 loc) · 2.57 KB
/
Mask.php
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
<?php
/*
* File: Mask.php
* Category: Mask
* Author: M.Goldenbaum
* Created: 14.03.19 20:49
* Updated: -
*
* Description:
* -
*/
namespace Webklex\IMAP\Support\Masks;
use Webklex\IMAP\Exceptions\MethodNotFoundException;
/**
* Class Mask
*
* @package Webklex\IMAP\Support\Masks
*/
class Mask {
/**
* @var array $attributes
*/
protected $attributes = [];
/**
* @var object $parent
*/
protected $parent;
/**
* Mask constructor.
* @param $parent
*/
public function __construct($parent) {
$this->parent = $parent;
if(method_exists($this->parent, 'getAttributes')){
$this->attributes = array_merge($this->attributes, $this->parent->getAttributes());
}
$this->boot();
}
/**
* Boot method made to be used by any custom mask
*/
protected function boot(){}
/**
* Call dynamic attribute setter and getter methods and inherit the parent calls
* @param string $method
* @param array $arguments
*
* @return mixed
* @throws MethodNotFoundException
*/
public function __call($method, $arguments) {
if(strtolower(substr($method, 0, 3)) === 'get') {
$name = snake_case(substr($method, 3));
if(isset($this->attributes[$name])) {
return $this->attributes[$name];
}
}elseif (strtolower(substr($method, 0, 3)) === 'set') {
$name = snake_case(substr($method, 3));
if(isset($this->attributes[$name])) {
$this->attributes[$name] = array_pop($arguments);
return $this->attributes[$name];
}
}
if(method_exists($this->parent, $method) === true){
return call_user_func_array([$this->parent, $method], $arguments);
}
throw new MethodNotFoundException("Method ".self::class.'::'.$method.'() is not supported');
}
/**
* @param $name
* @param $value
*
* @return mixed
*/
public function __set($name, $value) {
$this->attributes[$name] = $value;
return $this->attributes[$name];
}
/**
* @param $name
*
* @return mixed|null
*/
public function __get($name) {
if(isset($this->attributes[$name])) {
return $this->attributes[$name];
}
return null;
}
/**
* @return mixed
*/
public function getParent(){
return $this->parent;
}
/**
* @return array
*/
public function getAttributes(){
return $this->attributes;
}
}