Download the PHP package grithin/record without Composer
On this page you can find all versions of the php package grithin/record. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download grithin/record
More information about grithin/record
Files in grithin/record
Package record
Short Description Managing inclusion of web assets
License Apache-2.0
Homepage http://clrmo.com
Informations about the package record
PHP Record
An arbitrarily deep array that tracks changes, emits events,
Intended to be an observable that matches a database record, allowing the handling of a record like an array, keeping track of changes, handling deep data structures, and allowing listeners to react to change events.
(Similar to SplSubject, but because SplSubject uses pointless SplObserver, SplSubject is not implemented)
Use
Basic
Tracking arbitrarily deep structures
Getter, Setter, And Events
Some additional highlights from the above.
On the after_get function unjsonify, $Record->record['children'] is used to avoid triggering more events. If you don't mind before_change and after_change being run, $Record['children'] could have been used.
When setting the record in the manner $Record['name'] = 'bob', each assignment triggers two events. It may be preferable to do a bulk change, which allows multiple things to change and will only emit two events. This can be done with both the local copy and the source
Overview
Events, Observers
Types:
- EVENT_AFTER_GET : immediately after getter function is called. This can be used to decode JSON
- EVENT_CHANGE_BEFORE
- EVENT_CHANGE_AFTER
- EVENT_UPDATE_BEFORE : before updating the source. This can be used to re-encode JSON
- EVENT_UPDATE_AFTER
Convenience functions after_get, before_change, after_change, before_update, after_update will call the parameter function on the corresponding event with parameters ($this, $details), where in $details is an array object of the change.
(EVENT_UPDATE_AFTER runs after EVENT_AFTER_GET since it may require the data to be reformatted by EVENT_AFTER_GET)
Update and Change events will fire only if there were changes.
The diff parameter presented to event observers is an ArrayObject, and that object is used when applying the diff to the record. Consequently, mutating the diff within a "before" event observer will affect the resulting record. If the diff count becomes 0, no change will be applied.
Potential uses of observers:
- mutate a particular column (timestamp to datetime format)
- check validity of record state. Throw an exception if a bad state, or clear the diff
- create a composite column. Ex: total_cost = cost1 + cost2
- logging specific changes