Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

45 Commits

Repository files navigation

ArrayContainer

Build StatusSoftware LicensePackagist VersionTotal Downloads

It offers convenient interface for encapsulated array. Implements strategy template for any number of filters.

Requirements

  • PHP version >= 7.1
  • Sting "It's probably me" inside your headphones.

Installation

Installation using composer:

composer require andydune/array-container

Or if composer was not installed globally:

php composer.phar require andydune/array-container

Or edit your composer.json:

"require" : {
"andydune/array-container": "^1"
}

And execute command:

php composer.phar update

Simple access to array

You do not need to worry about existence any key.

useAndyDune\ArrayContainer\ArrayContainer;
$aray = newArrayContainer(['pipe' => 'handemade', 'pipes_type' => ['lovat', 'canadian']]);
$array['pipe'] // value is 'handemade' $array['tobacco'] // value is null$array->getNested('pipes_type.0') // value is 'lovat'$array->getNested('pipes_type:0', null, ':') // value is 'lovat'$array->getNested('some.some', 'NO') // value is 'NO'// Set default value$array->setDefaultValue('NO');
$array['tobacco'] // value is 'NO'

Filters

Filters are callable objects. Filters are used during request properties with getters.

useAndyDune\ArrayContainer\ArrayContainer;
$aray = newArrayContainer(['pipe' => 'handemade', 'pipes_type' => ['lovat', 'canadian']]);
$array->addFilter(function ($value) {
returnstrtoupper($value);
});
$array['pipe'] // value is 'HANDEMADE'

Modifiers

Modifier is an object of class witch implements AndyDune\ArrayContainer\Action\AbstractAction interface. It can be simple extented without modification main class.

Add keys to array if not exist.

There is source array:

$array = [
'type' => 'good'
];

You need to use it inside model. Model waits array with keys: type, key and value

useAndyDune\ArrayContainer\ArrayContainer;
useAndyDune\ArrayContainer\Action\KeysAddIfNoExist;
$container = newArrayContainer();
$defaultValue = 0; // Set this value if array key does not exist.$container->setAction(newKeysAddIfNoExist($defaultValue))->executeAction('type', 'key', 'value');
$resultArray = $container->getArrayCopy();

Result array is:

$resultArray = [
'type' => 'good',
'key' => 0,
'value' => 0
];

Array shift maintaining key to data correlations.

It is source array with numeric keys.

$arraySource = [
40 => 'fourty',
50 => 'figty',
60 => 'sixty',
];

After execution function array_shift keys will be lost.

array_shift($arraySource);
$arraySource = [
0 => 'figty',
1 => 'sixty',
];

Array container action helps to avoid it.

useAndyDune\ArrayContainer\ArrayContainer;
useAndyDune\ArrayContainer\Action\ArrayShift;
$arraySource = [
40 => 'fourty',
50 => 'figty',
60 => 'sixty',
];
$container = newArrayContainer($arraySource);
$result = $container->setAction(newArrayShift())->executeAction();
$result == [40 => 'fourty'];
$resultArray = $container->getArrayCopy();

Result array is:

[
50 => 'figty',
60 => 'sixty',
];

Add value to nested array

We have array structure of witch we don't know. Need to set value to it's nested value with check of existence of nested structure. It changes only given array keys.

// Sourse array:$array = [
'a' => 1,
'b' => [
'c' => 2
]
];
$container->setAction(newSetValueIntoNestedArray(22))->executeAction('b', 'cc');
$arrayResult = $container->getArrayCopy();
// Result array is:$arrayResult = [
'a' => 1,
'b' => [
'c' => 2,
'cc' => 22
]
];

Ome more example.

We have stait array with month, year and counts of entities within this date.

$data = [[
'month' => 7,
'year' => 2020,
'orderCount' => 2,
],
],
[
'month' => 1,
'year' => 2020,
'orderCount' => 20,
],
],
...
]

We need to recieve something like this:

$result = [
2007 => [
1 => 2,
7 => 20
]
]

Here is code for this:

useAndyDune\ArrayContainer\Action\SetValueIntoNestedArray;
useAndyDune\ArrayContainer\ArrayContainer;
$arrayContainer = newArrayContainer();
foreach($dataas$row) {
$arrayContainer->setAction(newSetValueIntoNestedArray($row['orderCount']))
->executeAction($row['year'], $row['month']);
}
$result = $arrayContainer->getArrayCopy();

Remove from array duplicated values

It needs simple to remove values witch duplicates. Here how we can do it.

