Skip to content

Instantly share code, notes, and snippets.

@wjn
Last active December 31, 2015 18:18
Show Gist options
  • Save wjn/8025588 to your computer and use it in GitHub Desktop.
Save wjn/8025588 to your computer and use it in GitHub Desktop.
PHP Sample Problem: This application loads a csv datafile from the command line, manipulates the data and provides information based on the ID provided.
<?php
/**
*
* @author "Will Nielsen"
* website: http://willnielsen.com
* Date: December 13, 2013
*/
class Shapes {
/**
* The document root
*/
private $_docRoot = null;
/**
* data file
*/
private $_dataFile;
/**
* The header row from the csv
*/
protected $_headerRow;
/**
* Associative Array containing data found in the
* data.csv file.
*/
protected $_shapes = array();
public function __construct($datafile) {
// sets the doc root for this file
$this -> _setDocroot();
// sets the absolute path for the datafile
$this -> _dataFile = $this -> _docRoot . '/' . $datafile;
// returns an array of shapes found in the datafile (csv)
$this -> _shapes = $this -> _loadCsv();
}
private function _setDocroot() {
$this -> _docRoot = rtrim(dirname(__FILE__), '\\/');
}
private function _loadCsv() {
// Shape Type: array key containing either rectangle or circle
define('SHAPE_ID', 0);
define('SHAPE_TYPE', 1);
define('SHAPE_RADIUS_OR_WIDTH', 2);
define('SHAPE_HEIGHT', 3);
$numlines = count(file($this -> _dataFile));
$delimiter = ',';
$data = array();
if (!file_exists($this -> _dataFile) and !is_readable($this -> _dataFile))
throw new Exception($this->_datafile . " either does not exist or is not readable.", 1);
if (($handle = fopen($this -> _dataFile, 'r')) !== FALSE) {
while (($row = fgetcsv($handle, 1000, $delimiter, '"')) !== FALSE) {
// Check for rectangles
if (strtolower($row[SHAPE_TYPE]) == 'rectangle') {
$data[] = array(
'id' => trim($row[SHAPE_ID]),
'type' => trim($row[SHAPE_TYPE]),
'width' => trim($row[SHAPE_RADIUS_OR_WIDTH]),
'height' => trim($row[SHAPE_HEIGHT]),
'area' => $row[SHAPE_RADIUS_OR_WIDTH] * $row[SHAPE_HEIGHT]);
}
// otherwise assume circle
else {
$data[] = array(
'id' => trim($row[SHAPE_ID]),
'type' => trim($row[SHAPE_TYPE]),
'radius' => trim($row[SHAPE_RADIUS_OR_WIDTH]),
'area' => round(M_PI * pow($row[SHAPE_RADIUS_OR_WIDTH], 2),2));
}
}
fclose($handle);
// remove the header row from the array
$this->_headerRow = array_shift($data);
return $data;
}
}
protected function _getShapeType($id) {
$shape = $this -> getShapeById($id);
return $shape['type'];
}
public function getShapeArrayKeyById($id)
{
foreach ($this -> _shapes as $key => $shape) {
if ($shape['id'] == $id) {
return $key;
}
}
return FALSE;
}
public function getShapeById($id) {
foreach ($this -> _shapes as $shape) {
if ($shape['id'] == $id) {
return $shape;
}
}
return FALSE;
}
public function getShapes() {
return $this -> _shapes;
}
public function sortShapesByArea($direction = ASC)
{
$tmp = array();
foreach($this->_shapes as &$multi)
{
$tmp[] = &$multi['area'];
}
array_multisort($tmp,$this->_shapes);
}
public function listShapes($tableCaption = null)
{
$doubleLine = '===============================================================================================' . PHP_EOL;
$line = '-----------------------------------------------------------------------------------------------' . PHP_EOL;
$pad = "\t";
$out = '';
if(!is_null($tableCaption))
{
$out .= $doubleLine;
$out .= ' ' . $tableCaption . PHP_EOL;
$out .= $line;
}
else
{
$out .= $doubleLine;
}
$out .= "{$pad}ID{$pad}|";
$out .= "{$pad}Type{$pad}{$pad}|";
$out .= " Radius or Width |";
$out .= " [Height] |";
$out .= " Area ";
$out .= PHP_EOL;
$out .= $doubleLine;
foreach($this->_shapes as $array)
{
$out .= $pad . $array['id'] . $pad . '|';
$out .= $pad . $array['type'];
$out .= ($array['type'] == 'rectangle') ? $pad : $pad . $pad;
$out .= '|';
($array['type'] == 'rectangle') ?
$out .= $pad . $array['width'] . $pad . ' |' :
$out .= $pad . $array['radius'] . $pad . ' |';
($array['type'] == 'rectangle') ?
$out .= $pad . $array['height'] . $pad . '|' :
$out .= $pad . 'n/a' . $pad . '|';
$out .= ' ' . $array['area'];
$out .= PHP_EOL;
$out .= $line;
}
$out .= $doubleLine;
return $out;
}
}
We can make this file beautiful and searchable if this error is corrected: It looks like row 2 should actually have 3 columns, instead of 4 in line 1.
ID, type, radius or width/height
2,circle,5,
33,rectangle,23,90
5,rectangle,32,44
23,circle,8,
29,rectangle,22,34
66,circle,2,
35,rectangle,54,63
87,circle,111,
86,rectangle,44,98
90,circle,234,
867,circle,66,
5309,rectangle,22,12
16,circle,9,
17,rectangle,88,23
56,rectangle,78,46
52,circle,18,
421,rectangle,6,37
99,circle,17,
90210,circle,45,
42,rectangle,100,54
<?php
/**
*
* @author "Will Nielsen"
* website: http://willnielsen.com
* Date: December 13, 2013
*/
require_once ('class.shapes.php');
$cmd = array_shift($argv);
$datafile = $argv[0];
$shapeId = $argv[1];
$obj = new Shapes($datafile);
/* Read file and a create an object array of shapes */
//var_dump($obj->getShapes());
echo PHP_EOL;
$obj->getShapes();
/* Get the array index of the shape with ID that was passed in and write to screen */
echo 'The array index of shape (id=' . $shapeId . ') is [' . $obj->getShapeArrayKeyById($shapeId) . ']' . PHP_EOL;
echo PHP_EOL;
/* Sort the shapes by area and write the results to the screen */
$obj->sortShapesByArea();
echo $obj->listShapes('Shapes Sorted by Area (ASC)');
echo PHP_EOL;
/* Get the new array index of the shape with ID that was passed in and write to screen */
echo 'The new array index of shape (id=' . $shapeId . ') is [' . $obj->getShapeArrayKeyById($shapeId) . ']' . PHP_EOL;
echo PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment