Hello everyone, almost a year has passed since the publication of the first part . The discussion in the comments was hot, I drew conclusions for myself, made changes to the lib almost immediately, but there was no time to write about this.
Recently, I expanded the functionality with a couple of methods, and I want to share this news with you.
And of course I will write about working on bugs.
For those who did not know and have forgotten what an ArrayHandler is
Spoiler
Let's answer the question: "what is type-safe working with arrays in PHP?"
Type-safe is:
;
;
, , - . , :
$a = 0;
if (key_exists($key, $collection))
{
$a = (int) $collection[$key];
}
:
$a = (int) $collection[$key] ?? 0;
, " ".
In addition to these three advantages, ArrayHandler provides immutability, that is, we can safely pass our ArrayHandler through layers of abstractions and no one will accidentally change the elements inside the original array. In order to change the value, you need to create a new ArrayHandler instance - and it is more difficult to skip this for the code review than to skip writing a new value to an array element.
I will not copy and paste examples of working with either, you can see them in the first part or you can read the documentation .
Liba is installed through Composer:
composer require sbwerewolf/language-specific
There are versions for PHP 5.6 / 7.0 / 7.2.
It was a long introduction now to the point.
Updates
A couple of days ago I was both sad and bored, I wanted to do something good, for example, make it so that when you run through the elements using foreach (), you could not only get the (ValueHandler) element, but the index of this element.
I got to work with enthusiasm and already having written tons of code, I came across a comment in the PHP documentation that made all the new code useless.
It turns out that you can do this:
yield $key => $value;
And foreach () will return the index of the element. Eureka!
Now IArrayHandler :: pulling () returns both the new IArrayHandler from the array element and the index of that element. I was happy, it seems now ArrayHandler has become an ideal library for working with arrays (as I indicated at the beginning of the article).
. - IArrayHandler::getting(), Iterator ArrayHandler foreach() .
IArrayHandler::pulling() ArrayHandler ( , ). "pulling" - IArrayHandler::pull(), ArrayHandler .
IArrayHandler::getting() IValueHandler , . "getting" - IArrayHandler::get(), IValueHandler .
IArrayHandler::pulling() , IArrayHandler::getting() .
:
$data = new ArrayHandler(
[
'first' => ['A' => 1],
'next' => ['B'=>2],
'last' => ['C'=>3],
4=>[5,6],
7,
8,
9
]);
echo 'arrays'.PHP_EOL;
foreach ($data->pulling() as $key => $value) {
echo "[$key] => class is ".get_class($value).' '.PHP_EOL;
}
echo 'values'.PHP_EOL;
foreach ($data->getting() as $key => $value) {
echo "[$key] => {$value->asIs()} , class is ".get_class($value).' '.PHP_EOL;
}
:
arrays
[first] => class is LanguageSpecific\ArrayHandler
[next] => class is LanguageSpecific\ArrayHandler
[last] => class is LanguageSpecific\ArrayHandler
[4] => class is LanguageSpecific\ArrayHandler
values
[5] => 7 , class is LanguageSpecific\ValueHandler
[6] => 8 , class is LanguageSpecific\ValueHandler
[7] => 9 , class is LanguageSpecific\ValueHandler
, foreach():
$data = new ArrayHandler(
[
'first' => ['A' => 1],
'next' => ['B'=>2],
'last' => ['C'=>3],
4=>[5,6],
7,
8,
9
]);
echo 'ALL'.PHP_EOL;
foreach ($data as $key => $value) {
$type = gettype($value->asIs());
echo "[$key] => value type is $type , class is ".get_class($value).PHP_EOL;
}
:
ALL
[first] => value type is array , class is LanguageSpecific\ValueHandler
[next] => value type is array , class is LanguageSpecific\ValueHandler
[last] => value type is array , class is LanguageSpecific\ValueHandler
[4] => value type is array , class is LanguageSpecific\ValueHandler
[5] => value type is integer , class is LanguageSpecific\ValueHandler
[6] => value type is integer , class is LanguageSpecific\ValueHandler
[7] => value type is integer , class is LanguageSpecific\ValueHandler
, , , foreach(), :
foreach ($data as $key => $value) {
/* @var \LanguageSpecific\ValueHandler $value */
if($value->type() === 'array'){
$handler = new ArrayHandler($value->array());
/* some code */
}
}
IValueHandler::default() , @GreedyIvan, , .
ArrayHandler::simplify() ,
ArrayHandler->simplify(), array_column? (c) @olegmar
C @olegmar.
The IArrayHandler :: next () method has been replaced with IArrayHandler :: pulling (), this method iterates over all nested arrays (first nesting level). Not that the comment from @Hett convinced me directly, but pushed me into thinking.
Thanks @ ReDev1L for the support in the comments.
The IArrayHandler :: raw () method was added to get the original array. Previously, when it was not possible to get the index of an element, it was necessary to iterate over the original array, now, from experience of use, there is a need to add / subtract array elements and create a new ArrayHandler from the modified array.
That's all. Thanks for reading.