2017 © Pedro Peláez
 

library getset

Trait for adding magic setters / getters to objects

image

snscripts/getset

Trait for adding magic setters / getters to objects

  • Saturday, June 24, 2017
  • by snsmurf
  • Repository
  • 1 Watchers
  • 0 Stars
  • 938 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 65 % Grown

The README.md

GetSet

Author Latest Version Software License Build Status, (*1)

Introduction

GetSet is a PSR-2 compliant trait to be used for adding magic getters / setters to objects., (*2)

Requirements

Composer

GetSet requires the following:, (*3)

  • "php": ">=5.6.0"

And the following if you wish to run in dev mode and run tests., (*4)

  • "phpunit/phpunit": "~5.7"
  • "squizlabs/php_codesniffer": "~2.0"

Installation

Composer

Simplest installation is via composer., (*5)

composer require snscripts/getset 1.*

or adding to your projects composer.json file., (*6)

{
    "require": {
        "snscripts/getset": "1.*"
    }
}

Setup

To initiate GetSet simply use the trait within your class, (*7)

class MyClass
{
    use \Snscripts\GetSet\GetSet;

    ...
}

Usage

Once setup, simply set or get any variables on the object you need., (*8)

$MyClass->foo_bar = 'barfoo';

echo $MyClass->foo_bar; // barfoo

This will set the variable into a $data array added to your object via the GetSet trait., (*9)

Variable Transformers

You can create custom get / set transformers to change a variables data as it gets added to or retrieved from the $data variable. These should be in the format setXAttr and getXAttr where X is a CamelCased version of the variable you are setting or getting from the object., (*10)

In the example above, basic getter / setter methods for foo_bar could look like:, (*11)

public function setFooBarAttr($value)
{
    $this->data['foo_bar'] = strtoupper($value);
}

public function getFooBarAttr()
{
    return strtolower($this->data['foo_bar']);
}

Mass data assignment

If you need to assign a full array of data to your object and wish for the variables to be passed through any custom setters there is the setAllData method., (*12)

$MyClass->setAllData([
    'foo_bar' => 'barfoo'
]);

Data Export

If you need to export all the data set within your object you can use 1 of 2 methods., (*13)

toArray

$MyClass->toArray();

This will simply return all data set onto the object as an array., (*14)

toJson

$MyClass->toJson();

This will return all the data set onto the object as a Json object. This first uses the toArray method to retrieve an array before returning the data through json_encode., (*15)

Changelog

You can view the changelog HERE, (*16)

Contributing

Please see CONTRIBUTING for details., (*17)

License

The MIT License (MIT). Please see License File for more information., (*18)

The Versions

24/06 2017

dev-master

9999999-dev https://github.com/mikebarlow/GetSet

Trait for adding magic setters / getters to objects

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

by Mike Barlow

trait getters setters object properties

24/06 2017

1.0.0

1.0.0.0 https://github.com/mikebarlow/GetSet

Trait for adding magic setters / getters to objects

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

by Mike Barlow

trait getters setters object properties