2017 © Pedro Peláez
 

library php-component

PHP components - collection of cross-project PHP classes

image

ansas/php-component

PHP components - collection of cross-project PHP classes

  • Friday, July 27, 2018
  • by dhm80
  • Repository
  • 2 Watchers
  • 1 Stars
  • 223 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 35 Versions
  • 4 % Grown

The README.md

PHP components

Latest Stable Version Total Downloads Latest Unstable Version License, (*1)

Collection of cross-project PHP classes., (*2)

Install:, (*3)

$ composer require ansas/php-component

Ansas\Component\Convert\ConvertPrice

Convert "dirty" prices into Euro or Cent, (*4)

Methods (without internal):, (*5)

public function __construct($price = null, $format = self::EURO)
public function __toString()
public function clearPrice()
public function getPrice($format = self::EURO)
public function setPrice($price, $format = self::EURO)
public function sanitize($price, $format = self::EURO)
final public static function getInstance()

Ansas\Component\Collection\Collection

Making handling of context data a bit easier., (*6)

Usage:, (*7)

use Ansas\Component\Collection\Collection;

// Create e. g. context
$context = new Collection([
    'key1' => 'value1',
]);

// Setter
$context->key2 = 'value2';
$context['key3'] = 'value3';
$context->set('key4', 'value4');
$context->append([
    'key4', 'value4',
    'key5', 'value5',
]);

// Getter
$key1 = $context->get('key1');
$key2 = $context['key2'];
$key3 = $context->key3;
$key4 = $context->need('key6'); // throws Exception if key does not exist

// Getter (array)
$array = $context->all();
$partArray1 = $context->only('key1,key3');
$partArray2 = $context->only(['key2', 'key4']);

// Count
$elemets = $context->count();
$elemets = count($context);

// Delete
$context->remove('key1');
unset($context->key1);
unset($context['key1']);

// Delete (extended)
$context->replace([
    'key4', 'value4',
    'key5', 'value5',
]); // replaces all existing elements with specified elements
$context->clear(); // deletes all elements

// Check
if ($context->has('key1')) {}
if (isset($context->key1)) {}
if (isset($context['key1'])) {}

// Iterate
foreach ($context as $key => $value) {}
foreach ($context->getIterator() as $key => $value) {}

// Special
$context->add('key6', 'value1'); // element key6 is string
$context->add('key6', 'value2'); // key6 is converted to array automatically
$keyArray = $context->keys(); // new numeric array containing keys only
$valueArray = $context->values(); // new numeric array containing values only

Ansas\Component\Convert\ConvertToNull

This trait can be used to "sanitize values" by setting empty values to null., (*8)

It holds methods which can be called in order to check if a field is empty and set it to "null" before calling the parent setter method. By doing this we have a more cleaned up object and also prevent e. g. the "versionable" behavior from adding a new version (as "" !== null)., (*9)

Methods:, (*10)

protected function convertEmptyToNull($value, array $considerNull = [''])
protected function convertToNull($value, array $considerNull, $trim = false)
protected function trimAndConvertEmptyToNull($value, array $considerNull = [''])
protected function trimAndConvertToNull($value, array $considerNull)

Ansas\Component\Session\ThriftyFileSession

