Skip to content

Latest commit

 

History

History
398 lines (308 loc) · 7.57 KB

File metadata and controls

398 lines (308 loc) · 7.57 KB

Dsheiko\Extras\Type\PlainObject

Object representing an associative array similar to plain object in JavaScript

Overview example

<?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"

Methods

JavaScript-inspired methods

assign

Copy the values of all properties from one or more source arrays to a target array.

Parameters
  • {PlainObject} $target - target object
  • {array} ...$sources - variadic list of source plain objects or arrays
Syntax
 assign(PlainObject $target, ...$sources): PlainObject
Example
<?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"

entries

Return an array of a given object's own enumerable property [key, value] pairs

Syntax
 entries(): array
Example
<?php
$po = new PlainObject(["foo" => "FOO", "bar" => "BAR"]);
$res = $po->entries();
var_dump($res[0]); // ["foo", "FOO"]
var_dump($res[1]); // ["bar", "BAR"]

keys

Return all the keys or a subset of the keys of a plain object

Parameters
  • {string} $searchValue - [OPTIONAL] substring to look for
Syntax
 keys($searchValue = null): array
Example
<?php
$po = new PlainObject(["foo" => "FOO", "bar" => "BAR"]);
$res = $po->keys(); // ["foo", "bar"]

toArray

Transform back to array

Syntax
 toArray(): array
Example
<?php
$po = new PlainObject(["foo" => "FOO", "bar" => "BAR"]);
$res = $po->toArray();
echo is_array($res); // true

values

Return all the values of a plain object

Syntax
 values(): array
Example
<?php
$po = new PlainObject(["foo" => "FOO", "bar" => "BAR"]);
$res = $po->values(); // ["FOO", "BAR"]

Underscore.js\Objects methods

mapObject

Like map, but for associative arrays. Transform the value of each property in turn.

Parameters
  • {callable} $iteratee - iteratee function
  • {object} $context - (optional) context object to bind to
Syntax
 mapObject(callable $iteratee, $context = null): PlainObject
Example
<?php
$po = new PlainObject([
        "start" => 5,
        "end" => 12,
]);
$res = $po->mapObject(function($val){
    return $val + 5;
}); // PlainObject{ "start": 10, "end": 17 }

pairs

Convert an object into a list of [key, value] pairs. Alias of entries

Syntax
 pairs(): array
Example
<?php
$po = new PlainObject(["foo" => "FOO", "bar" => "BAR"]);
$res = $po->pairs(); // [["foo", "FOO"], ["bar", "BAR"]]

invert

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.

Syntax
 invert(): PlainObject
Example
<?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"

findKey

Similar to findIndex, but for keys in objects. Returns the key where the predicate truth test passes or undefined

Parameters
  • {callable} $iteratee - iteratee function
  • {object} $context - (optional) context object to bind to
Syntax
 findKey($iteratee = null, $context = null): string
Example
<?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"

pick

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.

Parameters
  • {array} ...$keys - keys or iteratee function
Syntax
 pick(...$keys): PlainObject
Example #1
<?php
$po = new PlainObject([
    'name' => 'moe',
    'age' => 50,
    'userid' => 'moe1',
]);
$res = $po->pick('name', 'age');
// PlainObject{ 'name': 'moe', 'age': 50 }
Example #2
<?php
$po = new PlainObject([
    'name' => 'moe',
    'age' => 50,
    'userid' => 'moe1',
]);

$res = $po->pick(function($value){
    return is_int($value);
});
// PlainObject{ 'age': 50 }

omit

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.

Parameters
  • {array} ...$keys - keys or iteratee function
Syntax
 omit(...$keys): PlainObject
Example #1
<?php
$po = new PlainObject([
    'name' => 'moe',
    'age' => 50,
    'userid' => 'moe1',
]);

$res = $po->omit('userid');
// PlainObject{ 'name': 'moe', 'age': 50 }
Example #2
<?php
$po = new PlainObject([
    'name' => 'moe',
    'age' => 50,
    'userid' => 'moe1',
]);

$res = $po->omit(function($value){
    return is_int($value);
});
// PlainObject{ 'name': 'moe', 'userid': 'moe1' }

defaults

Fill in undefined properties in object with the first value present in the following list of defaults objects.

Parameters
  • {array} $defaults - key-value array of defaults
Syntax
 defaults(array $defaults): PlainObject
Example
<?php
$po = new PlainObject([
    "flavor" => "chocolate"
]);
$res = $po->defaults([
     "flavor" => "vanilla",
     "sprinkles" => "lots",
]);
// PlainObject{ "flavor": "chocolate", "sprinkles": "lots" }

has

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.

Parameters
  • {string} $key - key to look for
Syntax
 has(string $key): bool
Example
<?php
$po = new PlainObject([
     "flavor" => "vanilla",
     "sprinkles" => "lots",
]);
$res = $po->has("flavor"); // true

isEmpty

Returns true if an enumerable object contains no values (no enumerable own-properties).

Syntax
 isEmpty(): bool
Example
<?php
$po = new PlainObject([]);

$res = Arrays::isEmpty($po); // true