{"id":20028738,"url":"https://github.com/phase2/p2ools","last_synced_at":"2025-07-05T09:40:18.531Z","repository":{"id":2157540,"uuid":"3103009","full_name":"phase2/p2ools","owner":"phase2","description":"Phase2 Tools","archived":false,"fork":false,"pushed_at":"2012-08-01T17:22:46.000Z","size":142,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":79,"default_branch":"master","last_synced_at":"2025-01-12T17:35:42.528Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"ethereum/web3.js","license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/phase2.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-01-04T15:43:47.000Z","updated_at":"2018-04-04T17:49:21.000Z","dependencies_parsed_at":"2022-09-14T04:31:17.780Z","dependency_job_id":null,"html_url":"https://github.com/phase2/p2ools","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phase2%2Fp2ools","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phase2%2Fp2ools/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phase2%2Fp2ools/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phase2%2Fp2ools/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/phase2","download_url":"https://codeload.github.com/phase2/p2ools/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241460048,"owners_count":19966518,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-13T09:16:18.552Z","updated_at":"2025-03-02T04:45:29.149Z","avatar_url":"https://github.com/phase2.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# p2ools\n\nA collection of utilities that make writing Drupal and PHP easier. This should\nserve as a point of consolidation for utilities that are written and re-written\nacross Drupal projects.\n\nP2ools currently includes\n\n* a logging abstraction\n\n```php\n\u003c?php\n$lg = new Logger(\"foo_module\");\n$lg-\u003einfo(\"This is a var!\", array(1, 2, 3));\n\n  =\u003e \"[file basename]:[line] This is a var! Array\n      (\n          [0] =\u003e 1\n          [1] =\u003e 2\n          [2] =\u003e 3\n      )\"\n```\n\n* a fancier taxonomy API\n\n```php\n\u003c?php\n$t = new Tax();\n$t-\u003eget_term(\"saucy\", \"tags\");\n\n  =\u003e stdClass rep. of \"saucy\" term in \"tags\" vocab\n```\n\n* an array wraper\n\n```php\n\u003c?php\n$a = array(1, 2, 20, 25);\n$arr = new ArrWrap($a);\n$arr\n  -\u003efilter(function ($v) {return $v \u003e 15;})\n  -\u003emap(function($v) {return $v * 2;});\n\n  =\u003e \u003cobject #380 of type ArrWrap\u003e {\n      arr =\u003e array(\n        2 =\u003e 40,\n        3 =\u003e 50,\n      ),\n    }\n```\n\n## Using it\n\nSimply include the following in your Drush makefile:\n\n    projects[p2ools][type] = module\n    projects[p2ools][subdir] = contrib\n    projects[p2ools][download][type] = git\n    projects[p2ools][download][url] = git://github.com/phase2/p2ools.git \n\nAfterwards, all of the classes contained in p2ools will be at your disposal.\n \n## Testing it\n\nTests can be run using the following command:\n\n    drush test-run --uri=[url of test site] p2ools\n\n## Guidelines for contribution\n                          \n* Pull requests should be submitted for contributions.\n* All contributed code must have corresponding unit-tests written and placed\n  in `tests/`. The goal here is to have 100% test coverage of p2ools.\n* Contributed code must follow the Drupal style guide.\n\n## API\n\n### Logger\n\nA logging abstraction that wraps an arbitrary logging function, but defaults to\nwatchdog. `Logger` keeps track of your module name, features logging levels,\nautomatically provides file basename and line identification, and provides easy\n`print_r`ing.\n\n#### Usage\n\n    $log = new Logger('modulename', Logger::INFO);\n\n    $log-\u003einfo(\"I need to test the watchdog log.\");\n    $log-\u003eerror(\"This var will be print_r'd\", $var);\n\n    $log-\u003edebug(\"This won't display because of the log level.\");\n    $log-\u003eVERBOSITY = Logger::DEBUG;\n    $log-\u003edebug(\"Now it will!\");\n\n### Tax\n\nTaxonomy manipulation done easier.\n\n#### Summary\n\n* `Tax::get_term(name_or_tid [,vocab_name_or_vid])`: An easy way to retrieve \n  a taxonomy object.\n* `Tax::get_tid(name_or_tid [,vocab_name_or_vid])`: An easy way to retrieve a\n  tid.\n\n#### Usage\n\n##### Fancy taxonomy indexing\n\n    php\u003e $tax_util = new Tax();\n\n    php\u003e = $tax_util-\u003eget_term('foobar');\n    false\n    \n    php\u003e $tax_term = new stdClass();\n    php\u003e $tax_term-\u003ename = \"foobar\";\n    php\u003e $tax_term-\u003evid = 1;\n    php\u003e = taxonomy_term_save($tax_term);\n    1\n\n    php\u003e $the_term = $tax_util-\u003eget_term('foobar');\n    php\u003e = $the_term\n    \u003cobject #92 of type stdClass\u003e {\n      tid =\u003e \"1\",\n      vid =\u003e \"1\",\n      name =\u003e \"foobar\",\n      description =\u003e null,\n      format =\u003e null,\n      weight =\u003e \"0\",\n      vocabulary_machine_name =\u003e \"tags\",\n      rdf_mapping =\u003e array(\n        \"rdftype\" =\u003e array(\n          0 =\u003e \"skos:Concept\",\n        ),\n        \"name\" =\u003e array(\n          \"predicates\" =\u003e array(\n            0 =\u003e \"rdfs:label\",\n            1 =\u003e \"skos:prefLabel\",\n          ),\n        ),\n        \"description\" =\u003e array(\n          \"predicates\" =\u003e array(\n            0 =\u003e \"skos:definition\",\n          ),\n        ),\n        \"vid\" =\u003e array(\n          \"predicates\" =\u003e array(\n            0 =\u003e \"skos:inScheme\",\n          ),\n          \"type\" =\u003e \"rel\",\n        ),\n        \"parent\" =\u003e array(\n          \"predicates\" =\u003e array(\n            0 =\u003e \"skos:broader\",\n          ),\n          \"type\" =\u003e \"rel\",\n        ),\n      ),\n    }        \n    php\u003e assert ($the_term == $tax_util-\u003eget_term('foobar', 1))\n    php\u003e assert ($the_term == $tax_util-\u003eget_term('foobar', 'tags'))\n    php\u003e assert ($the_term == $tax_util-\u003eget_term(1, 1))\n    php\u003e assert ($the_term == $tax_util-\u003eget_term(1, 'tags'))\n\n##### Easy tid retrieval\n\n    php\u003e // use with the same args above\n    php\u003e = $tax_util-\u003eget_tid(1)\n    \"1\"\n    php\u003e = $tax_util-\u003eget_tid('foobar')\n    \"1\"\n    php\u003e = $tax_util-\u003eget_tid('foobar', 'tags')\n    \"1\"\n\n### ArrWrap\n\n#### Real quick\n\nCheck out this PHP:\n\n    $x = 1;\n    $nums = array(10, 20, 30, 40);\n\n    $arr = array();\n    foreach ($nums as $n)\n      if ($n \u003e 15)\n        $arr[] = $n * 2 + $x;\n    $res = 0;\n    foreach ($arr as $r)\n      $res -= $r;\n\nGross, right? Now check out this PHP:\n      \n    $aw_nums = new ArrWrap($nums);\n    $res = $aw_nums\n      -\u003efilter(function($v) {return $v \u003e 15;})\n      -\u003emap(function($v) use ($x) {return $v * 2 + $x;})\n      -\u003ereduce(function($v, $w) {return $v + $w;});\n\nNow we're talkin'.\n\nWhen you're writing PHP, you're using arrays. A lot. Unfortunately, arrays\nin PHP are pretty cumbersome.\n\nWouldn't you like an array that\n\n* returns null if the key you've referenced doesn't exist?\n* allows you to use it like an object, including all array functions as methods?\n* allows method chaining?\n* is subclass-able?\n* includes nice utility methods?\n\nWait no longer. \n\n`ArrWrap` is an Array-like object that makes using arrays easier at almost no \nperformance cost, since it just wraps array references. Check out a sample \nusage.\n    \n#### Usage\n\n    php\u003e $a = new ArrWrap();\n    php\u003e $a[1] = 2;\n    php\u003e = $a[1];\n    2\n    php\u003e = $a['abc'];\n    php\u003e if (!$a['abc']) echo \"foobar\";\n    foobar\n\nTired of repeating `isset($a[$foo]) ? $a[$foo] : 'abc';`? Skip that noise\nand use `ArrWrap::get`:\n\n    php\u003e echo $a-\u003eget('abc', 1);\n    1\n    php\u003e $a['yo'] = 'bar';\n    php\u003e echo $a-\u003eget('yo', 1);\n    bar\n\nHow about more concise access to your favorite array functions?\n\n    php\u003e $arr = new ArrWrap(array_fill(0, 3, 1));\n\n    php\u003e = $arr-\u003eunique();\n    \u003cobject #607 of type ArrWrap\u003e {\n      arr =\u003e array(\n        0 =\u003e 1,\n      ),\n    }\n\n    php\u003e $u = $arr-\u003eunique();\n    php\u003e = $u[0];\n    1\n\n    php\u003e = $arr-\u003emerge(array(1, 2, 3));\n    \u003cobject #607 of type ArrWrap\u003e {\n      arr =\u003e array(\n        0 =\u003e 1,\n        1 =\u003e 1,\n        2 =\u003e 1,\n        3 =\u003e 1,\n        4 =\u003e 2,\n        5 =\u003e 3,\n      ),\n    }\n\n    php\u003e = $arr-\u003echunk(2);\n    \u003cobject #607 of type ArrWrap\u003e {\n      arr =\u003e array(\n        0 =\u003e array(\n          0 =\u003e 1,\n          1 =\u003e 1,\n        ),\n        1 =\u003e array(\n          0 =\u003e 1,\n        ),\n      ),\n    }\n\nThat's right: any `array_` function that accepts an array as the first\nargument is automagically a method of `ArrWrap` objects.\n\nBut what about functions that aren't? Easy. The `arr` attribute of any\n`ArrWrap` object contains its underlying array.\n\n    php\u003e = count($arr-\u003earr);\n    3\n\n`ArrWrap` objects are also fully iterable.\n\n    php\u003e foreach($arr as $num) {\n     ...   print_r($num);\n     ... }\n    111\n\n    php\u003e foreach($arr as $i =\u003e $num) {\n     ...   print_r(\"${i} =\u003e ${num}; \");\n     ... }\n    0 =\u003e 1; 1 =\u003e 1; 2 =\u003e 1;\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphase2%2Fp2ools","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphase2%2Fp2ools","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphase2%2Fp2ools/lists"}