ivir3zam/object-array-tools 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ivir3zam/object-array-tools

Composer 安装命令:

composer require ivir3zam/object-array-tools

包简介

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

README 文档

README

Build Status Code Climate Issue Count Test Coverage

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

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

Installation

The recommended way to install ObjectArrayTools is through Composer.

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

Next, run the Composer command to install the latest stable version of Guzzle:

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

After installing, you need to require Composer's autoloader:

require 'vendor/autoload.php';

You can then later update ObjectArrayTools using composer:

composer.phar update

Examples

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

Basic Array Usage

this example only show you the basic usage of this library

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

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

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
)
*/

统计信息

  • 总下载量: 159
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-12-21

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固