2017 © Pedro Peláez
 

library common

Some classes commonly used in other projects.

image

bluepsyduck/common

Some classes commonly used in other projects.

  • Saturday, July 14, 2018
  • by BluePsyduck
  • Repository
  • 1 Watchers
  • 1 Stars
  • 340 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 106 % Grown

The README.md

BluePsyduck's Common Library

Latest Stable Version Total Downloads License Build Status codecov, (*1)

This library contains some classes commonly used in other projects., (*2)

DataBuilder

The data builder is a class helping with building up an array with different values, ensuring that the values have the correct type. With all methods a default value can be specified which should be ignored and not added to the data array., (*3)

The data builder provides several methods for automatically casting the set values:, (*4)

  • setBoolean(): Sets a boolean value.
  • setInteger(): Sets an integer number.
  • setFloat(): Sets a floating point number.
  • setString(): Sets a string value.
  • setDateTime(): Sets a date and time value in the specified format. If the passed value is not a DateTime instance, the value gets ignored automatically.
  • setArray(): Sets an array of value, optionally with a mapping method to e.g. cast each value.
Example
<?php
use BluePsyduck\Common\Data\DataBuilder;

$dataBuilder = new DataBuilder();

// Set some values
$dataBuilder->setInteger('foo', 42)
            ->setString('bar', 'baz');

// Set a variable value unless it is a specific one
$age1 = 21;
$age2 = -1;
$dataBuilder->setInteger('age1', $age1, -1)
            ->setInteger('age2', $age2, -1); // $age2 will get ignored.

// Set a date and time value
$date = new DateTime('2038-01-19 03:14:07');
$dataBuilder->setDateTime('creationTime', $date, 'Y-m-d'); // Will set '2038-01-17' as value.

// Set an array of strings
$dataBuilder->setArray('data', ['foo', 'bar', 42], 'strval'); // Casts all array values to a string.

DataContainer

The data container wraps around a deep array and is able to access and map the elements of the array without the need to check if any level key exists. All methods provide a default value to fall back to when a key is not defined., (*5)

The data container provides several methods to automatically map the data:, (*6)

  • getBoolean(): Reads a boolean value.
  • getInteger(): Reads an integer number.
  • getFloat(): Reads a floating point number.
  • getString(): Reads a string value.
  • getDateTime(): Reads a date and time value. An integer value will be interpreted as Unix timestamp, a string value as date string.
  • getArray(): Reads an array value.
  • getObject(): Reads the value and returns a new DataContainer instance with that value.
  • getObjectArray(): Reads an array value and casts every item to a new instance of DataContainer.
Example
<?php
use BluePsyduck\Common\Data\DataContainer;

$data = [
    'foo' => 'bar',
    'first' => [
        'second' => 42
    ]
];
$dataContainer = new DataContainer($data);

// Accessing the first level
echo $dataContainer->getString('foo'); // 'bar'

// Accessing the second level
echo $dataContainer->getObject('first')->getInteger('second'); // 42
echo $dataContainer->getInteger(['first', 'second']); // 42

// Default values for undefined keys
echo $dataContainer->getFloat('missing', 13.37); // 13.37 

ReflectionTrait

This trait provides some methods to easily access non-public properties and invoke non-public methods of classes to be used in tests., (*7)

  • injectProperty(): Injects a value into the specified property.
  • extractProperty(): Extracts the value of the specified property.
  • invokeMethod(): Invokes the specified method and returns its result value.

The Versions

14/07 2018

dev-master

9999999-dev https://github.com/BluePsyduck/Common

Some classes commonly used in other projects.

  Sources   Download

GPL-3.0-or-later

The Requires

  • php >=7.0.0

 

The Development Requires

by Avatar BluePsyduck

common bluepsyduck

17/06 2018

1.0.1

1.0.1.0 https://github.com/BluePsyduck/Common

Some classes commonly used in other projects.

  Sources   Download

GPL-3.0-or-later

The Requires

  • php >=7.0.0

 

The Development Requires

by Avatar BluePsyduck

common bluepsyduck

16/02 2018

1.0.0

1.0.0.0 https://github.com/BluePsyduck/Common

Some classes commonly used in other projects.

  Sources   Download

GPL-3.0-or-later

The Requires

  • php >=7.0.0

 

The Development Requires

by Avatar BluePsyduck

common bluepsyduck