All you need is to configure the native PHP session settings as usual (see http://php.net/manual/de/session.configuration.php)., (*11)

After that load this class and call the static init() method: Ansas\Component\Session\ThriftyFileSession::init();, (*12)

This will automatically start the session and you can use the native session handling functions and the super global variable $_SESSION as usual., (*13)

The benefit of this class is that session storage on disk and session cookies are set only if $_SESSION has data. Also the session cookie will also be updated with every request, so it will not expire before session.gc_maxlifetime and the cookie will be deleted automatically if you destroy the session (both not the case with pure native PHP sessions)., (*14)

Methods (without internal):, (*15)

public static function getInstance()
public static function init()
public static function force(boolean $force)
public static function ttl(int $ttl)
public static function cleanup(callable $cleanup)
public static function kill()

Ansas\Monolog\Profiler

A small profiler (stop watch) for different profiles that are logged to any Monolog logger., (*16)

Methods (without internal):, (*17)

// object handling methods
public function __construct(Logger $logger, $level = Logger::DEBUG, callable $formatter = null)
public function __destruct()
public function __get($profile)
public function __toString()

// profile methods
public function add($profile)
public function context(Collection $context = null)
public function get($profile)
public function has($profile)
public function remove($profile)
public function removeAll()
public function set($profile)

// stopwatch methods
public function start($message = 'Start', $context = [])
public function start($context = [])
public function startAll($message = 'Start', $context = [])
public function lap($message = 'Lap', $context = [])
public function lap($context = [])
public function stop($message = 'Stop', $context = [])
public function stop($context = [])
public function stopAll($message = 'Stop', $context = [])
public function note($message = 'Note', $context = [])
public function note($context = [])
public function clear()
public function clearAll()
public function restart($message = 'Restart', $context = [])
public function restart($context = [])

// setter methods
public function setLogger(Logger $logger)
public function setLevel($level)
public function setFormatter(callable $formatter = null)

// time methods
public function timeCurrent()
public function timeStart()
public function timeStop()
public function timeTotal()
public function timeLap($lap = -1)

// default methods
public function defaultFormatter()

// helper methods
public function countLaps()
public function getFormatter()
public function getLaps()
public function getName()
public function getProfiles()
public function isRunning()
public function isStarted()
public function isStopped()

Usage:, (*18)

use Ansas\Monolog\Profiler;

$profiler = new Profiler($logger);

// Starts default profile (must be started before any other profiles)
$profiler->start();

sleep(1);

// Adds profile "testA" and logs default message "Start"
$profiler->add("testA")->start(); 

sleep(1);

// Gets profile "testA" and adds a lap incl. logging it with default message 
// "Lap"
$profiler->get("testA")->lap();

// Adds profile "anotherB" (add always returns new new profile)
$anotherB = $profiler->add("anotherB");

// Starts profile "anotherB" with individual message "B is rolling"
$anotherB->start("B is rolling");

sleep(1);

// Add lap for default profile with individual message
$profiler->lap("Lap for main / default profile");

// Stop profile "anotherB" and log with default message "Stop"
$anotherB->stop();

// Add subprofile "moreC" to profile "anotherB" and start with individual 
// message and context array (default Monolog / PSR3 signature)
$moreC = $anotherB->add("moreC")->start("Message", ['key' => 1]);

// Add profile "lastD"  to "anotherB" as well
$lastD = $profiler->get("anotherB")->add("lastD");

// Just log a note to profile "lastD"
$lastD->note("Starting soon");
sleep(1);

// Clear only profile "anotherB" and start it again
$anotherB->clear()->start("Restarting after clear");

// Stopping "testA" (will not be restarted with upcomming "startAll")
$testA = $profiler->get("testA");
$testA->stop();

// Just add a profile without doing anything (will be started with startAll)
$profiler->add("test123");

// Start all profiles incl. default that are not startet
$profiler->startAll("Starting all profiles not started yet");

// Add "veryLastE" to "lastD" but do not log it (as message is null)
$veryLastE = $lastD->add("veryLastE")->start(null);

sleep(1);

// Some more profiling
$profiler->get("anotherB")->get("moreC")->lap("Lapdance ;P");
$veryLastE->stop("Stopping E");

// Make things easier, just create profiles via set() or magic method __get()
$profiler->set("profilX")->start();
$profiler->set("profilX")->lap();
$profiler->profilX->lap();
$profiler->profilX->stop();

sleep(1);

// Clear "lastD" all subprofiles (no logging will be done now or on exit)
$lastD->clearAll("Clearing D and all subprofiles");

// Stops all counters (done automatically on script end)
$anotherB->stopAll();

// Display current profiling status / report
echo $profiler;

// All running profiles are lapped (if needed), stopped and logged if profiler
// is destroyed or program ends
$profiler = null;
gc_collect_cycles();
sleep(3);

exit;

Ansas\Monolog\Processor\ConsoleColorProcessor

Adds colors to Monolog for console output via Processor. The $record parts level_name and message are colored by this processor, (*19)

Usage:, (*20)

use Ansas\Monolog\Processor\ConsoleColorProcessor;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Formatter\LineFormatter;

$loggerFormat   = "[%datetime%] %level_name% %message% %context%\n";
$loggerLevel    = getenv('DEBUG') ? Logger::DEBUG : Logger::NOTICE;
$loggerTimeZone = new DateTimeZone('Europe/Berlin');

$formatter = new LineFormatter($loggerFormat, $loggerTimeFormat);
$formatter->ignoreEmptyContextAndExtra(true);

$defaultHandler = new StreamHandler('php://stdout', $loggerLevel, $bubble = false);
$defaultHandler->setFormatter($formatter);

$errorHandler = new StreamHandler('php://stderr', Logger::ERROR, $bubble = false);
$errorHandler->setFormatter($formatter);

$logger = new Logger('console');
$logger->pushHandler($defaultHandler);
$logger->pushHandler($errorHandler);
$logger->pushProcessor(new ConsoleColorProcessor());
$logger->useMicrosecondTimestamps(true);

$logger->debug(str_repeat("Xx ", rand(5, 40)));
$logger->info(str_repeat("Xx ", rand(5, 40)));
$logger->notice(str_repeat("Xx ", rand(5, 40)));
$logger->warning(str_repeat("Xx ", rand(5, 40)));
$logger->error(str_repeat("Xx ", rand(5, 40)));
$logger->critical(str_repeat("Xx ", rand(5, 40)));
$logger->alert(str_repeat("Xx ", rand(5, 40)));
$logger->emergency(str_repeat("Xx ", rand(5, 40)));

TODO

  • Write tests

Contribute

Everybody can contribute to this package. Just:, (*21)

  1. fork it,
  2. make your changes and
  3. send a pull request.

Please make sure to follow PSR-1 and PSR-2 coding conventions., (*22)

License

__MIT license__ (see the LICENSE file for more information)., (*23)

The Versions

27/07 2018

dev-master

9999999-dev

PHP components - collection of cross-project PHP classes

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

file component php monolog slim convert session format profiler

03/03 2018

dev-develop

dev-develop

PHP components - collection of cross-project PHP classes

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

file component php monolog slim convert session format profiler

03/03 2018

1.2.7

1.2.7.0

PHP components - collection of cross-project PHP classes

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

file component php monolog slim convert session format profiler

05/01 2018

1.2.6

1.2.6.0

PHP components - collection of cross-project PHP classes

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

file component php monolog slim convert session format profiler

09/12 2017

1.2.5

1.2.5.0

PHP components - collection of cross-project PHP classes

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

file component php monolog slim convert session format profiler

17/09 2017

1.2.4

1.2.4.0

PHP components - collection of cross-project PHP classes

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

file component php monolog slim convert session format profiler

27/07 2017

1.2.3

1.2.3.0

PHP components - collection of cross-project PHP classes

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

file component php monolog slim convert session format profiler

17/07 2017

1.2.2

1.2.2.0

PHP components - collection of cross-project PHP classes

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

file component php monolog slim convert session format profiler

27/06 2017

1.2.1

1.2.1.0

PHP components - collection of cross-project PHP classes

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

file component php monolog slim convert session format profiler

06/06 2017

1.2.0

1.2.0.0

PHP components - collection of cross-project PHP classes

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

file component php monolog slim convert session format profiler

19/05 2017

1.1.14

1.1.14.0

PHP components - collection of cross-project PHP classes

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

file component php monolog slim convert session format profiler

03/04 2017

1.1.13

1.1.13.0

PHP components - collection of cross-project PHP classes

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

file component php monolog slim convert session format profiler

20/02 2017

1.1.12

1.1.12.0

PHP components - collection of cross-project PHP classes

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

file component php monolog slim convert session format profiler

31/01 2017

1.1.11

1.1.11.0

PHP components - collection of cross-project PHP classes

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

file component php monolog slim convert session format profiler

31/12 2016

1.1.10

1.1.10.0

PHP components - collection of cross-project PHP classes

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

file component php monolog slim convert session format profiler

29/12 2016

1.1.9

1.1.9.0

PHP components - collection of cross-project PHP classes

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

file component php monolog slim convert session format profiler

29/12 2016

1.1.8

1.1.8.0

PHP components - collection of cross-project PHP classes

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

file component php monolog slim convert session format profiler

22/11 2016

1.1.7

1.1.7.0

PHP components - collection of cross-project PHP classes

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

file component php monolog slim convert session format profiler

01/10 2016

1.1.6

1.1.6.0

PHP components - collection of cross-project PHP classes

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

file component php monolog slim convert session format profiler

25/09 2016

1.1.5

1.1.5.0

PHP components - collection of cross-project PHP classes

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

file component php monolog slim convert session format profiler

19/09 2016

1.1.4

1.1.4.0

PHP components - collection of cross-project PHP classes

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

file component php monolog slim convert session format profiler

08/09 2016

1.1.3

1.1.3.0

PHP components - collection of cross-project PHP classes

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

file component php monolog slim convert session format profiler

05/09 2016

1.1.2

1.1.2.0

PHP components - collection of cross-project PHP classes

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

file component php monolog slim convert session format profiler

02/09 2016

1.1.1

1.1.1.0

PHP components - collection of cross-project PHP classes

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

file component php monolog slim convert session format profiler

02/09 2016

1.1.0

1.1.0.0

PHP components - collection of cross-project PHP classes

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

file component php monolog slim convert session format profiler

29/08 2016

1.0.9

1.0.9.0

PHP components - collection of cross-project PHP classes

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

file component php monolog slim convert session format profiler

25/08 2016

1.0.8

1.0.8.0

Collection of cross-project PHP classes

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

file component php monolog convert session format profiler

13/08 2016

1.0.7

1.0.7.0

Collection of cross-project PHP classes

  Sources   Download

MIT

The Requires

  • php >=5.4

 

file component php monolog convert session format profiler

09/08 2016

1.0.6

1.0.6.0

Collection of cross-project PHP classes

  Sources   Download

MIT

The Requires

  • php >=5.4

 

file component php monolog convert session format profiler

28/07 2016

1.0.5

1.0.5.0

Collection of cross-project PHP classes

  Sources   Download

MIT

The Requires

  • php >=5.4

 

file component php monolog convert session format profiler

27/07 2016

1.0.4

1.0.4.0

Collection of cross-project PHP classes

  Sources   Download

MIT

The Requires

  • php >=5.4

 

file component php monolog convert session format profiler

26/07 2016

1.0.3

1.0.3.0

Collection of cross-project PHP classes

  Sources   Download

MIT

The Requires

  • php >=5.4

 

file component php monolog convert session format profiler

21/06 2016

1.0.2

1.0.2.0

Collection of cross-project PHP classes

  Sources   Download

MIT

The Requires

  • php >=5.4

 

file component php convert session format

19/05 2016

1.0.1

1.0.1.0

Collection of cross-project PHP classes

  Sources   Download

MIT

The Requires

  • php >=5.4

 

file component php convert session format

29/04 2016

1.0.0

1.0.0.0

Collection of cross-project PHP classes

  Sources   Download

MIT

The Requires

  • php >=5.4

 

file component php convert session format