$knot

Modern Array collection and more...

View on GitHub

Factories

Is Helpers

Structure

Main

Install

Knot can easily install with composer.

                            
composer require knot/knot
                            
                        

Simple Usage

There are some examples.

                            
$knot = ar();
// $knot equals knot which data is empty array

$knot->foo = 'some value';
// 'foo' key which value is 'some value' inserted to $knot's data.

$knot->merge([1, 2, 3])->reverse();
// $knot's data merged with [1, 2, 3] then reversed.
                            
                        

Relations

Knot objects has relations like father and child. If functions result is a piece of knot data which is an array, then result will be a child of main object. Child Data will be referenced to father's data with PHP References.

                            
$parent = arr(['child'=>[]]);

$child = $parent->child;

$child->sub = 'new value';
// $father's data changed too.
                            
                        

Child object has path value. This path is helpful when child need to be killed.

                            
$child->path();
// 'child'
                            
                        
Copy function can use for crate knot without relations.
                            
$newKnot = $parent->child->copy();
                            
                        

Factories

Factory methods for create Knot objects.

ar([$variable], ...)

Create Knot object with arguments.

                            
$knot = ar(1, 2, 3);

$knot = ar();
                            
                        

arr(array $array)

Create Knot object with array.

                            
$knot = arr([1, 2, 3]);
                            
                        

arrRef(array $array)

Create Knot object with array reference.

                            
$array = [1, 2, 3];

$knot = arrRef($array);
                            
                        

Is Helpers

Is helper functions for knot.

is_dict(mixed $instance)

This function checks Knot Dict instances.

Functions

Base Knot functions.

$knot->toArray()

Return Knot array data. And also can returns references of Knot Data.

                            
$array = [1, 2, 3];

$knot = arr($array);

$knot->toArray();
// [1, 2, 3]

$foo =& $knot->Array();
// returns $knot array references.
                            
                        

$knot->get(string path, [$default])

Return Knot data which is target of path. If path target is empty, default variable is added to path with set function.

If target is array, then get returns knot child object.

                            
$array = [
    'one' => [
        'two' => 'value'
    ]
];

$knot = arr($array);

$knot->get('one.two');
// return 'value'

$knot->get('new.value', 'new value');
// return 'new value'

$knot->get('new.value');
// return 'new value' again
                            
                        

$knot->getOnly(string path, [default])

Similar to get. However knot data is not changed with default value if this path is empty. If path target is empty and there is no default value then throw error.

$knot->set(string path, value);

Set Knot data by path.

                            
$knot = ar();

$knot->set('one.two', 'value');
                            
                        

$knot->del(string path)

Delete Knot data by path.

                            
$array = [
    'one' => [
        'two' => 'value'
    ]
];

$knot = arr($array);

$knot->del('one.two');

$knot->toArray();
// return []
                            
                        

$knot->isPath(string path)

Returns true if path target is not empty.

$knot->kill()

Knot data changed with empty array.

$knot->path()

If Knot is child, returns path by father knot.

$knot->copy()

Create a new Knot without any relations if knot has a father knot.

$knot->count([$countRecursive = false])

Count of Knot's data. If $countRecursive then returns count of total keys.

$knot->call(string $callableVariableName, [...arguments])

If there is a callable variable in Knot's data, it can executed by call function.

                            
$knot = ar();

$knot["simple"] = function($word) {
    return "Hi " . $word;
}

$result = $knot->call("simple", "world!");
// returns "Hi world!"
                            
                        

$knot->parent()

If Knot object is child, then child object will return parent of Knot object.

Helpers

Helpers are a very powerful way to include new functions to Knot. There are standard helpers for base PHP Array Functions.

PHPArrayChangerHelper

This helpers include functions which PHP Array functions change array content with references and returns their results or not returns new array. And there can be called without array_ prefix and with camel case. PHPArrayChangerHelper is return array function's results.

There are witch PHPArrayChangerHelper contains

  • array_multisort
  • array_pop
  • array_product
  • array_push
  • array_rand
  • array_reduce
  • array_shift
  • array_splice
  • array_sum
  • array_unshift
  • array_walk_recursive
  • array_walk

Example for PHPArrayChangerHelper functions

                            
$knot = ar(1, 2, 3);

$sum = $knot->sum();
// returns 6
                            
                        

PHPArrayEqualizerHelper

This helpers includes functions to Knot which PHP Array functions return a new array. And this PHP functions can be called without array_ prefix and with camel case. For example array_merge_recursive function will be called mergeRecursive PHPArrayEqualHelper is return knot object for recursive usage.

There are PHPArrayEqualizerHelper functions

  • array_column
  • array_count_values
  • array_change_key_case
  • array_chunk
  • array_combine
  • array_diff_assoc
  • array_diff_key
  • array_diff_uassoc
  • array_diff_ukey
  • array_diff
  • array_fill_keys
  • array_filter
  • array_flip
  • array_intersect_assoc
  • array_intersect_key
  • array_intersect_uassoc
  • array_intersect_ukey
  • array_intersect
  • array_keys
  • array_merge_recursive
  • array_merge
  • array_pad
  • array_reverse
  • array_replace_recursive
  • array_replace
  • array_slice
  • array_udiff_assoc
  • array_udiff_uassoc
  • array_udiff
  • array_uintersect_assoc
  • array_uintersect_uassoc
  • array_uintersect
  • array_unique
  • array_values

Example for PHPArrayEqualizerHelper functions

                            
$knot = ar(1,2,3);

$knot->merge([4, 5])->filter(function($var) {
    return $var % 2;
});

$knot->toArray();
// return odd numbers [1, 3, 5]
                            
                        

UnderscoreHelper

Underscore.php is similar underscore.js. UnderscoreHelper is a way for user Underscores functions with Knot. For UnderscoreHelper activate, underscore.php needs to added.

                            
composer require anahkiasen/underscore-php
                            
                        

Interfaces

Knot object's interfaces.

ArrayAccess

Knot can be used like normal array for get or set data.

                            
$knot = ar(1, 2, 3);
$knot[] = 4;
// Knot added 4 to last of knot's data.

$knot[0];
// 1

$knot[1] = -1;
// sets knot data's to -1 where key is 1

$ref =&  $knot[0];
$ref['foo'] = 'ok!';

$knot['foo'];
// result is 'ok!'
                            
                        

Countable

Knot can be use with count() function.

WARNING: When count($knot, COUNT_RECURESIVE) is used, COUNT_RECURESIVE can't be used. Here it is some information about that. For COUNT_RECURESIVE, try count functions.

                            
$knot = ar(1, 2, 3);

count($knot);
// returns 3
                            
                        

IteratorAggregate

Knot can be used with foreach.

                            
$knot = ar(1, 2, 3);

foreach($knot as $key => $value)
{
    echo $key . '=>' . $value;
}
                            
                        

Structure

Structure for Knot.

API

Visit documentation for Knot's api.

assembled with