useAndyDune\ArrayContainer\ArrayContainer;
useAndyDune\ArrayContainer\Action\RemoveDuplicates;
$array = [
'a' => 'a',
'b' => 'b',
'b1' => 'b',
'c' => 'c',
];
$container = newArrayContainer($array);
$count = $container->setAction(newRemoveDuplicates())->executeAction();
$count == 1; // it is count of removed items$array = $container->getArrayCopy());
$array; // it has no value with key b1

Check is value in nested array

There is nested value with any structure. It checks is some value in given array including values in nested arrays.

useAndyDune\ArrayContainer\ArrayContainer;
useAndyDune\ArrayContainer\Action\InNestedArray;
$array = [
'a' => 1,
'b' => [
'c' => 2
]
];
$container = newArrayContainer($array);
$container->setAction(newInNestedArray(1))->executeAction(); // true$container->setAction(newInNestedArray('1'))->executeAction(); // true$container->setAction(newInNestedArray(5))->executeAction(); // false// With strong type comparision$container->setAction(newInNestedArray('1', true))->executeAction(); // false

Values can be changed before comparision:

$array = [
[
[
'name' => 'Ivan'
],
[
'name' => 'Andrey'
],
]
];
$container = newArrayContainer($array);
// false: Ivan != ivan$container->setAction(newInNestedArray('ivan'))->executeAction(); // true: strtolower to values before compare$container->setAction((newInNestedArray('ivan'))->setValueMutator(function($value){
if (!is_string($value)) {
return$value;
}
returnstrtolower($value);
}))->executeAction();

Create new array with values from current array

It needs to create new list of values from any fixed list. The new list must contain random values.

useAndyDune\ArrayContainer\ArrayContainer;
useAndyDune\ArrayContainer\Action\ExtractRandomItems;
$array = [
'a',
'b',
'c',
'd',
'e',
'f'
];
$container = newArrayContainer($array);
$arrayNew = $container->setAction(newExtractRandomItems(3))->executeAction();

It leaves keys in new array as it was in source array.

Concat arrays

Function array_merge may act not right with associative arrays.

Example down next:

$a1 = ['first' => 1, 'second' => 2];
$ar = array_merge($a1, ['second' => 22])
The result is:
$ar == ['first' => 1, 'second' => 22];

Concat action helps to do it right:

useAndyDune\ArrayContainer\ArrayContainer;
useAndyDune\ArrayContainer\Action\Concat;
$a1 = ['first' => 1, 'second' => 2];
$container = newArrayContainer($a1);
$result = $container->setAction(newConcat())->executeAction(['second' => 22]);
The result is:
$ar == ['first' => 1, 'second' => 2, 22];

Computes the difference of arrays with additional index check

Compares array1 against array2 and more and returns the difference. It computes arrays recursively.

useAndyDune\ArrayContainer\ArrayContainer;
useAndyDune\ArrayContainer\Action\ComputeDifferenceOfArrays;
$arrayWait = [
'r' => 'red',
'rr' => [
'r1' => 'red1',
'rrr' => [
'r2' => 'red2',
'r22' => ['red22']
],
'r2' => 'red2',
],
'b' => 'blue'
];
$container = newArrayContainer($array);
$result = $container->setAction(newComputeDifferenceOfArrays())->executeAction(
['r' => 'red', 'rr' => ['r1' => 'red1', 'rrr' => ['r22' => ['red22']]]]
);
// The resu is:$arrayWait = [
'rr' => [
'rrr' => [
'r2' => 'red2',
'r22' => ['red22']
],
'r2' => 'red2',
],
'b' => 'blue'
];

It may ignore some keys within result.

$container = newArrayContainer($array);
$result = $container->setAction(
(newComputeDifferenceOfArrays())->ignoreKeys('r2', ['b'])))->executeAction(
['r' => 'red', 'rr' => ['r1' => 'red1']]
);
// The resu is:$arrayWait = [
'rr' => [
'rrr' => [
'r22' => ['red22']
],
]
];

Check is array has only fixed keys

It checks source array if it has only this keys.

$array = [
'r' => 'red',
'rr' => [
'r1' => 'red1',
],
'b' => 'blue'
];
$container = newArrayContainer($array);
$result = $container->setAction(newIsOnlyThisKeysInArray())->executeAction('r');
$result == false;
$container = newArrayContainer($array);
$result = $container->setAction(newIsOnlyThisKeysInArray())->executeAction('r', ['rr']);
$result == false;
$container = newArrayContainer($array);
$result = $container->setAction(newIsOnlyThisKeysInArray())->executeAction('r', ['rr', 'b']);
$result == true;

Create integer array with values not in sequence

There is an array with integer values we have. Values are mixed, not in order and can be duplicated. We can receive array with values witch were skipped.

