Code Monkey home page Code Monkey logo

typedphp's Introduction

TypedPhp

Immutable Object representation for PHP primitive types and Typed Arrays in PHP.

Build Status Code Coverage Scrutinizer Code Quality

Motivations

In PHP versions 5.5 and 5.6 type hinting is supported for classes and arrays, but not for scalar types. In PHP version 7, type hinting for scalar types is supported, but not typed arrays. So I created object wrappers for scalar types that can be used in PHP 5 and also typed arrays that can be used in PHP7. These objects can be extended to create your own ValueObjects suitable for your domain.

Installation

Composer

"require": {
    "mcustiel/typed-php": "*"
}

How to use

All wrappers implement Primitive interface:

interface Primitive
{
    /**
     * @return mixed
     */
    public function value();
}

Also all of them return themselves as string, through implementation of __toString magic method. So you can echo them directly.

Primitives

  • DoubleValue
  • IntegerValue
  • StringValue
  • BooleanValue

Arrays:

There are two base types:

  • PrimitivesArray
  • ObjectsArray

PrimitivesArray

PrimitivesArray object allows a collection of variables an internal PHP type. And there are already three classes using it:

  • DoubleArray
  • IntegerArray
  • StringArray
  • BooleanArray

All of them are constructed with an array as argument. If there is a value in the array of a type that is not correct, an exception will be thrown.

ObjectsArray

This type allows a collection of instances of a given class. An example to create a type for this is as follow:

use Mcustiel\TypedPhp\ValueObjects\Multiple\ObjectsArray;

class Foo 
{
    private function bar()
    {
        echo 'I am Bar';
    }
}

class FooArray extends ObjectsArray
{
    public function __construct(array $array)
    {
        parent::__construct(Foo::class, $array);
    }
}

That's it, you have an array type that accepts only Foo objects. In your code you can use any FooArray object as a regular array.

If there are classes that extend Foo, FooArray will allow them, so polymorphism is supported.

Immutable arrays

For each array type provider there is an immutable version, that allows to be created with a set of values and is not allowed to change.

  • ImmutableDoubleArray
  • ImmutableIntegerArray
  • ImmutableStringArray
  • ImmutableBooleanArray

Flyweight implementation

A basic flyweight pattern implementation is provided. It will return the exact same value object on multiple calls:

use Mcustiel\TypedPhp\Values\IntegerCreator;

$a = IntegerCreator::instance()->getValueObject(5);
$b = IntegerCreator::instance()->getValueObject(5);

In this example $a and $b reference the exact same instance of IntegerValue.

typedphp's People

Contributors

mcustiel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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