lfbn / base-model
This is a Base Model that can be extended to define Models. It helps handling data validation, and extracting data.
Installs: 5
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/lfbn/base-model
Requires
- php: ^7.1
- monolog/monolog: ^1.23
Requires (Dev)
- jakub-onderka/php-console-highlighter: ^0.3.2
- jakub-onderka/php-parallel-lint: ^0.9.2
- jakub-onderka/php-var-dump-check: ^0.2.0
- mockery/mockery: ^1.0
- phpunit/php-code-coverage: ^6.0
- phpunit/phpunit: ^7.0
- squizlabs/php_codesniffer: ^3.2
This package is not auto-updated.
Last update: 2025-10-26 11:34:15 UTC
README
This is a Base Model that can be extended to define Models. It helps handling data validation, and data conversion.
Features
- Validation of properties. All, except the isNotEmpty, doesn't validate the data if it is empty. The following validator are available:
- isNotEmpty
- isNumeric
- isInteger
- isFloat
- isString
- isBoolean
- isTrue
- isFalse
- isNull
- isNotNull
- isArray
- isEmail
- Model to array and JSON, preserving hidden attributes.
- Define attributes using arrays of data.
- Can define, when validation fails, if an exception is thrown.
- Can define if data should be validated or not.
Installation
composer require lfbn/base-model
Examples
How to use it
Define your model extending the AbstractBaseModel, then implement a public method getValidationRules. This method should define the properties you want to validate.
Here is an example:
class User extends AbstractBaseModel { /** * @var int */ protected $id; /** * @var string */ protected $name; /** * @var float */ protected $height; /** * @var boolean */ protected $active; /** * @return int */ public function getId(): int { return $this->id; } /** * @param int $id */ public function setId(int $id): void { $this->id = $id; } /** * @return string */ public function getName(): string { return $this->name; } /** * @param string $name */ public function setName(string $name): void { $this->name = $name; } /** * @return float */ public function getHeight(): float { return $this->height; } /** * @param float $height */ public function setHeight(float $height): void { $this->height = $height; } /** * @return bool */ public function isActive(): bool { return $this->active; } /** * @param bool $active */ public function setActive(bool $active): void { $this->active = $active; } /** * @return array */ public function getValidationRules() { return [ ['property' => 'id', 'validator' => 'isNotEmpty'], ['property' => 'id', 'validator' => 'isInteger'], ['property' => 'height', 'validator' => 'isFloat'], ['property' => 'active', 'validator' => 'isBoolean'] ]; } }
How to know if is valid?
You need to call the validate() method
$user = new User(); $user->validate();
Can I use my own validator?
Yes. It only needs to implement the interface IValidator.
$user = new User(); $myValidator = new MyValidator(); $user->setValidator($myValidator);
Can I use my own converter?
Yes. It only needs to implement the interface IConverter.
$user = new User(); $myConverter = new MyConverter(); $user->setConverter($myConverter);
About
Requirements
- Base Model works with PHP 7 or above.
Running Tests
You can run the tests executing the following command. Before you can run these, be sure to run composer install.
composer test
Submitting bugs and feature requests
Bugs and feature request are tracked on GitHub
License
Base Model is licensed under the MIT License - see the LICENSE file for details