PHP Classes

SouthCoast PHP Helpers Classes: General purpose classes to help PHP development

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2025-07-28 (-5 hours ago) RSS 2.0 feedNot yet rated by the usersTotal: 77 All time: 10,162 This week: 560Up
Version License PHP version Categories
southcoast-helpers 1.0.1The PHP License7PHP 5, Tools
Description 

Author

This package provides general purpose classes to help PHP development.

It comes with a collection of classes that can be used to help developers solve common Web development problems. Currently it provide helpers for:

- Manipulating arrays
- Logging actions
- Display messages in a console
- Load environment variables in a console
- Generate unique identifiers
- Validate and process JSON encode variable values
- Process hashed values
- Validate string values of several types
- Working with files on the local filesystem

Picture of Corné de Jong
  Performance   Level  
Name: Corné de Jong <contact>
Classes: 1 package by
Country: The Netherlands The Netherlands
Age: ???
All time rank: 4455105 in The Netherlands The Netherlands
Week rank: 195 Up4 in The Netherlands The Netherlands Up

Example

<?php

error_reporting
(E_ALL);
ini_set('display_errors', true);

require
realpath('../vendor/autoload.php');

use
SouthCoast\Helpers\File;

File::setBaseDirectory(__DIR__);
// File::defineDirectory('dir_identifier', '/Path/To/Dir');

var_dump(File::getCsv('test.csv', false, ';'));

die();

File::list('$dir_identifier');

File::list(File::BASE_DIRECTORY, 'txt');

/* Save file to defined directory */
File::save('$dir_identifier/thisname.tmp', $data);

File::get('$dir_identifier/thisname.tmp');

/* Save file to base directory as Json */
File::saveJson('thisname', $data, File::Minified);

File::getJson('thisname', true);

File::saveCsv('thiscsv', $data, true || []);

File::getCsv('thiscsv', false);

/* Open a Write Stream to a file */
$stream = File::writeStream('anothername.txt');
/* Loop over all lines in a file */
foreach (File::getAsArray('thisname.txt') as $line) {
   
$stream->write($line);
}
/* Close the stream */
$stream->close();

/* Open a Write Stream to a file */
$stream = File::writeStream('anothername.txt');

while (
$stream->hasContent()) {
   
/* Optionally you can pass the amount of bites */
   
processContent($stream->read());
}

/* Close the stream, or just unset it */
$stream->close();
unset(
$stream);

/* Move a file to another directory */
File::move('$dir_identifier/thisname.tmp', '$another_defined_dir');
/* Rename a file */
File::rename('$dir_identifier/thisname.tmp', 'thisnewname');

/* Get File Extention */
File::getExtension('thisname.txt');
/* Get mime type */
File::getMimeType('thisname');

File::delete('thisname');

File::describe('thisname');

File::getDirectories();

File::getPath('thisname.json');


Details

SouthCoast | Helpers

Codacy Badge

A Collection of helper classes for PHP

Could be installed via composer:

$ composer require southcoast/helpers:dev-master

Or by manually downloading the .zip file.

Array Helper

`ArrayHelper::Map(array $map, array $array): array;`

$map        array   The mapping 
$array      array   The original array where data should come from
Returns     array   The mapped array

This method allows you to map an existing array to a new one. It has support for dot notation for use of multidimensional arrays. Both for the 'field' and the mapping keys

Accepted keys in the mapping array:

[
    'field' => 'The key to the field from the original $array',
    'alt_field' => 'An alternative key in the original $array for when the primary field is not found or returns null',
    'value' => 'A static value, or mutation of the value, This will over ride the value of the primary field',
    'or' => 'A static value that should be used',
    'add' => 'If the field should be used or not, accepts true or false values'
]

Mapping Example:

$map = [
    // The Key is the to be used key for the Array
    // The value of 'field' is the value origin
    'New_Name' => ['field' => 'old_name'],

    // Add 'value' to add custom value or value mutation
    'Email' => ['value' => 'Some Other Value'],
    
    // Add '.' separators for sub objects
    'Email.primary' => ['field' => 'email'],
    
    // Use '0' for arrays
    'Addresses.0.street' => ['field' => 'address_1_line_1'],
    
    // Get a value from a multidimensional source
    'isDefault' => ['field' => 'meta.system.default'],
    
    // Add the 'or' field to supply a value that will used if the value from the original array is not found or null
    'automated' => ['field' => 'system.automated', 'or' => 'nope, not automated']
    
    // Use the 'add' field to specify if this field should be added
    'someAwesomeField' => ['field' => 'getMyValue', 'add' => false] // Wont be added
    'someAwesomeField' => ['field' => 'getMyValue', 'add' => true] // Will be added
    
    // Add an alternative field to the mapping if the original field is missing or returned null
    'arbitraryKey' => ['field' => 'getItFromHere', 'alt_field' => 'or_from_here', 'or' => 'a default value']
];

