Giter VIP home page Giter VIP logo

domainobject's Introduction

DomainObject

Build Status Scrutinizer Code Quality

This is a simple class that signifies classes that extend it are a DomainObject. In a more practical sense it offers a [properties](http://en.wikipedia.org/wiki/Property_(programming\)) implementation, that is lacking from PHP.

Why

We believe in a plain old php objects (POPO) for modelling your domain. These objects should hold no logic other than their core values. A DomainObject should have a direct link to an entity in your Universe of Discourse.

This class helps you do that in a generic way. It has some niceties such as support for accessing your properties through both functions and object property notation. But mostly, it is a strong signal that the class that's extending it is, in fact, a DomainObject.

Why not simply rely on public variables?

Using simple variables like:

class Person
{
    public $name;
}

Works pretty well for most simple properties.

Imagine the following though:

class Person
{
    public $firstName;

    public $lastName;

    public function getFullName()
    {
        return $this->firstName . ' ' . $this->lastName;
    }
}

In order to get the full name for a person (first + last), you need to write a method. Now you have to mix both properties and methods in your API. This is not very consistent and rather inflexible.

Example

<?php

use Angrybytes\DomainObject;

use \InvalidArgumentException;

class BlogPost extends DomainObject
{
    private $title;

    private $contents;

    public function getTitle()
    {
        return $this->title;
    }

    public function setTitle($title)
    {
        // You can do simple sanity checks in your setters
        if (strlen($title) < 3) {
            throw new InvalidArgumentException('Title should be 3 or more characters long');
        }

        $this->title = $title;

        return $this;
    }

    public function getContents()
    {
        return $this->contents;
    }

    public function setContents($contents)
    {
        $this->contents = $contents;

        return $this;
    }
}

Using this you can:

<?php

$post = new BlogPost;

// Set properties
$post
    ->setTitle('This is the title for my blog post')
    ->setContents('foo');

// Retrieve properties using property notation
echo $post->title;
echo $post->contents;

// Retrieve data in array form for easy serialization
$json = json_encode(
    $post->toArray()
);

domainobject's People

Contributors

naneau avatar ivarb avatar

Stargazers

primoz klemensek avatar Cameron Cox avatar

Watchers

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