takemo101 / simple-vm
Simple ViewModel Object
Installs: 1 065
Dependents: 1
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^8.0
Requires (Dev)
- php: ^8.0
- phpstan/phpstan: ^1.4
- phpunit/phpunit: ^9.5
README
The Simple VM allows you to simply create a ViewModel object.
By using the ViewModel object, you can convey appropriate data to the View.
Enjoy!
How to use
Please use as follows
Basic
use Takemo101\SimpleVM\ViewModel; /** * Create a class that inherits from the ViewModel class */ class TestViewModel extends ViewModel { // Set methods and property names that are not reflected in this property protected array $__ignores = [ 'd', ]; /** * constructor * * @param string $a * @param integer $b */ public function __construct( public string $a, private int $b, ) { // public property is reflected in View } /** * public method is reflected in View * * @return string */ public function c(): string { return 'C'; } /** * @return string */ public function d(): string { return 'D'; } /** * Set the initial value to the __data method * * @return mixed[] */ public function __data(): array { return [ 'e' => 'E', ]; } } $model = new TestViewModel('A', 2); var_dump($model->toArray()); // array(3) { // 'e' => // string(1) "E" // 'a' => // string(1) "A" // 'c' => // string(1) "C" // }
PHP Attribute
You can use the PHP Attribute class.
use Takemo101\SimpleVM\ViewModel; use Takemo101\SimpleVM\Attribute\{ Ignore, ChangeName, }; /** * Create a class that inherits from the ViewModel class */ class TestAttributeViewModel extends ViewModel { /** * Not output when Ignore class is set in a property or method * * @param string $a */ public function __construct( #[Ignore] public string $a, ) { // public property is reflected in View } /** * Setting the ChangeName class on a property or method will rename the data * * @return string */ #[ChangeName('cc')] public function c(): string { return 'C'; } } $model = new TestAttributeViewModel('A'); var_dump($model->toArray()); // array(3) { // 'cc' => // string(1) "C" // }