2017 © Pedro Peláez
 

library object-array-tools

A full featured array behavior Abstract class for using in php libraries

image

ivir3zam/object-array-tools

A full featured array behavior Abstract class for using in php libraries

  • Tuesday, January 3, 2017
  • by IVIR3zaM
  • Repository
  • 1 Watchers
  • 1 Stars
  • 112 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 7 Versions
  • 2 % Grown

The README.md

ObjectArrayTools

Build Status Code Climate Issue Count Test Coverage, (*1)

this library helps you to have an object as an active array with hooks for updating, inserting, deleting, sanitizing and filtering elements., (*2)

Technical Details

this library is different from builtin ArrayObject class in PHP, and this deference is that this library is based on traits so you can use it in your classes that already extending some other classes and other differences is about hooks that help you to implement Observer Pattern for your ObjectArray, and also you have many methods equivalents to php native Array Functions, (*3)

Installation

The recommended way to install ObjectArrayTools is through Composer., (*4)

# Install Composer
curl -sS https://getcomposer.org/installer | php

Next, run the Composer command to install the latest stable version of Guzzle:, (*5)

php composer.phar require ivir3zam/object-array-tools

After installing, you need to require Composer's autoloader:, (*6)

require 'vendor/autoload.php';

You can then later update ObjectArrayTools using composer:, (*7)

bash composer.phar update, (*8)

Examples

this library helps you to use your objects as an storage and it don't affect your current object functionality, (*9)

Basic Array Usage

this example only show you the basic usage of this library, (*10)

use IVIR3aM\ObjectArrayTools\AbstractActiveArray;

class ActiveArray extends AbstractActiveArray {}

$object = new ActiveArray(['Hello', 'World']);

echo 'Number of elements: ' . count($object) . "\n";

foreach ($object as $string) {
    echo $string  . ' ';
}

echo "\n";

$object[1] = 'Visitor';

foreach ($object as $string) {
    echo $string  . ' ';
}

echo "\n";

/*
OUTPUT:

Number of elements: 2
Hello World
Hello Visitor

/*

Save input in database and show output in browser

in this example we try to have an array that when you write data in it, filter input for database and when you read from it, sanitize for html output, (*11)

use IVIR3aM\ObjectArrayTools\AbstractActiveArray;

class DatabaseRecord extends AbstractActiveArray
{
    protected function sanitizeInputHook($offset, $data)
    {
        // some strong operation of sanitizing $data needed, this is only a sample
        return mysqli_real_escape_string($link, $data);
    }

    protected function sanitizeOutputHook($offset, $data)
    {
        return htmlspecialchars($data);
    }

    protected function updateHook($offset, $data, $oldData)
    {
        // some strong operation of updating database process needed, this is only a sample
        mysqli_query($link, "UPDATE SomeTable SET `Data` = '{$data}' WHERE `ID` = " . intval($offset));
    }

    protected function removeHook($offset, $oldData)
    {
        // some strong operation of updating database process needed, this is only a sample
        mysqli_query($link, "DELETE FROM SomeTable WHERE `ID` = " . intval($offset));
    }

    protected function insertHook($offset, $data)
    {
        mysqli_query($link, "INSERT INTO SomeTable SET `ID` = " . intval($offset) . ", `Data` = '{$data}'");
    }
}

$db = new DatabaseRecord();
$db[1] = "Lorem Ipsum <some>script</some>'; DELETE FROM SomeTable";
// mysqli_query($link, "INSERT INTO SomeTable SET `ID` = 1, `Data` = 'Lorem Ipsum <some>script</some>\'; DELETE FROM SomeTable'");

echo $db[1];
// Lorem Ipsum &lt;some&gt;script&lt;/some&gt;'; DELETE FROM SomeTable

Sorting an array

you can see sorting functionality of this library here. there is a complete list of supported functions in AbstractActiveArray class documentation, (*12)

use IVIR3aM\ObjectArrayTools\AbstractActiveArray;

class ActiveArray extends AbstractActiveArray {}

$object = new ActiveArray(['How', 'Are', 'You']);

print_r($object->getData());
/*
Array
(
    [0] => How
    [1] => Are
    [2] => You
)
*/

$object->sort();

print_r($object->getData());
/*
Array
(
    [0] => Are
    [1] => How
    [2] => You
)
*/

The Versions

03/01 2017

dev-master

9999999-dev https://ivir3zam.github.com/

A full featured array behavior Abstract class for using in php libraries

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Mohammad Reza Maghoul

array-access iterator-object observer-array active-array

03/01 2017

1.1.2

1.1.2.0 https://ivir3zam.github.com/

A full featured array behavior Abstract class for using in php libraries

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Mohammad Reza Maghoul

array-access iterator-object observer-array active-array

24/12 2016

1.1.1

1.1.1.0 https://ivir3zam.github.com/

A full featured array behavior Abstract class for using in php libraries

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Mohammad Reza Maghoul

array-access iterator-object observer-array active-array

24/12 2016

1.1

1.1.0.0 https://ivir3zam.github.com/

A full featured array behavior Abstract class for using in php libraries

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Mohammad Reza Maghoul

array-access iterator-object observer-array active-array

23/12 2016

1.0

1.0.0.0 https://ivir3zam.github.com/

A full featured array behavior Abstract class for using in php libraries

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Mohammad Reza Maghoul

array-access iterator-object observer-array active-array

21/12 2016

0.9.1

0.9.1.0 https://ivir3zam.github.com/

A full featured array behavior Abstract class for using in php libraries

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Mohammad Reza Maghoul

array-access iterator-object observer-array active-array

21/12 2016

0.9

0.9.0.0 https://ivir3zam.github.com/

A full featured array behavior Abstract class for using in php libraries

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Mohammad Reza Maghoul

array-access iterator-object observer-array active-array