wilmoore/attributes.php

minimal object attributes for PHP.

0.1.4 2014-03-30 16:51 UTC

This package is not auto-updated.

Last update: 2024-04-13 12:59:05 UTC


README

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