GithubHelp home page GithubHelp logo

dagpro / arrays Goto Github PK

View Code? Open in Web Editor NEW

This project forked from yiisoft/arrays

0.0 0.0 0.0 277 KB

Yii Array Helper

Home Page: https://www.yiiframework.com/

License: BSD 3-Clause "New" or "Revised" License

PHP 100.00%

arrays's Introduction

Yii Arrays


Latest Stable Version Total Downloads Build status Scrutinizer Code Quality Code Coverage Mutation testing badge static analysis type-coverage

The package provides:

  • ArrayHelper that has static methods to work with arrays;
  • ArraySorter that has static methods for sort arrays;
  • ArrayAccessTrait provides the implementation for \IteratorAggregate, \ArrayAccess and \Countable;
  • ArrayableInterface and ArrayableTrait for use in classes who want to support customizable representation of their instances.

Requirements

  • PHP 7.4 or higher.

Installation

composer require yiisoft/arrays --prefer-dist

ArrayHelper usage

Array helper methods are static so usage is like the following:

$username = \Yiisoft\Arrays\ArrayHelper::getValue($_POST, 'username');

Overall the helper has the following method groups.

Getting data

  • getValue
  • getValueByPath
  • getColumn
  • getObjectVars

Setting data

  • setValue
  • setValueByPath

Removing data

  • remove
  • removeByPath
  • removeValue

Detecting array types

  • isIndexed
  • isAssociative

HTML encoding and decoding values

  • htmlDecode
  • htmlEncode

Testing against arrays

  • isIn
  • isSubset

Transformation

  • index
  • group
  • filter
  • map
  • merge
  • toArray

Other

  • keyExists
  • pathExists

ArraySorter usage

Array sorter has one static method which usage is like the following:

\Yiisoft\Arrays\ArraySorter::multisort($data, ['age', 'name'], [SORT_ASC, SORT_DESC]);

ArrayAccessTrait usage

ArrayAccessTrait provides the implementation for \IteratorAggregate, \ArrayAccess and \Countable.

Note that ArrayAccessTrait requires the class using it contain a property named data which should be an array. The data will be exposed by ArrayAccessTrait to support accessing the class object like an array.

Example of use:

use \Yiisoft\Arrays\ArrayAccessTrait;

class OfficeClassification implements \IteratorAggregate, \ArrayAccess, \Countable
{
    use ArrayAccessTrait;

    public array $data = [
        'a' => 'Class A',
        'b' => 'Class B',
        'c' => 'Class C',
    ];
}

$classification = new OfficeClassification();

echo 'Count classes: ' . $classification->count() . "\n"; // 3

$iterator = $classification->getIterator();
while ($iterator->valid()) {
    echo $iterator->current() . "\n"; // Class A, Class B, Class C
    $iterator->next();
}

ArrayableInterface and ArrayableTrait usage

ArrayableInterface and its implementation ArrayableTrait intended for use in classes who want to support customizable representation of their instances.

Example of use:

use \Yiisoft\Arrays\ArrayableTrait;
use \Yiisoft\Arrays\ArrayableInterface;

class Car implements ArrayableInterface
{
    use ArrayableTrait;

    public string $type = 'Crossover';
    public string $color = 'Red';
    public int $torque = 472;
}

$car = new Car();

$data = $car->toArray(['type', 'color']); // ['type' => 'Crossover', 'color' => 'Red']

Testing

Unit testing

The package is tested with PHPUnit. To run tests:

./vendor/bin/phpunit

Mutation testing

The package tests are checked with Infection mutation framework with Infection Static Analysis Plugin. To run it:

./vendor/bin/roave-infection-static-analysis-plugin

Static analysis

The code is statically analyzed with Psalm. To run static analysis:

./vendor/bin/psalm

License

The Yii Arrays is free software. It is released under the terms of the BSD License. Please see LICENSE for more information.

Maintained by Yii Software.

Support the project

Open Collective

Follow updates

Official website Twitter Telegram Facebook Slack

arrays's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.