piano/accessor

There is no license information available for the latest version (0.2) of this package.

A creator of getter and setter method by using annotations!

0.2 2016-05-30 16:16 UTC

This package is auto-updated.

Last update: 2024-03-28 10:13:31 UTC


README

Build Status Latest Stable Version

Piano Accessor

This package allows us to create getters and setters just by using a few annotations.

Installing

composer require piano/accessor

Usage example

See the example:

This User class:

<?php

namespace App;

class User
{
    private $name;
    private $age;
    private $createdAt;

    public function setName($name)
    {
        $this->name = $name;
    }

    public function setAge($age)
    {
        $this->age = (int) $age;
    }

    public function setCreatedAt(\DateTime $createdAt)
    {
        $this->createdAt = $createdAt;
    }

    public function getName()
    {
        return $this->name;
    }

    public function getAge()
    {
        return (int) $this->age;
    }

    public function getCreatedAt()
    {
        return $this->createdAt;
    }
}

Is the same as this User class:

<?php

namespace App;

class User
{
    use \Piano\AccessorTrait;

    /**
     * @set
     * @get
     */
    private $name;

    /**
     * @set int
     * @get int
     */
    private $age;

    /**
     * @set \DateTime
     * @get
     */
    private $createdAt;
}

As you can see it's possible to specify the type hint or type cast when defining the @set and it's also possible to specify the type cast when defining the @get. That's optional though.

As below:

Setting Getting
@set int @get int
@set integer @get integer
@set bool @get bool
@set boolean @get boolean
@set float @get float
@set double @get double
@set string @get string
@set array @get array
@set object @get object

For @set any other value will be treated as type hint.