| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
namespace Webklex\PHPIMAP\Support\Masks; |
| 14 |
|
| 15 |
use Illuminate\Support\Str; |
| 16 |
use Webklex\PHPIMAP\Exceptions\MethodNotFoundException; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
class Mask { |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
protected array $attributes = []; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
protected mixed $parent; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public function __construct($parent) { |
| 44 |
$this->parent = $parent; |
| 45 |
|
| 46 |
if(method_exists($this->parent, 'getAttributes')){ |
| 47 |
$this->attributes = array_merge($this->attributes, $this->parent->getAttributes()); |
| 48 |
} |
| 49 |
|
| 50 |
$this->boot(); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
protected function boot(): void {} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public function __call(string $method, array $arguments) { |
| 67 |
if(strtolower(substr($method, 0, 3)) === 'get') { |
| 68 |
$name = Str::snake(substr($method, 3)); |
| 69 |
|
| 70 |
if(isset($this->attributes[$name])) { |
| 71 |
return $this->attributes[$name]; |
| 72 |
} |
| 73 |
|
| 74 |
}elseif (strtolower(substr($method, 0, 3)) === 'set') { |
| 75 |
$name = Str::snake(substr($method, 3)); |
| 76 |
|
| 77 |
if(isset($this->attributes[$name])) { |
| 78 |
$this->attributes[$name] = array_pop($arguments); |
| 79 |
|
| 80 |
return $this->attributes[$name]; |
| 81 |
} |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
if(method_exists($this->parent, $method) === true){ |
| 86 |
return call_user_func_array([$this->parent, $method], $arguments); |
| 87 |
} |
| 88 |
|
| 89 |
throw new MethodNotFoundException("Method ".self::class.'::'.$method.'() is not supported'); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public function __set($name, $value) { |
| 100 |
$this->attributes[$name] = $value; |
| 101 |
|
| 102 |
return $this->attributes[$name]; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public function __get($name) { |
| 112 |
if(isset($this->attributes[$name])) { |
| 113 |
return $this->attributes[$name]; |
| 114 |
} |
| 115 |
|
| 116 |
return null; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public function getParent(): mixed { |
| 125 |
return $this->parent; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
public function getAttributes(): array { |
| 134 |
return $this->attributes; |
| 135 |
} |
| 136 |
|
| 137 |
} |