Mask.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. namespace Webklex\PHPIMAP\Support\Masks;
  13. use Illuminate\Support\Str;
  14. use Webklex\PHPIMAP\Exceptions\MethodNotFoundException;
  15. /**
  16. * Class Mask
  17. *
  18. * @package Webklex\PHPIMAP\Support\Masks
  19. */
  20. class Mask {
  21. /**
  22. * Available attributes
  23. *
  24. * @var array $attributes
  25. */
  26. protected $attributes = [];
  27. /**
  28. * Parent instance
  29. *
  30. * @var object $parent
  31. */
  32. protected $parent;
  33. /**
  34. * Mask constructor.
  35. * @param $parent
  36. */
  37. public function __construct($parent) {
  38. $this->parent = $parent;
  39. if(method_exists($this->parent, 'getAttributes')){
  40. $this->attributes = array_merge($this->attributes, $this->parent->getAttributes());
  41. }
  42. $this->boot();
  43. }
  44. /**
  45. * Boot method made to be used by any custom mask
  46. */
  47. protected function boot(){}
  48. /**
  49. * Call dynamic attribute setter and getter methods and inherit the parent calls
  50. * @param string $method
  51. * @param array $arguments
  52. *
  53. * @return mixed
  54. * @throws MethodNotFoundException
  55. */
  56. public function __call($method, $arguments) {
  57. if(strtolower(substr($method, 0, 3)) === 'get') {
  58. $name = Str::snake(substr($method, 3));
  59. if(isset($this->attributes[$name])) {
  60. return $this->attributes[$name];
  61. }
  62. }elseif (strtolower(substr($method, 0, 3)) === 'set') {
  63. $name = Str::snake(substr($method, 3));
  64. if(isset($this->attributes[$name])) {
  65. $this->attributes[$name] = array_pop($arguments);
  66. return $this->attributes[$name];
  67. }
  68. }
  69. if(method_exists($this->parent, $method) === true){
  70. return call_user_func_array([$this->parent, $method], $arguments);
  71. }
  72. throw new MethodNotFoundException("Method ".self::class.'::'.$method.'() is not supported');
  73. }
  74. /**
  75. * Magic setter
  76. * @param $name
  77. * @param $value
  78. *
  79. * @return mixed
  80. */
  81. public function __set($name, $value) {
  82. $this->attributes[$name] = $value;
  83. return $this->attributes[$name];
  84. }
  85. /**
  86. * Magic getter
  87. * @param $name
  88. *
  89. * @return mixed|null
  90. */
  91. public function __get($name) {
  92. if(isset($this->attributes[$name])) {
  93. return $this->attributes[$name];
  94. }
  95. return null;
  96. }
  97. /**
  98. * Get the parent instance
  99. *
  100. * @return mixed
  101. */
  102. public function getParent(){
  103. return $this->parent;
  104. }
  105. /**
  106. * Get all available attributes
  107. *
  108. * @return array
  109. */
  110. public function getAttributes(){
  111. return $this->attributes;
  112. }
  113. }