useAndyDune\ArrayContainer\ArrayContainer;
useAndyDune\ArrayContainer\Action\GetIntegerNumbersNotInSequence;
$array = [5, 1, 7, 8];
$container = newArrayContainer($array);
$result = $container->setAction(newGetIntegerNumbersNotInSequence())->executeAction();
// result is:
[2,3,4,6] == $result;

Find max float value in array

It searches max float value in the given array. Each value is prepared by removing spaces.

useAndyDune\ArrayContainer\ArrayContainer;
useAndyDune\ArrayContainer\Action\FindMaxFloatValue;
$container = newArrayContainer(['- 1', -2.56, 10, ' 1 1 ']);
$result = $container->setAction(newFindMaxFloatValue())->executeAction();
$this->assertEquals(11, $result);
// result is:11 == $result;

Access array with path notation

Access to array value (more if array is nested) may require validation and check. Path helps make it easily.

useAndyDune\ArrayContainer\Path;
$arr = [
'key1' => 'bum',
'key2' => ['key21' => [
'key211' => 'mub'
]],
];
// To get value with key `key211` you need:$arr['key2']['key21']['key211'] // mub// with Path $arrObject = newPath($arr);
(string)$arr->key2->key21->key211; // mub

Set value inside nested array:

useAndyDune\ArrayContainer\Path;
$arrObject = newPath($arr);
$arr->key2->key21->key211 = 'bum';
(string)$arr->key2->key21->key211; // 'bum'$arr->key2->key21->noExist_id->getValue(); // null

Build array

MultilineTextToAssociatedArray

It creates array from text with lines as key-values pairs in it.

useAndyDune\ArrayContainer\Builder;
useAndyDune\ArrayContainer\BuilderStrategy\MultilineTextToAssociatedArray;
$sourceText = 'one => twothree=> four';
$expectResult = [
'one' => 'two',
'four',
'three' => null
];
$builder = newBuilder($sourceText, newMultilineTextToAssociatedArray('=>'));
// result is$expectResult == $builder->execute();

MultilineTextAsJsonToAssociatedArray

It creates array from text with lines as json-like key-values pairs in it.

useAndyDune\ArrayContainer\Builder;
useAndyDune\ArrayContainer\BuilderStrategy\MultilineTextAsJsonToAssociatedArray;
$text = '{"one":"two","two" : 2,"three":null}';
$expectResult = [
'one' => 'two',
'two' => 2,
'three' => null
];
$builder = newBuilder($text, newMultilineTextAsJsonToAssociatedArray());
// result is$expectResult == $builder->execute();

MarkdownTableToArray

It creates array from Markdown table.

Read about format here

useAndyDune\ArrayContainer\Builder;
useAndyDune\ArrayContainer\BuilderStrategy\MultilineTextAsJsonToAssociatedArray;
$text = '| one | two | | --- | ---1 | 211| 12| 13 | 14';
$expectResult = [
[
'one' => 1,
'two' => 2
],
[
'one' => '11',
'two' => null
],
[
'one' => '12',
'two' => '13'
]
];
$builder = newBuilder($text, newMarkdownTableToArray());
$expectResult == $builder->execute();

You can build not assoc array like this:

useAndyDune\ArrayContainer\Builder;
useAndyDune\ArrayContainer\BuilderStrategy\MultilineTextAsJsonToAssociatedArray;
$text = ' | one | two |  1 | 2 | || 5 11 | 12| 13 | 14';
$expectResult = [
['one', 'two'],
[1, 2],
['', 5],
['11', null],
['12', '13']
];
$builder = newBuilder($text, newMarkdownTableToArray());
$expectResult == $builder->execute();

MultilineTextToNestedAssociatedArray

It creates array from text with lines as key-values pairs in it. Values are nested arrays.

useAndyDune\ArrayContainer\Builder;
useAndyDune\ArrayContainer\BuilderStrategy\MultilineTextToNestedAssociatedArray;
$text = 'one > one, one > two, onefour > 4, 5, 6';
$expectResult = [
'one' => ['one', 'two'],
'four' => [4, 5, 6],
];
$builder = newBuilder($text, newMultilineTextToNestedAssociatedArray());
$expectResult == $builder->execute();

StringExplode

It does simple string explode procedure with removing empty values or not.

useAndyDune\ArrayContainer\Builder;
useAndyDune\ArrayContainer\BuilderStrategy\StringExplode;
$text = ' one , two,';
$expectResult = [
'one', 'two'
];
$builder = newBuilder($text, newStringExplode(','));
$expectResult == $builder->execute();

About

It offers convenien inteface for incapsulated array. Implaments strategy template for any number of filters.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages