Object representing an associative array similar to plain object in JavaScript
<?php
use Dsheiko\Extras\Type\PlainObject;
$po = new PlainObject(["foo" => "FOO", "bar" => "BAR"]);
// $po = \Dsheiko\Extras\Array::object(["foo" => "FOO", "bar" => "BAR"]);
echo $po->foo; // "FOO"
echo $po->bar; // "BAR"- JavaScript-inspired methods
- Underscore.js-inspired methods
Copy the values of all properties from one or more source arrays to a target array.
{PlainObject} $target- target object{array} ...$sources- variadic list of source plain objects or arrays
assign(PlainObject $target, ...$sources): PlainObject<?php
$po1 = new PlainObject(["foo" => "FOO"]);
$po2 = new PlainObject(["bar" => "BAR"]);
$res = PlainObject::assign($po1, $po2);
echo $res->foo; // "FOO"
echo $res->bar; // "BAR"
echo $po1->foo; // "FOO"
echo $po1->bar; // "BAR"Return an array of a given object's own enumerable property [key, value] pairs
entries(): array<?php
$po = new PlainObject(["foo" => "FOO", "bar" => "BAR"]);
$res = $po->entries();
var_dump($res[0]); // ["foo", "FOO"]
var_dump($res[1]); // ["bar", "BAR"]Return all the keys or a subset of the keys of a plain object
{string} $searchValue- [OPTIONAL] substring to look for
keys($searchValue = null): array<?php
$po = new PlainObject(["foo" => "FOO", "bar" => "BAR"]);
$res = $po->keys(); // ["foo", "bar"]Transform back to array
toArray(): array<?php
$po = new PlainObject(["foo" => "FOO", "bar" => "BAR"]);
$res = $po->toArray();
echo is_array($res); // trueReturn all the values of a plain object
values(): array<?php
$po = new PlainObject(["foo" => "FOO", "bar" => "BAR"]);
$res = $po->values(); // ["FOO", "BAR"]Like map, but for associative arrays. Transform the value of each property in turn.
{callable} $iteratee- iteratee function{object} $context- (optional) context object to bind to
mapObject(callable $iteratee, $context = null): PlainObject<?php
$po = new PlainObject([
"start" => 5,
"end" => 12,
]);
$res = $po->mapObject(function($val){
return $val + 5;
}); // PlainObject{ "start": 10, "end": 17 }Convert an object into a list of [key, value] pairs. Alias of entries
pairs(): array<?php
$po = new PlainObject(["foo" => "FOO", "bar" => "BAR"]);
$res = $po->pairs(); // [["foo", "FOO"], ["bar", "BAR"]]Returns a copy of the object where the keys have become the values and the values the keys. For this to work, all of your object's values should be unique and string serializable.
invert(): PlainObject<?php
$po = new PlainObject([
"Moe" => "Moses",
"Larry" => "Louis",
"Curly" => "Jerome",
]);
$res = $po->invert();
echo $res->Moses; // "Moe"
echo $res->Louis; // "Larry"
echo $res->Jerome; // "Curly"Similar to findIndex, but for keys in objects. Returns the key where the predicate truth test passes or undefined
{callable} $iteratee- iteratee function{object} $context- (optional) context object to bind to
findKey($iteratee = null, $context = null): string<?php
$po = new PlainObject([
"foo" => [
'name' => 'Ted',
'last' => 'White',
],
"bar" => [
'name' => 'Frank',
'last' => 'James',
],
"baz" => [
'name' => 'Ted',
'last' => 'Jones',
],
]);
$res = $po->findKey([ "name" => "Ted" ]); // "foo"Return a copy of the object, filtered to only have values for the whitelisted keys (or array of valid keys). Alternatively accepts a predicate indicating which keys to pick.
{array} ...$keys- keys or iteratee function
pick(...$keys): PlainObject<?php
$po = new PlainObject([
'name' => 'moe',
'age' => 50,
'userid' => 'moe1',
]);
$res = $po->pick('name', 'age');
// PlainObject{ 'name': 'moe', 'age': 50 }<?php
$po = new PlainObject([
'name' => 'moe',
'age' => 50,
'userid' => 'moe1',
]);
$res = $po->pick(function($value){
return is_int($value);
});
// PlainObject{ 'age': 50 }Return a copy of the object, filtered to omit the blacklisted keys (or array of keys). Alternatively accepts a predicate indicating which keys to omit.
{array} ...$keys- keys or iteratee function
omit(...$keys): PlainObject<?php
$po = new PlainObject([
'name' => 'moe',
'age' => 50,
'userid' => 'moe1',
]);
$res = $po->omit('userid');
// PlainObject{ 'name': 'moe', 'age': 50 }<?php
$po = new PlainObject([
'name' => 'moe',
'age' => 50,
'userid' => 'moe1',
]);
$res = $po->omit(function($value){
return is_int($value);
});
// PlainObject{ 'name': 'moe', 'userid': 'moe1' }Fill in undefined properties in object with the first value present in the following list of defaults objects.
{array} $defaults- key-value array of defaults
defaults(array $defaults): PlainObject<?php
$po = new PlainObject([
"flavor" => "chocolate"
]);
$res = $po->defaults([
"flavor" => "vanilla",
"sprinkles" => "lots",
]);
// PlainObject{ "flavor": "chocolate", "sprinkles": "lots" }Does the object contain the given key? Identical to object.hasOwnProperty(key), but uses a safe reference to the hasOwnProperty function, in case it's been overridden accidentally.
{string} $key- key to look for
has(string $key): bool<?php
$po = new PlainObject([
"flavor" => "vanilla",
"sprinkles" => "lots",
]);
$res = $po->has("flavor"); // trueReturns true if an enumerable object contains no values (no enumerable own-properties).
isEmpty(): bool<?php
$po = new PlainObject([]);
$res = Arrays::isEmpty($po); // true