Skip to content

Latest commit

 

History

History
118 lines (105 loc) · 5.86 KB

flat.md

File metadata and controls

118 lines (105 loc) · 5.86 KB

Back

Flat data types, using FlatTypeHandler

This handler provides the ability to map database fields through converters that convert to what the user needs, consider them as factories after find and before save.

Usage

use Saschati\ValueObject\Behaviors\ORMBehavior;
use Saschati\ValueObject\Types\Flats\BooleanType;
use Saschati\ValueObject\Types\Flats\TimestampType;
use Saschati\ValueObject\Helpers\TypeScope;
use Saschati\ValueObject\Scope\Handlers\FlatTypeHandler;
...

class User extends ActiveRecord
{
    ...
    public function behaviors(): array
    {
        return [
            'vo' => [
                'class' => ORMBehavior::class,
                'attributes' => [
                    'is_active'  => 'boolean', // BooleanType::class
                    'created_at' => [
                        'scope' => TypeScope::FLAT_TYPE, // FlatTypeHandler::class
                        'type' => TimestampType::class, // 'timestamp'
                        'reference' => 'created_at',
                    ],
                ],
            ],
        ];
    }
    ...
}

This handler will be applied if the key specification has a FlatInterface and "scope" is specified as FLAT_TYPE in the array.

List of FlatTypeHandler properties

name type description
type* ::class, alias Must be a valid type that extends FlatInterface
reference @attribute, property, #virtual The attribute or property from which to cast via FlatType, and to which the value will be transferred when saved.

List of existing FlatTypes

The list of available classes can be found at the link.

type description
boolean Convert database value in php boolean
integer Convert database value in php integer
float Convert database value in php float
json Convert database value as json in php assoc array
string Convert database value in php string
timestamp Convert database value as timestamp in php \DateTimeImmutable
timestamp:integer Convert database value as integer in php \DateTimeImmutable
date Convert database value as date in php \DateTimeImmutable
datetime Convert database value as datetime in php \DateTimeImmutable
serialized Convert database value as canned object in php object

Create custom FlatType

use Saschati\ValueObject\Types\Flats\Interfaces\FlatInterface;
use Saschati\ValueObject\Behaviors\ORMBehavior;

class Money implements SpecialInterface
{
    /**
     * @param integer $value
     *
     * @return float
     */
    public static function convertToPhpValue($value): float
    {
        return ($value / 100);
    }

    /**
     * @param integer $value
     *
     * @return integer
     */
    public static function convertToDatabaseValue($value): int
    {
        return ($value * 100);
    }
}

class User extends ActiveRecord
{

    ...
    public function behaviors()
    {
        return [
            'st' => [
                'class'      => SpecialTypeBehavior::class,
                'attributes' => [
                    'money' => Money::class,
                ],
            ],
        ];
    }
    ...
}

// In database money equal 980
$user = User::find()->one();

echo $user->money // 9.8