2017 © Pedro Peláez
 

library php-missing

The stdlib you wish PHP had

image

dxw/php-missing

The stdlib you wish PHP had

  • Tuesday, October 24, 2017
  • by tomdxw
  • Repository
  • 6 Watchers
  • 1 Stars
  • 5,956 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 6 Open issues
  • 4 Versions
  • 7 % Grown

The README.md

php-missing

PHP's standard library contains a lot of stuff, but it's missing a lot of stuff. All these functions are basic things that I've had to implement over and over again., (*1)

Installation

Install composer., (*2)

Run the following in the root of your project:, (*3)

composer require dxw/php-missing

Or, create a composer.json file with the following contents and run "composer install":, (*4)

{
  "require": {
    "dxw/php-missing": "^2.0"
  }
}

API

Arrays

, (*5)

$array = \Missing\Arrays::flatten($array)

Flattens an array containing arrays., (*6)

\Missing\Arrays::flatten([1, [2, 3, [4, 5]]]) === [1, 2, 3, 4, 5]

, (*7)

$array = \Missing\Arrays::sortBy($array, $callback)

Sorts $array by $callback($array_element)., (*8)

\Missing\Arrays::sortBy(['abc', 'ab', 'a'], function ($a) {return strlen($a);}) === ['a', 'ab', 'abc']

Dates

, (*9)

list($timestamp, $err) = \Missing\Dates::parse($str)

Parses several common/standard time formats, returns a Dxw\Result\Result object containing either the UNIX timestamp or an error., (*10)

$result = \Missing\Dates::parse(get_post_meta($post->ID, '_EventStartDate', true));
if ($result->isErr()) {
  $date = 'Unknown date';
} else {
  $date = strftime('%e %B %Y', $result->unwrap());
}

The following date formats are parsed:, (*11)

  • %Y-%m-%dT%H:%M:%S
  • %Y-%m-%d %H:%M:%S
  • %Y-%m-%dT%H:%M
  • %Y-%m-%d %H:%M
  • %Y-%m-%d

, (*12)

$date = \Missing\Dates::strftime($date_string, $format, $else, $tz)

Parses $date_string using \Missing\Dates::parse() (also accepts a UTC timestamp), if it parses correctly, return the date formatted with $format in the timezone $tz, otherwise return $else., (*13)

<p>Date: <?php echo \Missing\Dates::strftime($date, 'd/m/Y', 'unknown', 'Europe/London') ?></p>

Ints

, (*14)

$string = \Missing\Ints::ordinalize($number)

Returns English ordinals for any given integer (i.e. 1 => "1st", 2 => "2nd")., (*15)

Copied directly from active_record's Inflector#ordinalize (also MIT licensed)., (*16)

Reflection

, (*17)

$value = \Missing\Reflection::call($object, $methodName, $arguments)

Calls private or protected method $methodName of $object with $arguments (an array) and returns what it returns., (*18)

Strings

, (*19)

$bool = \Missing\Strings::startsWith($haystack, $needle)

Returns true if string $haystack starts with $needle (uses substr() - regexes not supported)., (*20)

, (*21)

$bool = \Missing\Strings::endsWith($haystack, $needle)

Returns true if string $haystack ends with $needle (uses substr() - regexes not supported)., (*22)

, (*23)

$string = \Missing\Strings::getOutput($callback)

Executes $callback, returns what it prints as a string., (*24)

Licence

MIT - see COPYING.md, (*25)

\Missing\Ints::ordinal and \Missing\Ints::ordinalize were ported from the Rails active_support module, also licensed under MIT., (*26)

The Versions

24/10 2017

dev-feature/psalm

dev-feature/psalm https://github.com/dxw/php-missing

The stdlib you wish PHP had

  Sources   Download

MIT

The Requires

 

The Development Requires

17/05 2016

dev-master

9999999-dev https://github.com/dxw/php-missing

The stdlib you wish PHP had

  Sources   Download

MIT

The Development Requires

17/05 2016

v1.0.0

1.0.0.0 https://github.com/dxw/php-missing

The stdlib you wish PHP had

  Sources   Download

MIT

The Development Requires

03/11 2015

dev-feature/php-cs-fixer

dev-feature/php-cs-fixer https://github.com/dxw/php-missing

The stdlib you wish PHP had

  Sources   Download

MIT

The Development Requires