2017 © Pedro PelĂĄez
 

library base-model

This is a Base Model that can be extended to define Models. It helps handling data validation, and extracting data.

image

lfbn/base-model

This is a Base Model that can be extended to define Models. It helps handling data validation, and extracting data.

  • Tuesday, February 27, 2018
  • by lfbn
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 0 % Grown

The README.md

About Base Model

This is a Base Model that can be extended to define Models. It helps handling data validation, and data conversion., (*1)

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., (*2)

Here is an example:, (*3)

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, (*4)

$user = new User();
$user->validate();

Can I use my own validator?

Yes. It only needs to implement the interface IValidator., (*5)

$user = new User();
$myValidator = new MyValidator();
$user->setValidator($myValidator);

Can I use my own converter?

Yes. It only needs to implement the interface IConverter., (*6)

$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., (*7)

composer test

Submitting bugs and feature requests

Bugs and feature request are tracked on GitHub, (*8)

License

Base Model is licensed under the MIT License - see the LICENSE file for details, (*9)

The Versions

27/02 2018

dev-master

9999999-dev https://github.com/lfbn/base-model

This is a Base Model that can be extended to define Models. It helps handling data validation, and extracting data.

  Sources   Download

MIT

The Requires

 

The Development Requires

by LuĂ­s NĂłbrega

validation model

26/02 2018

1.0.5

1.0.5.0 https://github.com/lfbn/base-model

This is a Base Model that can be extended to define Models. It helps handling data validation, and extracting data.

  Sources   Download

MIT

The Requires

 

The Development Requires

by LuĂ­s NĂłbrega

validation model

22/02 2018

1.0.4

1.0.4.0 https://github.com/lfbn/base-model

This is a Base Model that can be extended to define Models. It helps handling data validation, and data conversion.

  Sources   Download

MIT

The Requires

 

The Development Requires

by LuĂ­s NĂłbrega

validation model

20/02 2018

1.0.3

1.0.3.0 https://github.com/lfbn/base-model

This is a Base Model that can be extended to define Models. It helps handling data validation, and data conversion.

  Sources   Download

MIT

The Requires

 

The Development Requires

by LuĂ­s NĂłbrega

validation model

13/02 2018

1.0.0

1.0.0.0

Base Model

  Sources   Download

MIT

The Requires

 

The Development Requires

by LuĂ­s NĂłbrega

validation

13/02 2018

dev-improvement/add-changelog-better-readme

dev-improvement/add-changelog-better-readme

Base Model

  Sources   Download

MIT

The Requires

 

The Development Requires

by LuĂ­s NĂłbrega

validation