Download the PHP package tarsisioxavier/magic-object without Composer
On this page you can find all versions of the php package tarsisioxavier/magic-object. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download tarsisioxavier/magic-object
More information about tarsisioxavier/magic-object
Files in tarsisioxavier/magic-object
Package magic-object
Short Description Simple object inspired on Laravel's models
License MIT
Informations about the package magic-object
Installation ๐
Installation from composer.
Usage โ๏ธ
DataModel it's a abstract class, which means it cannot be instantiated. You'll need to create another class and extend from it.
Attributes
DataModel contains a property which holds all the attributes of the class - except the ones matching some class' property - making them accessable like the property exists within the class.
You may notice an "original" property in your objects, that's just to hold the attributes passed to the class' contructor.
This is usefull when you're encapsulating data comming from APIs to log some stuff and debug when needed.
Attributes Accessors
If you need to modify the way you access the object's attribute, you can write an accessor in the object extending the DataModel.
The example below creates a object with name and CNPJ. The accessor in the ExampleClass will try to return the CPF of the object, since it doesn't exists a CNPJ will be returned.
Attributes Mutators
When you want to modify the data before filling your object, there is a option to write a mutator to this specific(s) attribute(s).
The example below will receive a DateTime object and will transform it into a simple string carring only the year, month, day, the hour and the minutes from the value (parameter) informed.
Bootable Traits
You can run any code when your class extending from DataModel
is instantiated. Look the example below:
Notice that the method must match with the trait's name.
This is completly optional, if the method for booting it's not implemented the DataModel
class will just ignore it.
Testing ๐งช
Run all the tests executing composer test
, if you want a specific test to run, try: composer test -- --filter <the_test_you_want>
.
Composer's script 'test' uses the Pest PHP.
You can also check the code coverage by using the command composer coverage
, this will create a HTML file inside ./.coverage
directory.
There is also a code quality assurance by PHPStan, run composer phpstan
whenever you want to check that.
And last but not least, you can use the Pest Mutation feature by running composer test -- --mutate --parallel
.