Skip to content

Instantly share code, notes, and snippets.

@jonathonbyrdziak
Created August 18, 2010 20:24
Show Gist options
  • Save jonathonbyrdziak/536059 to your computer and use it in GitHub Desktop.
Save jonathonbyrdziak/536059 to your computer and use it in GitHub Desktop.
Parse Phone Number :: This function does some basic telephone parsing so that we can mask the numbers appropriately.
/**
* @author Jonathon Byrd
* @link www.jonathonbyrd.com
*
*
*/
class ObjectBase
{
/**
* Variable contains the error messages that the child model object has encountered.
*
* @var array
*/
var $_errorMsgs = array();
/**
* Binds a named array/hash to this object
*
* Can be overloaded/supplemented by the child class
*
* @access public
* @param $from mixed An associative array or object
* @param $ignore mixed An array or space separated list of fields not to bind
* @return boolean
*/
function bind( $from, $ignore=array(), $public = false )
{
$fromArray = is_array( $from );
$fromObject = is_object( $from );
if (!$fromArray && !$fromObject)
{
trigger_error( get_class( $this ).'::bind failed. Invalid from argument' );
return false;
}
if (!is_array( $ignore )) {
$ignore = explode( ' ', $ignore );
}
if ($fromArray) $from = array_change_key_case($from, CASE_LOWER);
foreach ($this->getProperties($public) as $k => $v)
{
// internal attributes of an object are ignored
if (!in_array( $k, $ignore ))
{
if ($fromArray && isset( $from[$k] ))
{
$this->$k = $from[$k];
}
else if ($fromObject && isset( $from->$k ))
{
$this->$k = $from->$k;
}
}
}
return true;
}
/**
* Create Camel Case
*
* Method is responsible for creating a camel case from the string given.
*
* @param $string
*/
function createCamel( $string = null )
{
//reasons to fail
if (is_null($string)) return false;
$string = ereg_replace("[^A-Za-z0-9 _]", '', $string);
return str_replace(" ","", ucwords(strtolower(str_replace("_", " ", $string))));
}
/**
* Magic Call Method
*
* Method allows us to target all object properties through individual methods
* allowing us to easily override the default method properties throughout the system
* by easily adding the override and without changing every method caller
*
* @param $method
* @param $args
*/
private function __call($method, $args)
{
//initializing variables
$switch = substr($method,0,3);
$getproperty = substr($method,3);
$property = $method;
//allows use to determine what to do using the first three characters of the method call
switch($switch)
{
case 'get':
if (isset($this->$getproperty)) return $this->$getproperty;
break;
default:
if (isset($this->$property)) return $this->$property;
break;
}
return false;
}
/**
* Fire this Method
*
* Method will determine if the requested method exists, and fire it
* returning a consistent boolean result or the actual result
*
* @param $method
* @param $args
* @return boolean
*/
function fireMethod( $method = null, $args = null )
{
//reasons to fail
if (!method_exists($this, $method)) return false;
//run the method
$result = $this->$method( $args );
//making the results consistent
if (is_null($result)) return false;
if (!$result) return false;
return $result;
}
/**
* Returns a property of the object or the default value if the property is not set.
*
* @access public
* @param string $property The name of the property
* @param mixed $default The default value
* @return mixed The value of the property
* @see getProperties()
*
*/
function get( $property, $default = null )
{
//initializing variables
$result = false;
if ( !($result = $this->fireMethod( $property, $default )) && isset($this->$property) )
{
$result = $this->$property;
}
if($result)
return $result;
return $default;
}
/**
* Get Model Errors
*
* Method will return a false if the error array is empty or will return
* the error messages.
*
* @return string
*/
function getErrors()
{
//reasons to fail
if (!isset($this->_errorMsgs)) return false;
if (empty($this->_errorMsgs)) return false;
return $this->_errorMsgs;
}
/**
* Get Instance
*
* Method returns an instance of the proper class and its variable set
*
* @param $class string
*/
public static function &getInstance( $class, $options = null )
{
//intialize variables
static $instance;
$appendix = "vendor";
if (is_null($instance))
{
$instance = array();
}
//create the class if it does not exist
if (!isset($instance[$class]))
{
//creating the instance
//initializing variables
$class = parent::createCamel($class."_".$appendix);
s_autoload($class.$appendix);
$instance[$class] = new $class($class, $options);
}
//return an instance of this instantiation
return $instance[$class];
}
/**
* Get this Class Methods
*
* Method will create an array of this classes methods, allowing
* the programmer to filter out unwanted methods from the array.
*
* @param $prefix
* @param $private
*/
function getMethods( $prefix = null, $private = false )
{
//initializing variables
$methods = get_class_methods($this);
if (!is_null($prefix))
{
$prelen = strlen($prefix);
if (substr($prefix,0,1) == '_') $private = true;
}
foreach ($methods as $key => $method)
{
//remove the private methods
if (!$private)
{
if (substr($method,0,1) == '_') unset($methods[$key]);
}
//remove the methods that are not prefixed properly
if (!is_null($prefix))
{
if (substr($method,0,$prelen) != $prefix) unset($methods[$key]);
}
}
return $methods;
}
/**
* Returns an associative array of object properties
*
* @access public
* @param boolean $public If true, returns only the public properties
* @return array
* @see get()
*
*/
function getProperties( $public = true )
{
$vars = get_object_vars($this);
if($public)
{
foreach ($vars as $key => $value)
{
if ('_' == substr($key, 0, 1)) {
unset($vars[$key]);
}
}
}
return $vars;
}
/**
* Is this property set?
*
* Method will check the value for
*
* @param $property
* @return boolean
*/
function _isset( $property = null )
{
//reasons to fail
if (!($this->$property)) return false;
if (is_object($this->$property)) return true;
if (is_array($this->$property) && empty($this->$property)) return false;
if (strlen(trim($this->$property)) < 1) return false;
return true;
}
/**
* Is this Valid
*
* Method will search for all of the _valid methods and then loop
* through each of them, returning true only if all methods return
* true.
*
* @return boolean
*/
public function isValid( $method_prefix = '_valid' )
{
//initializing variables
$validation_methods = $this->getMethods( $method_prefix );
$valid = true;
//checking the boolean response from each method
//once false, it cannot be set to true
foreach ($validation_methods as $method)
{
if ($valid && !$this->$method()) $valid = false;
}
return $valid;
}
/**
* Modifies a property of the object, creating it if it does not already exist.
*
* @access public
* @param string $property The name of the property
* @param mixed $value The value of the property to set
* @return mixed Previous value of the property
* @see setProperties()
* @since 1.5
*/
function set( $property, $value = null )
{
$previous = isset($this->$property) ? $this->$property : null;
$this->$property = $value;
return $previous;
}
/**
* Set Error
*
* Method records all of the errors that this table model has encoutered.
*
* @param $error
*/
function setError( $error = "" )
{
//reasons to fail
if (trim($error) == "") return false;
//initializing variables
if (!isset($this->_errorMsgs))
{
$this->_errorMsgs = array();
}
$this->_errorMsgs[] = $error;
return true;
}
/**
* Set the object properties based on a named array/hash
*
* @access protected
* @param $array mixed Either and associative array or another object
* @return boolean
* @see set()
*/
function setProperties( $properties )
{
$properties = (array) $properties; //cast to an array
if (is_array($properties))
{
foreach ($properties as $k => $v) {
$this->$k = $v;
}
return true;
}
return false;
}
}
/**
* @author Jonathon Byrd
* @desc This class has the responsibility of managing phone numbers.
* It will clean the phone number and be able to tell us everything
* that we want to know about that number, as well as returning it in any format
* that we desire
*
*/
class PhoneNumber extends ObjectBase
{
/**
* Property contains the phone number value as a numeric only string
*
* @var string
*/
var $_number = null;
/**
* This value is normally 1
*
* @var string
*/
var $_country = null;
/**
* Contains the three digit area code that follows the country code
*
* @var string
*/
var $_area = null;
/**
* Contains the local seven digit phone number
*
* @var string
*/
var $_local = null;
/**
* Parses the number into the class properties
*
* @param $number
*/
function __construct( $number = null )
{
//reasons to fail
if (is_null($number)) return false;
if (strlen(trim($number)) < 2) return false;
//getting to work
$this->_parse( $number );
}
/**
* Parse the number
*
* Method will break down the number and properly set the
* class properties
*
* @param string $number
* @return boolean
*/
private function _parse( $number )
{
//initializing variables
$patterns = array("/[^0-9]/");
//cleaning the number
$number = preg_replace($patterns, "", $number);
$number = (float)trim($number);
switch(strlen($number))
{
case 11:
$country_code = substr($number,0,1);
$area_code = substr($number,1,4);
$local_code = substr($number,4,11);
break;
case 10:
$country_code = 1;
$area_code = substr($number,0,3);
$local_code = substr($number,3,10);
break;
case 7:
$country_code = 1;
$area_code = 000;
$local_code = substr($number,0,7);
break;
default:
$country_code = 1;
$area_code = 000;
$local_code = $number;
break;
}
//initializing properties
$this->_number = $number;
$this->_country = $country_code;
$this->_area = $area_code;
$this->_local = $local_code;
return true;
}
/**
* Clean the number
*
* Method will clean the number and return it or a boolean value
*
* @param $number
* @return string
*/
private function _clean( $number = null )
{
//initializing variables
$patterns = array("/[^0-9]/");
//cleaning the number
$number = preg_replace($patterns, "", $number);
$number = (float)trim($number);
//reasons to fail
if (is_null($number)) return false;
if ($number < 1) return false;
return $number;
}
/**
* Get a Property
*
* Method will ensure that the property is valid before
* returning it as a string. Otherwise will return false
*
* @param $property
* @return string
*/
function _get( $property = null, $default = false )
{
//reasons to fail
if (is_null($property)) return $default;
if (!isset($this->$property)) return $default;
if (is_null($this->$property)) return $default;
if (strlen($this->$property) < 1) return $default;
if ((float)trim($this->$property) < 1) return $default;
return $this->$property;
}
/**
* Get the Country Code
*
* Method will return the country code if it is a valid code.
* Otherwise the method will return false
*
* @param $default
* @return string
*/
public function getCountryCode( $default = false )
{
return $this->_get('_country', $default);
}
/**
* Get the Area Code
*
* Method will return the area code if it is a valid code.
* Otherwise the method will return false
*
* @param $default
* @return string
*/
public function getAreaCode( $default = false )
{
return $this->_get('_area', $default);
}
/**
* Get the Local Phone Code
*
* Method will return a clean local phone number
* Otherwise returning false or defailt
*
* @param $default
* @return string
*/
public function getLocalCode( $default = false )
{
return $this->_get('_local', $default);
}
/**
* Set the Country Code
*
* Method will set the country code, so long as we have a valid
* code to set it with.
*
* @param string $code
*/
public function setCountryCode( $code = null )
{
//clean the input
if (!($code = $this->_clean($code))) return false;
//setting properties
$this->_country = $code;
return true;
}
/**
* Set the Area Code
*
* Method will set the area code, so long as we have a valid
* code to set it with.
*
* @param string $code
*/
public function setAreaCode( $code = null )
{
//clean the input
if (!($code = $this->_clean($code))) return false;
if (strlen($code) < 3) return false;
//setting properties
$this->_area = $code;
return true;
}
/**
* Set the Local Code
*
* Method will set the local code, so long as we have a valid code
* to set it with
*
* @param string $code
*/
public function setLocalCode( $code = null )
{
//clean the input
if (!($code = $this->_clean($code))) return false;
if (strlen($code) < 7) return false;
//setting properties
$this->_local = $code;
return true;
}
/**
* Is This Number Valid
*
* Method will determine if this number has at least
* a valid local code
*
* @return boolean
*/
public function isValid()
{
//reasons to fail
if (is_null($this->_number)) return false;
if (strlen($this->_local) < 7) return false;
return true;
}
/**
* Return With Mask
*
* Method will return the phone number with a valid mask
*
* @return string
*/
public function toMask()
{
if (is_null($this->_number)) return '';
//breaking down the local number
$three = substr($this->_local,0,3);
$four = substr($this->_local,3,7);
return (string)$this->_country.'('.$this->_area.')'.$three.'-'.$four;
}
/**
* Return as a clean number
*
* Method will return the phone number without any masking
*
* @return string
*/
private function __toString()
{
if (is_null($this->_number)) return '';
return (string)$this->_number;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment