Coder Social home page Coder Social logo

flex-array's Introduction

Introduction

Make your array usage quite easy and fast.


FlexArray. Get started.

You have to provide you array to constructor and after that you can do a lot of operations on haystack via class methods.

// Some array to be processed.
$array = [
    [0, 1, 2, 3, 5],
    'fruits' => ['apple', 'orange'],
    'persons' => [
        'john_doe' => [
            'name' => 'John Doe',
            'cars' => ['bmw', 'audi'],
            'isActive' => true, 
            'comment' => 'some description',   
        ],
        'mike_shepard' => [
            'name' => 'Mike Shepard',
            'cars' => [],
            'isActive' => false,
            'comment' => null
        ],
    ]
];

// Making FlexArray.
$array = new FlexArray($array);

Available methods

Getters

Setters

Modifiers

Factories

Predicates


Describing methods

Getters


get

get(int|string $key, mixed $default = null): mixed|null

Returns value from haystack by $key or $default (null on default) on not set.

getInteger

getInteger(int|string $key, mixed $default = null): ?int

Returns value from haystack by $key cast to integer or $default (null on default) on not set.

getFloat

getFloat(int|string $key, mixed $default = null): ?float

Returns value from haystack by $key cast to float or $default (null on default) on not set.

getString

getString(int|string $key, mixed $default = null): ?string

Returns value from haystack by $key cast to string or $default (null on default) on not set.

getBoolean

getBoolean(int|string $key, mixed $default = null): ?bool

Returns value from haystack by $key cast to boolean or $default (null on default) on not set.

getAny

getAny(int|string ...$keys): mixed|null

Returns value first found by $keys or null on not found.

getAll

getAll(): array

Returns haystack as array.

getKeys

getKeys(): array

Return keys of the haystack.

getAllBut

getAllBut(int|string ...$keys): array

Returns list of haystack values but provided in $keys.

getAllIntegers

getAllIntegers(): array

Returns list of integer values indexed by its keys.

getAllStrings

getAllStrings(): array

Returns list of string values indexed by its keys.

getAllBooleans

getAllBooleans(): array

Returns list of boolean values indexed by its keys.

getAllCleaned

getAllClean(): array

Returns the haystack of not real empty values.

getFirst

getFirst(): mixed

Returns the first element.

getFirstKey

getFirstKey(): int|string|null

Returns first key of the haystack or null on empty haystack.

getLast

getLast(): mixed

Returns the last element.

getLastKey

getLastKey(): int|string|null

Returns last key of the haystack or null on empty haystack.

getByIndex

getByIndex(int $index): mixed

Returns value by index or nul if $index is not in haystack length. Pass negative number for reverse search.

getKeyOfIndex

getKeyOfIndex(int $index): int|string|null

Returns key of integer index in the haystack or nul if $index is not in haystack length. Pass negative number for reverse search.

getUpTo

getUpTo(int $index): array

Return values up to $index. Pass negative number for reverse search.

implodeAll

implodeAll(string $separator, bool $associative = false): string

Returns array values imploded to string recursively. Pass true in $associative to represent in indexed by its keys.

implode

implode(string $separator, bool $associative = false): string

Returns only scalar values in the haystack as string. Pass true in $associative to represent in indexed by its keys.

implodeKeys

implodeKeys(string $separator): string

Returns haystack keys as string.

count

count(): int

Returns the haystack length.

keyOf

keyOf(int|string $key): int|string|null

Returns key of value in the haystack or null on found.

keysOf

keysOf(int|string $key): array

Returns list of keys for haystack values.

indexOf

indexOf(mixed $needle): int|null

Returns integer index of value or null on not found.

indexesOf

indexesOf(mixed $needle): array

Returns list of integer indexes of provided values.

binarySearch

binarySearch(int $needle): int|null

Binary search method for integer value in haystack. Sorts the haystack due the process. Returns the integer index of value on found or null on not.

toJson

toJson(): string

Represents haystack in JSON string content. Trows Exception on decode error.

touch

touch(int|string ...$keys): array

Returns only scalar values in the haystack as string. Pass true in $associative to represent in indexed by its keys.

findValues

findValues(mixed ...$values): array

Returns list of found values associated with found keys in haystack.

Setters


set

set(int|string $key, mixed $value): self

Sets value by $key.

prepend

prepend(mixed $value): self

Adds value to haystack to the top of haystack.

append

append(mixed $value): self

Adds value to haystack in the end.

delete

delete(int|string $key): self

Deletes value by $key.

deleteOnFound

deleteOnFound(mixed ...$values): self

Remove haystack elements by values if exists.

deleteFirst

deleteFirst(): self

Deletes the first element.

deleteLast

deleteLast(): self

Deletes the last element.

deleteByIndex

deleteByIndex(int|string $key): self

Deletes element by index.

deleteAll

deleteAll(): self

Delete all values and sets empty array to haystack.

Modifiers


flip

flip(): self

Flips the haystack.

merge

merge(array ...$arrays): self

Merges the elements of one or more arrays together.

unique

unique(int $flags = SORT_STRING): self

Returns haystack keys as string.

