mynt / utilities
Mynt Utilities: Collection of classes meant to aid you in web development by solving some common problems.
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2016-10-16 06:26:13 UTC
README
Mynt Utilities
This project is a personal repository of tools I use when developing applications. It is in early stages of development and API changes can be introduced at any time, usually breaking BC.
Mynt\Utility\Url
This namespace hosts URL related utilities.
Parsing, representing, and manipulating URLs
Mynt URL is a simple OOP approach to creating or manipulating URL strings.
<?php // construct a URL $url = new Url(); $url->setHostname('google.com'); $url->setPathParts(array('channel67', 'b', 89009)); $this->assertEquals('http://google.com/channel67/b/89009', $url->getUrl()); $url = new Url(); $url->setProtocol('https'); $url->setHostname('google.com'); $url->setPath('search/query/'); $url->setUser('tickner'); $url->setPass('chris'); $url->setQuery('test=no&this=bar'); $url->setPort(9000); $this->assertEquals('https://tickner:chris@google.com:9000/search/query?test=no&this=bar', $url->getUrl());
It also lets you:
- getPathParts - An array of the path (useful for routing, etc)
- setPathParts - set the path with an array
- getQueryAsArray - A key/value array of the URL query string
- setQueryFromArray - Use a key/value array to set the query string of a URL
Mynt\Utility\DateTime
This namespace hosts DateTime related utilites.
Interval DateTime Data Series'
This is very new and needs some refactoring. The IntervalDataSeries only calculates averages of the data around a particular interval. Most use cases would benefit from a cumulative (additive) strategy rather than an average
Often when graphing or working with statistics we are presented with a data source that has some data associated with a specific DateTime, and are required to process that data in some way based on a specific interval. For example, calculating sales/hour, or graphing webpage hits. If we want to do this processing in PHP, the IntervalDataSeries might be of help.
// we have some data and want an array with the average data at each minute 6:00, 6:01, and 6:02 $start = new \DateTime('Oct 8, 1987 6:00pm'); $end = new \DateTime('Oct 8, 1987 6:02pm'); $window = new \DateInterval('PT30S'); // 30 second window +/-, 1 minute interval // we construct the interval series and add all of our data from the data source, they happen at any random DateTime $series = new IntervalDataSeries($start, $end, $window); $series->addData(new \DateTime('Oct 8, 1987 6:00:04pm'), 1); $series->addData(new \DateTime('Oct 8, 1987 6:00:29pm'), 5); $series->addData(new \DateTime('Oct 8, 1987 6:00:29pm'), 12); $series->addData(new \DateTime('Oct 8, 1987 6:00:45pm'), 10); $series->addData(new \DateTime('Oct 8, 1987 6:01:07pm'), 30); $series->addData(new \DateTime('Oct 8, 1987 6:01:49pm'), 1); $series->addData(new \DateTime('Oct 8, 1987 6:02:04pm'), 1); // when we call toArray on the series, we receive our averaged data list (based on start, end, and an interval) $expected = array( '1987-10-08 18:00:00' => 6, '1987-10-08 18:01:00' => 20, '1987-10-08 18:02:00' => 1, ); $series = $series->toArray('Y-m-d H:i:s'); $this->assertEquals($expected, $series);
If you want to make it continuous data (ie, no zeros between intervals if the interval is smaller than the data frequency), see the ContinuousIntervalDataSeries class.
To Do
[ ] Make more flexible so that we can have various strategies. Right now we only have an average strategy. [ ] When above is done, add a cumulative strategy that adds all data within the window (this would be very useful))
DateHelper
Mynt DateHelper is a very simple class, aimed to be a storehouse for static classes that aid in the use of Dates in PHP. Useful for any application that needs to tell users how much time has passed (or into the future) since a DateTime.
DateHelper::agoString($dateTime, $preciseToTheSecond)
Takes a dateTime instance and returns an english string explaining how long ago the DateTime is (or into the future). An optional second parameter $preciseToTheSecond allows you to include seconds (defaults false).
<?php $this->assertEquals( '1 hour, 15 minutes ago', DateHelper::agoString(new \DateTime('-75 minutes -45 seconds')) ); $this->assertEquals( '1 hour, 15 minutes, 45 seconds ago', DateHelper::agoString(new \DateTime('-75 minutes -45 seconds'), true) );
DateHelper::intervalString($startDateTime, $endDateTime, $preciseToTheSecond)
The same as agoString, but allows you to specify the two strings (DateHelper::agoString simply calls this and uses NOW as the second dateTime)
DateHelper::agoInSeconds($startDateTime, $endDateTime)
Will return the approximate difference of two dates in terms of seconds. This is very much in a beta state and should not be trusted for any value over 30 days. If no $startDateTime is passed it will default to the current DateTime.
Mynt\Utility\String
This namespace hosts string related utilities
Working with strings
Mynt String helps work with strings.
String::slugify($string)
Slugify will take a string and make it URL frindly.
<?php $slug = Mynt\String::slugify($string);
String::pluralize($num, $text)
Pluralize will add an 's' to $text if $num is not equal to 1.