2017 © Pedro Peláez
 

library lib-reflection

Reflection helper library of the Ride framework

image

ride/lib-reflection

Reflection helper library of the Ride framework

  • Friday, February 17, 2017
  • by ride-user
  • Repository
  • 7 Watchers
  • 0 Stars
  • 3,813 Installations
  • PHP
  • 21 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 17 Versions
  • 2 % Grown

The README.md

Ride: Reflection Library

Reflection helper library of the PHP Ride framework., (*1)

What's In This Library

Boolean

A helper to obtain a boolean value from various string formats:, (*2)

<?php

use ride\library\reflection\Boolean;

$bool = Boolean::getBoolean('yes'); // true
$bool = Boolean::getBoolean('off'); // false

Invoker

An interface to invoke dynamic callbacks. It can be used by eg an event manager, a controller dispatcher, ..., (*3)

The ReflectionHelper class implements this interface to offer a generic implementation out of the box., (*4)

ReflectionHelper

The reflection helper offers an easy interface for dynamic programming:, (*5)

  • It obtains the arguments of any callback as a named array
  • It creates objects or data containers with named arguments.
  • It gets and sets values from and to generic data containers. These data containers can be arrays or object instances.

Check the following code sample:, (*6)

<?php

use ride\library\reflection\ReflectionHelper;

$reflectionHelper = new ReflectionHelper();

// create an object
$date = $reflectionHelper->createObject('DateTime', array('time' => '6 July 1983'));

// create an object for a specific interface
$decorator = $reflectionHelper->createObject('ride\\library\\reflection\\ReflectionHelper', null, 'ride\\library\\reflection\\Invoker');

// get and set properties
$data = array();

$reflectionHelper->setProperty($data, 'property', '1');
$reflectionHelper->setProperty($data, 'sub[property]', '2');
// $data = array(
//     'property' => '1'
//     'sub' => array(
//         'property' => '2',
//     ),
// );

$result = $reflectionHelper->getProperty($data, 'property'); // 1
$result = $reflectionHelper->getProperty($data, 'sub[property]'); // 2
$result = $reflectionHelper->getProperty($data, 'sub[unexistant]'); // null
$result = $reflectionHelper->getProperty($data, 'sub[unexistant]', 'default'); // default

// what if we work with objects
$data = new DateTime();

// will call $data->setTimestamp('value');
$reflectionHelper->setProperty($data, 'timestamp', time()); 

// will set $data->unexistant to 'value'
$reflectionHelper->setProperty($data, 'unexistant', 'value'); 

// will check $data->getUnexistant2() and $data->unexistant2 before return 'default'
$result = $reflectionHelper->getProperty($data, 'unexistant2', 'default'); 

// retrieve callback arguments
$arguments = $reflectionHelper->getArguments('strpos');
$arguments = $reflectionHelper->getArguments('safeString', 'ride\library\String');
$arguments = $reflectionHelper->getArguments('safeString', new ride\library\String());
$arguments = $reflectionHelper->getArguments(array($data, 'safeString');
// $arguments = array(
//     'replacement' => ReflectionParameter[...],
//     'lower' => ReflectionParameter[...],
// );

// invoke a callback
$callback = array($reflectionHelper, 'createObject');
$arguments = array(
    'arguments' => array('time' => 'now'),
    'class' => 'DateTime',
);

$date = $reflectionHelper->invoke($callback, $arguments);

Sorter

Implementation to sort data containers, being arrays or objects, on different properties simultaneously., (*7)

<?php

use ride\library\reflection\ReflectionHelper;
use ride\library\reflection\Sorter;

// generate some data containers
$entry1 = array(
    'name' => 'John',
    'age' => 21,
);
$entry2 = array(
    'name' => 'Jane',
    'age' => 18,
);
$entry3 = array(
    'name' => 'Mark',
    'age' => 30,
);
$entry4 = array(
    'name' => 'Tom',
    'age' => 21,
);

$entries = array(
    $entry1,
    $entry2,
    $entry3,
    $entry4,
);

// create a sorter age ASC, name ASC
$sorter = new Sorter(new ReflectionHelper(), array('age' => true, 'name' => true));

// sort the entries
print_r($sorter->sort($entries));
/*
array(
    1 => array(
        'name' => 'Jane',
        'age' => 18,
    ),
    0 => array(
        'name' => 'John',
        'age' => 21,
    ),
    3 => array(
        'name' => 'Tom',
        'age' => 21,
    ),
    2 => array(
        'name' => 'Mark',
        'age' => 30,
    ),
);
*/

Installation

You can use Composer to install this library., (*8)

composer require ride/lib-reflection

The Versions

17/02 2017

dev-master

9999999-dev

Reflection helper library of the Ride framework

  Sources   Download

MIT

by Joris Vandeweerd

17/02 2017

dev-develop

dev-develop

Reflection helper library of the Ride framework

  Sources   Download

MIT

by Joris Vandeweerd

17/02 2017

1.0.2

1.0.2.0

Reflection helper library of the Ride framework

  Sources   Download

MIT

by Joris Vandeweerd

08/09 2016

1.0.1

1.0.1.0

Reflection helper library of the Ride framework

  Sources   Download

MIT

by Joris Vandeweerd

16/06 2016

1.0.0

1.0.0.0

Reflection helper library of the Ride framework

  Sources   Download

MIT

by Joris Vandeweerd

05/01 2016

0.6.1

0.6.1.0

Reflection helper library of the Ride framework

  Sources   Download

MIT

by Joris Vandeweerd

04/01 2016

0.6.0

0.6.0.0

Reflection helper library of the Ride framework

  Sources   Download

MIT

by Joris Vandeweerd

23/04 2015

0.5.0

0.5.0.0

Reflection helper library of the Ride framework

  Sources   Download

MIT

by Joris Vandeweerd

01/03 2015

0.4.5

0.4.5.0

Reflection helper library of the Ride framework

  Sources   Download

MIT

by Joris Vandeweerd

08/08 2014

0.4.4

0.4.4.0

Reflection helper library of the Ride framework

  Sources   Download

MIT

by Joris Vandeweerd

13/05 2014

0.4.3

0.4.3.0

Reflection helper library of the Ride framework

  Sources   Download

MIT

by Joris Vandeweerd

01/04 2014

0.4.2

0.4.2.0

Reflection helper library of the Ride framework

  Sources   Download

MIT

by Joris Vandeweerd

14/02 2014

0.4.1

0.4.1.0

Reflection helper library of the Pallo framework

  Sources   Download

MIT

by Joris Vandeweerd

04/11 2013

0.4.0

0.4.0.0

Reflection helper library of the Pallo framework

  Sources   Download

MIT

by Joris Vandeweerd

28/09 2013

0.3.0

0.3.0.0

Reflection helper library of the Pallo framework

  Sources   Download

MIT

by Joris Vandeweerd

27/09 2013

0.2.0

0.2.0.0

Reflection helper library of the Pallo framework

  Sources   Download

MIT

by Joris Vandeweerd

21/09 2013

0.1.0

0.1.0.0

Reflection helper library of the Pallo framework

  Sources   Download

MIT

by Joris Vandeweerd