PHP Classes

GDM Types: Manipulate scalar data types as objects

Recommend this page to a friend!
  Info   View files Example   View files View files (20)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-01-09 (4 days ago) RSS 2.0 feedNot yet rated by the usersTotal: 90 This week: 1All time: 9,893 This week: 348Up
Version License PHP version Categories
gdm-types 1.0.9BSD License5.3PHP 5, Data types
Description 

Author

This package can manipulate scalar data types as objects.

It provides a set of classes that wrap around several scalar data types so you can perform operations on them like regular objects.

Currently it provides wrapper classes for strings, dates, URLs, as well a wrapper for array elements.

Picture of Corey Sewell
  Performance   Level  
Name: Corey Sewell <contact>
Classes: 1 package by
Country: New Zealand New Zealand

Example

<?php
require './vendor/autoload.php';

function
stringExample()
{
   
$string = "Here is a string that we will use for some examples ";

// Implements __toString, so object instances can be used as plain old strings
   
$echoMe = GDM\Framework\Types\String::create($string);
    echo
$echoMe; // Outputs "Here is a string that we will use for some examples"
   
echo "<br/>";

// Simple example
   
$truncateMe = GDM\Framework\Types\String::create($string);
    echo
$truncateMe->neatTruncate(10); // Outputs "Here is...";
   
echo "<br/>";

// Most methods are Chainable
   
$chainMe = GDM\Framework\Types\String::create($string);
    echo
$chainMe->leftTrim("H")->upperCaseWords()->replace('/String/', 'Sentence'); // Outputs "Ere Is A Sentence That We Will Use For Some Examples "
   
echo "<br/>";
}

function
dateExample()
{
   
$date = "01/01/2014 14:35";

// Set global defaults
   
\GDM\Framework\Types\Settings\DefaultDateSettings::$defaultTimeZone = 'Pacific/Auckland';
    \
GDM\Framework\Types\Settings\DefaultDateSettings::$defaultDateFromat = 'd/m/Y';
    \
GDM\Framework\Types\Settings\DefaultDateSettings::$defaultDateTimeFromat = 'd/m/Y H:i';
    \
GDM\Framework\Types\Settings\DefaultDateSettings::$defaultLongDateFormat = 'l jS \of F Y';
    \
GDM\Framework\Types\Settings\DefaultDateSettings::$defaultLongDateTimeFormat = 'l jS \of F Y h:i:s A';
   
// Or pass a settings object in
   
$settings = GDM\Framework\Types\Settings\DateSettings::create('Pacific/Auckland',
                                                                                                                                     
'd/m/Y', 'd/m/Y H:i',
                                                                                                                                     
'l jS \of F Y',
                                                                                                                                     
'l jS \of F Y',
                                                                                                                                     
'l jS \of F Y h:i:s A');

   
// Implements __toString, so object instances can be used as plain old strings
   
$echoMe = GDM\Framework\Types\Date::create($date, $settings);
    echo
$echoMe; // Outputs "01/01/2014 14:35"
   
echo "<br/>";

   
// Long fromat
   
echo $echoMe->toLongDateTime(); // Outputs "Wednesday 1st of January 2014";
   
echo "<br/>";

   
// Helpers
   
echo GDM\Framework\Types\Date::getDaysDiff("01/01/2014", "21/01/2014"); // Outputs "20"
   
echo "<br/>";

    foreach (
GDM\Framework\Types\Date::daysOfTheWeek() as $day) {
        echo
$day."<br/>";
    }
// Outputs Monday
    // Tuesday
    // Wednesday
    // Thursday
    // Friday
    // Saturday
    // Sunday
}

function
urlExample()
{
   
$urlString = "http://www.example.com/path/to/file?foo=bar";

   
// Implements __toString, so object instances can be used as plain old strings
   
$echoMe = GDM\Framework\Types\Url::create($urlString);
    echo
$echoMe; // Outputs "http://www.example.com/path/to/file?bar=foo"
   
echo "<br/>";

   
// Url parameters are readable as propertie
   
echo $echoMe->foo; // Outputs "bar";
   
echo "<br/>";
   
// You can also set properties
   
$echoMe->bar = "some-new-value";
    echo
$echoMe; // Outputs "http://www.example.com/path/to/file?foo=bar&bar=some-new-value";
   
echo "<br/>";

   
// Path segements are array accessable
   
echo $echoMe[1]; // Outputs "to"
   
echo "<br/>";
   
$echoMe[3] = "somewhere";
    echo
$echoMe; // Outputs "http://www.example.com/path/to/file/somewhere?foo=bar&bar=some-new-value";
   
echo "<br/>";
}
stringExample();
dateExample();
urlExample();


Details

PHP Object orientated types

A collection of helper classes that attempt to make dealing with PHP types a bit more Object Orientated Inspired by https://github.com/alquerci/php-types-autoboxing

Example usage

String

$string = "Here is a string that we will use for some examples        ";

// Implements __toString, so object instances can be used as plain old strings
$echoMe = GDM\Framework\Types\String::create($string);
echo $echoMe; // Outputs "Here is a string that we will use for some examples"

// Simple example
$truncateMe = GDM\Framework\Types\String::create($string);
echo $truncateMe->neatTruncate(10); // Outputs "Here is...";

