Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
12 / 12
CRAP
100.00% covered (success)
100.00%
36 / 36
HasExtendedEvents
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
12 / 12
17
100.00% covered (success)
100.00%
36 / 36
 getAttributeListeners
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
7 / 7
 setAttribute
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
5 / 5
 getAttribute
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
5 / 5
 registerAttributeEvent
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 fireAttributeEvent
100.00% covered (success)
100.00%
1 / 1
4
100.00% covered (success)
100.00%
4 / 4
 initializing
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 initialized
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 settingAttribute
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 didSetAttribute
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 gettingAttribute
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 didGetAttribute
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
1<?php
2
3namespace Helium\Illuminate2\Database\Eloquent\Concerns;
4
5use Illuminate\Database\Eloquent\Model;
6
7/**
8 * @mixin Model
9 */
10trait HasExtendedEvents
11{
12    //Base
13    /**
14     * Registered listeners for attribute events.
15     *
16     * @var array
17     */
18    protected static array $attributeListeners = [];
19
20    /**
21     * Get listeners for attribute events.
22     *
23     * @param string $event
24     * @return array|mixed
25     */
26    public function getAttributeListeners(string $event)
27    {
28        return static::$attributeListeners[$event] ?? [];
29    }
30    //endregion
31
32    //region Overrides
33    /**
34     * Create a new Eloquent model instance.
35     *
36     * @param  array  $attributes
37     * @return void
38     */
39    public function __construct(array $attributes = [])
40    {
41        $this->bootIfNotBooted();
42
43        $this->fireModelEvent('initializing', false);
44
45        $this->initializeTraits();
46
47        $this->syncOriginal();
48
49        $this->fill($attributes);
50
51        $this->fireModelEvent('initialized', false);
52    }
53
54    /**
55     * Set a given attribute on the model.
56     *
57     * @param  string  $key
58     * @param  mixed  $value
59     * @return mixed
60     */
61    public function setAttribute($key, $value)
62    {
63        if ($this->fireAttributeEvent('setting_attribute', [$key, $value]) === false) {
64            return false;
65        }
66
67        $response = parent::setAttribute($key, $value);
68
69        $this->fireAttributeEvent('did_set_attribute', [$key, $value], false);
70
71        return $response;
72    }
73
74    /**
75     * Get an attribute from the model.
76     *
77     * @param  string  $key
78     * @return mixed
79     */
80    public function getAttribute($key)
81    {
82        if ($this->fireAttributeEvent('getting_attribute', [$key]) === false) {
83            return false;
84        }
85
86        $value = parent::getAttribute($key);
87
88        $this->fireAttributeEvent('did_get_attribute', [$key, $value], false);
89
90        return $value;
91    }
92    //endregion
93
94    //region Events
95    /**
96     * Register an attribute event.
97     *
98     * @param string $event
99     * @param $callback
100     */
101    public static function registerAttributeEvent(string $event, $callback)
102    {
103        static::$attributeListeners[$event][] = $callback;
104    }
105
106    /**
107     * Fire an attribute event.
108     *
109     * @param string $event
110     * @param array $args
111     * @param bool $halt
112     * @return bool
113     */
114    public function fireAttributeEvent(string $event, array $args = [], bool $halt = true)
115    {
116        foreach ($this->getAttributeListeners($event) as $listener) {
117            if(call_user_func_array($listener, [$this, ...$args]) === false && $halt) {
118                return false;
119            }
120        }
121
122        return true;
123    }
124
125    /**
126     * Register an initializing model event with the dispatcher.
127     *
128     * @param  \Closure|string  $callback
129     * @return void
130     */
131    public static function initializing($callback)
132    {
133        static::registerModelEvent('initializing', $callback);
134    }
135
136    /**
137     * Register an initialized model event with the dispatcher.
138     *
139     * @param  \Closure|string  $callback
140     * @return void
141     */
142    public static function initialized($callback)
143    {
144        static::registerModelEvent('initialized', $callback);
145    }
146
147    /**
148     * Register an setting attribute event with the dispatcher.
149     *
150     * @param  \Closure|string  $callback
151     * @return void
152     */
153    public static function settingAttribute($callback)
154    {
155        static::registerAttributeEvent('setting_attribute', $callback);
156    }
157
158    /**
159     * Register an did set attribute event with the dispatcher.
160     *
161     * @param  \Closure|string  $callback
162     * @return void
163     */
164    public static function didSetAttribute($callback)
165    {
166        static::registerAttributeEvent('did_set_attribute', $callback);
167    }
168
169    /**
170     * Register an getting attribute event with the dispatcher.
171     *
172     * @param  \Closure|string  $callback
173     * @return void
174     */
175    public static function gettingAttribute($callback)
176    {
177        static::registerAttributeEvent('getting_attribute', $callback);
178    }
179
180    /**
181     * Register a did get attribute event with the dispatcher.
182     *
183     * @param  \Closure|string  $callback
184     * @return void
185     */
186    public static function didGetAttribute($callback)
187    {
188        static::registerAttributeEvent('did_get_attribute', $callback);
189    }
190    //endregion
191}