Available flags

  • SORT_REGULAR - compare items normally (don't change types)
  • SORT_NUMERIC - compare items numerically
  • SORT_STRING - compare items as strings
  • SORT_LOCALE_STRING - compare items as strings, based on the current locale.

sort

sort(int $flags = SORT_REGULAR): self

Sort haystack.

Available flags

  • SORT_REGULAR - compare items normally; the details are described in the comparison operators section
  • SORT_NUMERIC - compare items numerically
  • SORT_STRING - compare items as strings
  • SORT_LOCALE_STRING - compare items as strings, based on the current locale. It uses the locale, which can be changed using setlocale()
  • SORT_NATURAL - compare items as strings using "natural ordering" like natsort()
  • SORT_FLAG_CASE - can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively

rsort

rsort(int $flags = SORT_REGULAR): self

Sort haystack in reverse order.

Available flags

  • SORT_REGULAR - compare items normally; the details are described in the comparison operators section
  • SORT_NUMERIC - compare items numerically
  • SORT_STRING - compare items as strings
  • SORT_LOCALE_STRING - compare items as strings, based on the current locale. It uses the locale, which can be changed using setlocale()
  • SORT_NATURAL - compare items as strings using "natural ordering" like natsort()
  • SORT_FLAG_CASE - can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively

ksort

ksort(int $flags = SORT_REGULAR): self

Sort the haystack by keys.

Available flags

  • SORT_REGULAR - compare items normally; the details are described in the comparison operators section
  • SORT_NUMERIC - compare items numerically
  • SORT_STRING - compare items as strings
  • SORT_LOCALE_STRING - compare items as strings, based on the current locale. It uses the locale, which can be changed using setlocale()
  • SORT_NATURAL - compare items as strings using "natural ordering" like natsort()
  • SORT_FLAG_CASE - can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively

krsort

krsort(int $flags = SORT_REGULAR): self

Sort the haystack by keys in reverse order.

Available flags

  • SORT_REGULAR - compare items normally; the details are described in the comparison operators section
  • SORT_NUMERIC - compare items numerically
  • SORT_STRING - compare items as strings
  • SORT_LOCALE_STRING - compare items as strings, based on the current locale. It uses the locale, which can be changed using setlocale()
  • SORT_NATURAL - compare items as strings using "natural ordering" like natsort()
  • SORT_FLAG_CASE - can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively

asort

asort(int $flags = SORT_REGULAR): self

Sort an array and maintain index association.

Available flags

  • SORT_REGULAR - compare items normally; the details are described in the comparison operators section
  • SORT_NUMERIC - compare items numerically
  • SORT_STRING - compare items as strings
  • SORT_LOCALE_STRING - compare items as strings, based on the current locale. It uses the locale, which can be changed using setlocale()
  • SORT_NATURAL - compare items as strings using "natural ordering" like natsort()
  • SORT_FLAG_CASE - can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively

arsort

arsort(int $flags = SORT_REGULAR): self

Sort an array and maintain index association in reverse order.

Available flags

  • SORT_REGULAR - compare items normally; the details are described in the comparison operators section
  • SORT_NUMERIC - compare items numerically
  • SORT_STRING - compare items as strings
  • SORT_LOCALE_STRING - compare items as strings, based on the current locale. It uses the locale, which can be changed using setlocale()
  • SORT_NATURAL - compare items as strings using "natural ordering" like natsort()
  • SORT_FLAG_CASE - can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively

clean

clean(): self

Cleans real empty values in the haystack.

Factories


createBy

createBy(int|string $key): self

Creates and returns inner collection (new static()) by $key if indexed value in the haystack is array. Otherwise, collection with empty haystack returns. Now, inner array is available to processed with all described methods.

createByFirst

createByFirst(): self

Creates class object by first element.

createByLast

createByLast(): self

Creates class object by last element.

createByIndex

createByIndex(): self

Creates class object by element located by index. Pass negative number to get it in reverse order.

Predicates


isEmpty

isEmpty(int|string $key): bool

Defines if value by $key exists and is not empty

keyExists

keyExists(int|string $key): bool

Defines if the given key or index exists in the haystack even nullable.

hasKeys

hasKeys(mixed ...$keys): bool

Defines if all provided keys exists.

hasAnyKey

hasAnyKey(mixed ...$keys): bool

Defines if the haystack has any value (even nullable) indexed by keys provided.

hasValues

hasValues(mixed ...$values): bool

Defines if the haystack has any value provided.

hasAnyValue

hasAnyValue(mixed ...$values): bool

Defines if the haystack has any value provided.

inCount

inCount(int ...$count): bool

Defines if the haystack length in provided value.

assertEqualsByKey

assertEqualsByKey(int|string $key, mixed ...$values): bool

Asserts if each value identically equals the value in the haystack by key.

assertAnyEqualsByKey

assertAnyEqualsByKey(int|string $key, mixed ...$values): bool

Asserts if any value identically equals the value in the haystack by key.

flex-array's People

Contributors

sitnikovik avatar

Watchers

 avatar

flex-array's Issues

New features

add:

  • checking of non-empty fields by one method or by list

Отрефачить flex-array

Сделать версию для PHP 7.3, переписать тесты из скрипта на PHPUnit и просто пройтись по коду и обновить если нужно

Add stack iterator method

Add the method to iterate all stack items and implement provided callback (2nd arg for example) to each item

Add new methods

  • getInteger() - returns value cast to integer on found by key
  • getFloat() - returns value cast to float on found by key
  • getString() - returns value cast to string on found by key
  • getBoolean() - returns value cast to boolean on found by key
  • getAny() - returns value first matched by list of keys provided
  • countByKey() - counts array elems quantity keyed by

Add type maps

It is need to implement type maps that can store only one type data.
For the first it can implement scalar type such as integer, float, string, bool.
When this library has type list implementation you may use it to implement for the next version.

Map implementation

Add map implementation with string keys and mixed values.
under the hood it should look like dynamic array with only string keys.
This data structure will be useful to implement some cache actual in runtime.

Update readme

Since there are map and type list implemented the readme.md is not updated

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.