Example:

$array = [
    'key_1' => 'value_1',
    'key_2' => 'value_2',
    'array' => [
        'a_key_1' => 'a_value_1',
        'a_key_2' => 'a_value_2',
        'a_key_3' => null
    ]
];

$map = [
    'field_one' => ['field' => 'array.a_key_1'],
    'field_two' => ['field' => 'key_1', 'add' => false],
    'field_three' => ['value' => 'new_value'],
    'field_four' => ['field' => 'array.a_key_5', 'alt_field' => 'key_2'],
    'field_five' => 'This Value',
    'array.a_field' => ['field' => 'array.a_key_3', 'or' => 'nope, no value'],
    'array.0' => ['field' => 'array.a_key_2']
];

$result = ArrayHelper::map($map, $array);

$result = [
    /The key 'field_one' had the value of array['a_key_1']/
    'field_one' => 'a_value_1',
    /The key 'field_two' was not added, because 'add' was false/
    /The key 'field_three' has a custom value/
    'field_three' => 'new_value',
    /The key 'field_four' now bears the value of 'key_2' because 'array.a_key_5' could not be found/
    'field_four' => 'value_2',
    /The key 'field_five' also carries a custom value/
    'field_five' => 'This Value',
    /The key 'array' now contains an array/
    'array' => [
        /With a key 'a_field' with the 'or' value because 'array.a_key_3' has a null value/
        'a_field' => 'nope, no value',
        /Also a numeric key was added with the value of 'array.a_key_2'/
        0 => 'a_value_2'
    ]
]

Environment

Create a file called 'sc.env'. This file should contain the following structure:

{
    "dev": true,
    "machine": "<Machine ID/Developer ID>",
    "...": "Any other parameters you'd like to add to your environment"
}

In your main php file load the env file.

$path_to_env = './sc.env';
Env::load($path_to_env);

  Files folder image Files (28)  
File Role Description
Files folder imageExamples (4 files)
Files folder imagesrc (1 directory)
Files folder imagevendor (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (28)  /  Examples  
File Role Description
  Accessible without login Plain text file ArrayHelper.php Aux. Auxiliary script
  Accessible without login Plain text file Dev.php Aux. Auxiliary script
  Accessible without login Plain text file Env.php Aux. Auxiliary script
  Accessible without login Plain text file File.php Example Example script

  Files folder image Files (28)  /  src  
File Role Description
Files folder imagehelpers (14 files, 2 directories)

  Files folder image Files (28)  /  src  /  helpers  
File Role Description
Files folder imageError (1 file)
Files folder imageObjects (6 files)
  Plain text file AddressHelper.php Class Class source
  Plain text file ArrayHelper.php Class Class source
  Plain text file Date.php Class Class source
  Plain text file Dev.php Class Class source
  Plain text file Env.php Class Class source
  Accessible without login Plain text file File.php Aux. Auxiliary script
  Plain text file Fridge.php Class Class source
  Plain text file Identifier.php Class Class source
  Plain text file Json.php Class Class source
  Plain text file Number.php Class Class source
  Plain text file Spice.php Class Class source
  Plain text file StringHelper.php Class Class source
  Plain text file Validate.php Class Class source
  Plain text file Xml.php Class Class source

  Files folder image Files (28)  /  src  /  helpers  /  Error  
File Role Description
  Plain text file FileError.php Class Class source

  Files folder image Files (28)  /  src  /  helpers  /  Objects  
File Role Description
  Plain text file Collection.php Class Class source
  Plain text file IteratorObject.php Class Class source
  Accessible without login Plain text file Observer.php Data Auxiliary data
  Plain text file Stream.php Class Class source
  Accessible without login Plain text file Subject.php Data Auxiliary data
  Plain text file XmlObject.php Class Class source

  Files folder image Files (28)  /  vendor  
File Role Description
  Accessible without login Plain text file autoload.php Aux. Configuration script

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:77
This week:0
All time:10,162
This week:560Up