\Phabstractic\Data\TypesTaggedUnion

Tagged Union Class - Defines A Dynamic Tagged Union

An instantiated TaggedUnion is a 'variable' that can hold only specifically defined data types, such as string, integer, resource, or object. One TaggedUnion for instance may only hold strings, and objects of a particular class. Code can detect what type a given TaggedUnion's value is at run time, thus enabling a form of 'data structure polymorphism'.

NOTE: since v3.0 restrictions aren't only configurable at construction

CHANGELOG

1.0: Created TaggedUnion - April 21st, 2013 Documented TaggedUnion - May 5th, 2013 1.1: public function getValue( $value ) -> getValue() - May 5th, 2013 2.0: Refactored and reformatted for Primus - April 11th, 2015 3.0: eliminated need for restrictions to only be defined at construction, reformatted for inclusion in phabstractic - July 18th, 2016

Summary

Methods
Properties
Constants
getRestrictions()
setRestrictions()
getValue()
getType()
__construct()
set()
get()
__invoke()
__debugInfo()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
No private methods found
$type
$restrictions
$value
N/A

Properties

$type

$type : \Phabstractic\Data\Types\Phabstractic\Data\Types\Type

The type of the data at any given moment.

Type

\Phabstractic\Data\Types\Phabstractic\Data\Types\Type

$restrictions

$restrictions : \Phabstractic\Data\Types\Phabstractic\Data\Types\Restrictions

The type restrictions placed on the tagged union

NOTE: v3.0 dynamic beyond being set at construction now

Type

\Phabstractic\Data\Types\Phabstractic\Data\Types\Restrictions

$value

$value : mixed

The actual value of the TaggedUnion at any given moment.

Type

mixed

Methods

getRestrictions()

getRestrictions() : \Phabstractic\Data\Types\Phabstractic\Data\Types\Restrictions

Get the restrictions

Returns the restrictions object that this tagged union is using

Returns

\Phabstractic\Data\Types\Phabstractic\Data\Types\Restrictions

setRestrictions()

setRestrictions(\Phabstractic\Data\Types\Resource\FilterInterface  $restrictions) 

Set the restrictions

Allows any type of object that has an isAllowed method

Parameters

\Phabstractic\Data\Types\Resource\FilterInterface $restrictions

getValue()

getValue() : mixed

Retrieve the actual value of the Tagged Union

Returns

mixed

getType()

getType() : \Phabstractic\Data\Types\Phabstractic\Data\Types\Type

Retrieve the type at the moment.

Returns

\Phabstractic\Data\Types\Phabstractic\Data\Types\Type

__construct()

__construct(\Phabstractic\Data\Types\Phabstractic\Data\Types\Resource\FilterInterface  $restrictions) 

Defines a TaggedUnion

OBSOLETE: This is the only place where you can define the data type restrictions of the tagged union.

Since v3 you can specify restrictions dynamically using setRestrictions

NOTE: $this->type will be of Type::BASIC_NULL until set regardless of restrictions.

Parameters

\Phabstractic\Data\Types\Phabstractic\Data\Types\Resource\FilterInterface $restrictions

The data restrictions

set()

set(mixed  $value) 

Sets the value, making sure it is allowed by the restrictions

Checks the value type of the given value, if it's allowed then sets both the type member and value member. Does not return true/false/null because those are values that may be passed. Throws errors instead if there's a problem setting the value.

Parameters

mixed $value

The value to set

Throws

\Phabstractic\Data\Types\Phabstractic\Data\Types\Exception\RuntimeException

if the given value is untypeable

\Phabstractic\Data\Types\Phabstractic\Data\Types\Exception\InvalidArgumentException

if the given value is not allowed

get()

get() : mixed

Get the value of the TaggedUnion

Returns

mixed —

The value of the TaggedUnion.

__invoke()

__invoke() : mixed

Shortcut to return value of Tagged Union

Returns

mixed

__debugInfo()

__debugInfo() 

Debug Info (var_dump)

Display debug info

Requires PHP 5.6+