GithubHelp home page GithubHelp logo

telkins / attributes Goto Github PK

View Code? Open in Web Editor NEW

This project forked from wilmoore/attributes.php

1.0 1.0 0.0 336 KB

PHP object attributes without the setter, getter boilerplate

License: MIT License

PHP 100.00%

attributes's Introduction

Attributes Trait for PHP

Build Status

A minimal trait to decrease getter/setter boilerplate.

Features

  • Omit setter/getter methods until needed.
  • JSON or Array representation of object attributes.
  • Get values via $object->firstName or $object->get('firstName')
  • Set values via $object->firstName = 'My Name'; or $object->set('firstName', 'My Name')
  • isset, empty, and unset work as expected.
  • Define acceptable input values like 'seconds' => ['accepts' => '0..59']
  • Define default values like 'score' => ['default' => 0]

Anti-Features

  • Leaning on complex IDEs to produce setter/getter cruft is not a good solution to the underlying problem.
  • Leaning on an ORM is not a good solution since not every object in your domain needs to be persisted.
  • Leaning on reflection-based meta-programming.

Examples

class Game {
  use Attributes;

  protected $__attributes = [
    'gameName'  => [],
    'userName'  => [],
    'score'     => ['accepts' => '0..100']
  ];
}

$game = new Game;
$game->set([
  'gameName' => 'pacman',
  'userName' => 'manny.pacquiao',
  'score'    => 95
]);

assert(95 === $game->score);

Installation

Composer

"require": {
    "wilmoore/attributes.php": "*"
}

Requirements

  • PHP 5.4+
  • [optional] PHPUnit 3.6+ to execute the test suite (phpunit --version)

Resources

Changelog

  • (0.0.2) 20120726: Added Travis Integration.
  • (0.0.1) 20120726: Initial Usable Release.

LICENSE

MIT

attributes's People

Contributors

wilmoore avatar

Stargazers

 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.