// Most methods are Chainable
$chainMe = GDM\Framework\Types\String::create($string);
echo $chainMe->leftTrim("H")->upperCaseWords()->replace('/String/', 'Sentence'); // Outputs "Ere Is A Sentence That We Will Use For Some Examples "

Date

$date = "01/01/2014 14:35";

// Set global defaults
\GDM\Framework\Types\Settings\DefaultDateSettings::$defaultTimeZone = 'Pacific/Auckland';
\GDM\Framework\Types\Settings\DefaultDateSettings::$defaultDateFromat = 'd/m/Y';
\GDM\Framework\Types\Settings\DefaultDateSettings::$defaultDateTimeFromat = 'd/m/Y H:i';
\GDM\Framework\Types\Settings\DefaultDateSettings::$defaultLongDateFormat = 'l jS \of F Y';
\GDM\Framework\Types\Settings\DefaultDateSettings::$defaultLongDateTimeFormat = 'l jS \of F Y h:i:s A';
// Or pass a settings object in
$settings = GDM\Framework\Types\Settings\DateSettings::create('Pacific/Auckland', 'd/m/Y', 'd/m/Y H:i', 'l jS \of F Y', 'l jS \of F Y', 'l jS \of F Y h:i:s A');

// Implements __toString, so object instances can be used as plain old strings
$echoMe = GDM\Framework\Types\Date::create($date, $settings);
echo $echoMe; // Outputs "01/01/2014 14:35"

// Long fromat
echo $echoMe->toLongDateTime(); // Outputs "Wednesday 1st of January 2014";

// Helpers
echo GDM\Framework\Types\Date::getDaysDiff("01/01/2014", "21/01/2014");  // Outputs "20"

foreach (GDM\Framework\Types\Date::daysOfTheWeek() as $day) {
            echo $day . "<br/>";
}// Outputs Monday
//      Tuesday
//      Wednesday
//      Thursday
//      Friday
//      Saturday
//      Sunday

URL

$urlString = "http://www.example.com/path/to/file?foo=bar";

// Implements __toString, so object instances can be used as plain old strings
$echoMe = GDM\Framework\Types\Url::create($urlString);
echo $echoMe; // Outputs "http://www.example.com/path/to/file?bar=foo"

// Url parameters are readable as propertie
echo $echoMe->foo; // Outputs "bar";

// You can also set properties
$echoMe->bar = "some-new-value";
echo $echoMe; // Outputs "http://www.example.com/path/to/file?foo=bar&bar=some-new-value";

// Path segements are array accessable
echo $echoMe[1];  // Outputs "to"

$echoMe[3] = "somewhere";
echo $echoMe; // Outputs "http://www.example.com/path/to/file/somewhere?foo=bar&bar=some-new-value";

To do

  • .... write a better readme?
  • Add more types

  Files folder image Files  
File Role Description
Files folder imageGDM (1 directory)
Files folder imagenbproject (2 files)
Files folder imageTests (1 directory)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file examples.php Example Main examples
Accessible without login Plain text file LICENSE Lic. Auxiliary data
Accessible without login Plain text file README.md Doc. Auxiliary data

  Files folder image Files  /  GDM  
File Role Description
Files folder imageFramework (1 directory)

  Files folder image Files  /  GDM  /  Framework  
File Role Description
Files folder imageTypes (6 files, 3 directories)

  Files folder image Files  /  GDM  /  Framework  /  Types  
File Role Description
Files folder imageInterfaces (1 file)
Files folder imageSettings (2 files)
Files folder imageTraits (1 file)
  Plain text file ArrayWrapper.php Class Auxiliary script
  Plain text file Date.php Class Class source
  Plain text file Scalar.php Class Auxiliary script
  Plain text file String.php Class Class source
  Plain text file Type.php Class Auxiliary script
  Plain text file Url.php Class Class source

  Files folder image Files  /  GDM  /  Framework  /  Types  /  Interfaces  
File Role Description
  Plain text file ConvertibleInterface.php Class Auxiliary script

  Files folder image Files  /  GDM  /  Framework  /  Types  /  Settings  
File Role Description
  Plain text file DateSettings.php Class Configuration script
  Plain text file DefaultDateSettings.php Class Configuration script

  Files folder image Files  /  GDM  /  Framework  /  Types  /  Traits  
File Role Description
  Plain text file ScalarTrait.php Class Auxiliary script

  Files folder image Files  /  nbproject  
File Role Description
  Accessible without login Plain text file project.properties Data Auxiliary data
  Accessible without login Plain text file project.xml Data Auxiliary data

  Files folder image Files  /  Tests  
File Role Description
Files folder imageGDM (1 directory)

  Files folder image Files  /  Tests  /  GDM  
File Role Description
Files folder imageFramework (1 directory)

  Files folder image Files  /  Tests  /  GDM  /  Framework  
File Role Description
Files folder imageTypes (4 files)

  Files folder image Files  /  Tests  /  GDM  /  Framework  /  Types  
File Role Description
  Accessible without login Plain text file ArrayWrapperTest.php Test Auxiliary script
  Accessible without login Plain text file DateTest.php Test Auxiliary script
  Accessible without login Plain text file StringTest.php Test Auxiliary script
  Accessible without login Plain text file UrlTest.php Test Auxiliary script

 Version Control Unique User Downloads Download Rankings  
 100%
Total:90
This week:1
All time:9,893
This week:348Up