Code Monkey home page Code Monkey logo

has-attributes's Introduction

Has Attributes Trait

A trait to give class attributes

Features

  • Give class attributes with array.
  • Define attributes strict type.

Quick Example

Just use HasAttributes trait and pass key value array to constructor

class Post
{
    use \Clouding\HasAttributes\HasAttributes;
}

$post = new Post([
    'title' => 'Hello',
    'body' => 'World',
]);

echo $post->title; // Hello
echo $post->body;  // World

Installation

composer require clouding/has-attributes

Usage

Define type

Declare $define to define type

require 'vendor/autoload.php';

interface Eatable {}

class Pig implements Eatable { }

class Zoo
{
    use \Clouding\HasAttributes\HasAttributes;

    protected $define = [
        'name' => 'string',
        'number' => 'int',
        'animal' => Eatable::class,
    ];
}

$zoo = new Zoo([
    'name' => 'Mike',
    'number' => 100,
    'animal' => new Pig(),
]);

echo $zoo->name;              // Mike
echo $zoo->number;            // 100
echo get_class($zoo->animal); // Pig

If you declare $define property, it will check type strictly

new Zoo(['name' => 999]);

// InvalidArgumentException: [name => 999] value is not equal to define type [string] 

And can't set key that is not defined

new Zoo(['foo' => 'bar']);

// InvalidArgumentException: Key [foo] is not defined 

Supported $define type:

  • string
  • int, integer
  • bool, boolean
  • object
  • array
  • real, float, double
  • Class, Interface

Set attributes

There have many way to set attributes

class Person
{
    use \Clouding\HasAttributes\HasAttributes;
}

$person = new Person([
    'id' => 100,
]);

$person->setAttributes([
    'name' => 'Marry',
    'phone' => '0912345678'
]);

$person->setAttribute('sex', 'female');

$person->age = 18;

echo $person->id;    // 100
echo $person->name;  // Marry
echo $person->phone; // 0912345678
echo $person->sex;   // female
echo $person->age;   // 18

Get attributes

class Person
{
    use \Clouding\HasAttributes\HasAttributes;
}

$person = new Person([
    'id' => 100,
    'name' => 'Jack',
]);

echo $person->getAttribute('id');                 // 100

echo $person->getAttribute('salary', 0);          // 0 (if key not exists return default value)

var_dump($person->getAttributes('id', 'name'));   // ['id' => 100, 'name' => 'Jack']

var_dump($person->getAttributes(['id', 'name'])); // ['id' => 100, 'name' => 'Jack']

has-attributes's People

Contributors

cloudingcity avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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