PHPackages                             strukt/commons - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. strukt/commons

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

strukt/commons
==============

Strukt Common Utilities

v1.1.2-alpha(1y ago)11.4k—0%2MITPHPPHP ^8.1

Since Nov 5Pushed 4mo ago1 watchersCompare

[ Source](https://github.com/struktapp/strukt-commons)[ Packagist](https://packagist.org/packages/strukt/commons)[ RSS](/packages/strukt-commons/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (5)Versions (15)Used By (2)

Strukt Commons
==============

[](#strukt-commons)

[![Build Status](https://camo.githubusercontent.com/0d66ec7adcf2b49811ba0d9ecf4829f9ba885ba9b6007239ee04aace193c08a6/68747470733a2f2f7472617669732d63692e6f72672f706974736f6c752f737472756b742d636f6d6d6f6e732e7376673f6272616e63683d6d6173746572)](https://packagist.org/packages/strukt/commons)[![Latest Stable Version](https://camo.githubusercontent.com/4866ade9ec92856305bf67810e1c10b640141a5c5ade77d6abd35fbf3625c71c/68747470733a2f2f706f7365722e707567782e6f72672f737472756b742f636f6d6d6f6e732f762f737461626c65)](https://packagist.org/packages/strukt/commons)[![Total Downloads](https://camo.githubusercontent.com/da77561a304189cceb268f1e4785c7251c6bd0008c7ce6609ab0eca4a30c8f34/68747470733a2f2f706f7365722e707567782e6f72672f737472756b742f636f6d6d6f6e732f646f776e6c6f616473)](https://packagist.org/packages/strukt/commons)[![Latest Unstable Version](https://camo.githubusercontent.com/e520ba1d00135a6105faee37c434957d716eab8a4dd89478202bd5bedea5650a/68747470733a2f2f706f7365722e707567782e6f72672f737472756b742f636f6d6d6f6e732f762f756e737461626c65)](https://packagist.org/packages/strukt/commons)[![License](https://camo.githubusercontent.com/9cb109bfd2b76ead02d58d0cc4321b50172ebd268fffe6005bc83cdda9077d5e/68747470733a2f2f706f7365722e707567782e6f72672f737472756b742f636f6d6d6f6e732f6c6963656e7365)](https://packagist.org/packages/strukt/commons)

Usage
-----

[](#usage)

### Collection

[](#collection)

```
$contact = collect([]);
$contact->set("mobile", "+2540770123456");
$contact->set("work-phone", "+2540202345678");

$user = collect([]);
$user->set("contacts", $contact);
$user->get("contacts.mobile"); //outputs +2540770123456

$s = array(
    "user"=>array(
        "firstname"=>"Gene",
		"surname"=>"Wilder",
		"db"=>array(
            "config"=>array(
                "username"=>"root",
				"password"=>"_root!"
            )
        ),
        "mobile_numbers"=>array( //Non Assoc Array
            "777111222",
            "770234567"
        )
    )
);

$b = collect($s);
$x = $b->get("user.db.config.username"); //returns root
```

Value Objects
-------------

[](#value-objects)

You may also find `Number` object via package `strukt/math` that is a dependency of this package.

### DateTime

[](#datetime)

```
$start = when()
$end = when("+30 days");
$rand = $start->rand($end);//make start date random date between start and end
$end->gt($start);//true
$end->gte($start);//true
$start->lt($end);
$end->lte($end);//true
$start->equals($end);//false
$start->same(new DateTime);//true -- is the same day
$newStart = $start->clone();
$newStartPlusOneDay = $start->clone("+1 day");
$start->reset();//reset time to 00:00:00 000000
$start->last();//reset time to 23:59:59 1000000
echo $start; //return date as string
```

### Today (Date Influence)

[](#today-date-influence)

```
// $p = period(when("1900-01-01"), when("1963-12-31"));
$p = period()

//In order for date influence to work the first 2 line below must be
// called before any further date manipulation
// messing arround with dates can create headaches
$p->create(when("1900-01-01"), when("1963-12-31")); //period
$p->reset(when("1960-03-23"));//create fake today
$fakeToday = today();

//All dates created with Strukt\Type\DateTime will be in 1960
$fakeToday->same(new DateTime); //false
$fakeToday->same(when());//true
$fakeToday->hasPeriod()//true -- has period
$fakeToday->withDate(when("1959-04-01"))->isValid(); //true -- is date valid with period

// $fakeToday->getState("period.start");
// $fakeToday->getState("period.end");
$fakeToday->getState();//get state of date manipulation
$fakeToday->reset();//reset back to original today
```

### String

[](#string)

```
$str = str("Strukt Framework");
$str->empty();//false
$str->startsWith("Str");//true
$str->endsWith("work");//true
$str->first(3);//Str
$str->last(4);//work
$str->contains("Frame");//true
$str->slice(7,5)->equals("Frame");//true
$str->replace("work", "play")->equals("Strukt Frameplay");
$str->replaceFirst("k","c")->equals("Struct Framework");
$str->replaceLast("k","d")->equals("Struct Frameword");
$str->replaceAt("ing", 3, 3)->equals("String Framwork")
$str->toUpper();//STRUKT FRAMEWORK
$str->toLower();//strukt framework
$camel = str("thisIsCamelCase");
$camel->toSnake();//this_is_camel_case
$camel->toSnake()->toCamel();//ThisIsCamelCase
$sdo = $str->prepend("Doctrine + ");//Doctrine + Strukt Framework
$sdo->concat(" = Strukt Do");//Doctrine + Strukt Framework = Strukt Do
$str->split(" ");//['Strukt', "Framework"]
str("blah blah blah")->count("blah");//3
```

### Array

[](#array)

```
$rr = array(
    "firstname"=>"Bruce",
    "lastname"=>"Wayne",
    "alias"=>"Joker",
    "contacts" =>array(
        "email"=>"brucewayne@wayneent.com",
        "address"=>array(

            "street"=>"Boulavard of Broken Dreams",
            "building"=>"Wayne Co."
        )
    )
);

$arr = arr($rr);//Arr::create($rr)
$arr->has("Banner")//false
$arr->empty();//false
$arr->length();//3
$arr->count();//3
$arr->next();//true
$arr->current()->yield();//Wayne
$arr->key();//lastname
$arr->last()->equals($rr["contacts"]);
$arr->reset();
$arr = $arr->each(function($key, $val){ //loop

    if($key == "alias")
        $val = "Batman";

    return $val;
});
$arr = $arr->recur(function($key, $val){ //recursive iterate

    if($key == "building")
        $val = "Wayne Co. & Associates";

    return $val;
});
$origarr = $arr->yield();
$rawarr = $arr->map(array( //reformat array

    "email_contact"=>"contacts.email",
    "address_street"=>"contacts.address.street",
    "address_building"=>"contacts.address.building"
));
$arr->pop();// remove at end of array.
$arr->push("brucebatman", "username");//add at end of queue. (key is optional)
$arr->enqueue("active", "status");//same as Arr.push (key is optional)
$arr->enqueueBatch(["empty","empty"]);
$arr->prequeue("admin", "type");//add at beginning of queue. (key is optional)
$arr->dequeue();//remove at beginning of array. returns Bruce
$flatarr = $arr->level();//flattens multidimentional array
$is_assoc = arr(["username"=>"pitsolu", "password"="redacted"])->isMap();//is fully associative arr
$arr = arr(array(
    array(
        "username"=>"pitsolu",
        "type"=>"admin"
    ),
    array(
        "username"=>"peterparker",
        "type"=>"user"
    )
));
$arr->column("type");//returns array("admin", "user")
$arr = arr(array(

    "user"=>"pitsolu",
    "type"=>"admin",
    "status"=>"active"
));
$arr->tokenize();//returns user:pitsolu|type:admin|status:active
$arr->concat(",")//pitsolu,admin,active
arr(["a","b","c"])->isOfStr();// true
arr([1,2,3])->isOfNum();// true
arr(["a","a","b","b","b","c","c","d"])->distict()->yield();//["a" => 2,"b" => 3,"c" => 2,"d" => 1]
arr(["a","b","b","c","c","c","d"])->uniq()->yield();//["a","b","c","d"]
arr(["a","b","c","d"])->slice(2)//["c","d"]
arr(["a","b","c","d"])->slice(1,3)//["b","c","d"]

$x = ["name"=>"peter","email"=>"peter@gmail.com"];
arr($x)->only(["email"])->yield()//["email"=>"peter@gmail.com"]

$x = [["name"=>"peter","email"=>"peter@gmail.com"], ["name"=>"john","email"=>"john@gmail.com"]]
arr($x)->order()->asc("email")->yield()//sorting 2d array by column
arr($x)->nested();//is nested array - true
arr([1,2,3])->product();//6
arr(["ab","cd","ef"])->has("ab");//true
arr(["a"=>1,"b"=>2,"c"=>3])->contains("a")//true
arr(["a"=>1,"b"=>2,"c"=>3])->values()//[1,2,3]
arr(["a","b"])->merge(["c","d"])->yield();//["a","b","c","d"]
arr(["a","b","c","d"])->reverse()->yield();//["d","c","b","a"]
arr([3="c", 1=>"b", 2="a"])->rehash()->yield();//Order array int keys
```

Others
------

[](#others)

### Token Query

[](#token-query)

```
/**
 * Basic Token
 */
$token = "user:pitsolu|status:active|is_superuser:true";

$query = token($token);

$query->get("user");//pitsolu
$query->get("status");//active
$query->get("is_superuser");//true

$query->has("role");//false
$query->keys();//["user","status","is_superuser"]

$query->token();//original token -- user:pitsolu|status:active|is_superuser:true
$query->set("role","admin");
$query->yield();//user:pitsolu|status:active|is_superuser:true|role:admin

/**
 * Complex Token
 */
$token = "contact:1|is:tenant,landlord,prospect";

$query = token($token);

$query->get("is");//["tenant","landlord","prospect"];
$query->set("status", ["active","published"]);
$query->yield();//contact:1|is:tenant,landlord,prospect|status:active,published
```

### Heap/Messages

[](#heapmessages)

```
heap("error 401!");
heap("error 402!");
heap("error 404!");

# $errors = heap()->get(pattern:"error");
$errors = heap()->get();

$errors->last()->yield(); //error 404!
$errors->reset();
$errors->current()->yield(); //error 401!
$errors->next();
$errors->current()->yield(); //error 402!
```

### Json

[](#json)

```
$l = json(array("fname"=>"Peter", "lname"=>"Pan"));//json string
$l->encode();//json string
$m = $l->pp();//pretty print
$n = $l->decode();//array
json("{}")->valid();// is valid json. will return true
```

###  Health Score

41

—

FairBetter than 88% of packages

Maintenance68

Regular maintenance activity

Popularity19

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 90.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~136 days

Recently: every ~181 days

Total

13

Last Release

373d ago

PHP version history (3 changes)v1.0.0-alphaPHP &gt;=7.0

v1.0.6-alphaPHP &gt;=7.4

v1.1.0-alphaPHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/6f77a56030c77ce25f7fcf6db800e45bb6a31bf3c07740b43aeabcdf5e9edec2?d=identicon)[pitsolu](/maintainers/pitsolu)

---

Top Contributors

[![pitsolu](https://avatars.githubusercontent.com/u/16669704?v=4)](https://github.com/pitsolu "pitsolu (150 commits)")[![samweru](https://avatars.githubusercontent.com/u/104033121?v=4)](https://github.com/samweru "samweru (15 commits)")

---

Tags

array-manipulationscollectiondate-manipulationenvfile-cache-storemaptokenization

### Embed Badge

![Health badge](/badges/strukt-commons/health.svg)

```
[![Health](https://phpackages.com/badges/strukt-commons/health.svg)](https://phpackages.com/packages/strukt-commons)
```

###  Alternatives

[civicrm/civicrm-core

Open source constituent relationship management for non-profits, NGOs and advocacy organizations.

728272.9k17](/packages/civicrm-civicrm-core)[cognesy/instructor-php

The complete AI toolkit for PHP: unified LLM API, structured outputs, agents, and coding agent control

310107.9k1](/packages/cognesy-instructor-php)[abuseio/abuseio

Open Source abusemanagement tool

2271.5k](/packages/abuseio-abuseio)[halfpastfouram/phpchartjs

PHP library for ChartJS

2512.2k](/packages/halfpastfouram-phpchartjs)[ffi/var-dumper

List of symfony/var-dumper casters with FFI support

2010.7k4](/packages/ffi-var-dumper)[avency/neos-vardump

Neos VarDump Package

147.1k](/packages/avency-neos-vardump)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
