Giter VIP home page Giter VIP logo

getset's Introduction

GetSet

Author Latest Version Software License Build Status

Introduction

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

Requirements

Composer

GetSet requires the following:

  • "php": ">=5.6.0"

And the following if you wish to run in dev mode and run tests.

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

Installation

Composer

Simplest installation is via composer.

composer require snscripts/getset 1.*

or adding to your projects composer.json file.

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

Setup

To initiate GetSet simply use the trait within your class

class MyClass
{
    use \Snscripts\GetSet\GetSet;

    ...
}

Usage

Once setup, simply set or get any variables on the object you need.

$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.

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.

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

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.

$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.

toArray

$MyClass->toArray();

This will simply return all data set onto the object as an array.

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.

Changelog

You can view the changelog HERE

Contributing

Please see CONTRIBUTING for details.

License

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

getset's People

Contributors

mikebarlow avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.