Class: File_CSV_DataSource
Source Location: /File_CSV_DataSource-1.0.1/DataSource.php
csv data fetcher
Author(s):
Copyright:
|
|
Inherited Variables
|
Inherited Methods
|
Class Details
Method Detail
__construct (Constructor) [line 111]
void __construct(
[mixed
$filename = null])
|
|
data load initialize
Parameters:
appendColumn [line 640]
boolean appendColumn(
string
$column, [mixed
$values = null])
|
|
column appender Appends a column and each or all values in it can be dinamically filled. Only when the $values argument is given.
true
true
true
array (
0 =>
array (
'name' => 'john',
'age' => 99,
'skill' => 'knows magic',
'candy_ownership' => 99,
'import_id' => 111111111,
),
1 =>
array (
'name' => 'tanaka',
'age' => 99,
'skill' => 'makes sushi',
'candy_ownership' => 44,
'import_id' => 111111111,
),
2 =>
array (
'name' => 'jose',
'age' => 99,
'skill' => 'dances salsa',
'candy_ownership' => 65,
'import_id' => 111111111,
),
)
Parameters:
appendRow [line 1457]
boolean appendRow(
array
$values)
|
|
row appender Aggregates one more row to the currently loaded dataset sample of a csv file "my_cool.csv" name,age,skill
john,13,knows magic
tanaka,8,makes sushi
jose,5,dances salsa
first let's load the file and output whatever was retrived. require_once 'File/CSV/DataSource.php';
$csv->load('my_cool.csv');
output array (
0 =>
array (
'name' => 'john',
'age' => '13',
'skill' => 'knows magic',
),
1 =>
array (
'name' => 'tanaka',
'age' => '8',
'skill' => 'makes sushi',
),
2 =>
array (
'name' => 'jose',
'age' => '5',
'skill' => 'dances salsa',
),
)
now lets do some modifications, let's try adding three rows.
output true
true
true
and now let's try to see what has changed
output array (
0 =>
array (
'name' => 'john',
'age' => '13',
'skill' => 'knows magic',
),
1 =>
array (
'name' => 'tanaka',
'age' => '8',
'skill' => 'makes sushi',
),
2 =>
array (
'name' => 'jose',
'age' => '5',
'skill' => 'dances salsa',
),
3 =>
array (
'name' => 1,
'age' => 1,
'skill' => 1,
),
4 =>
array (
'name' => '2',
'age' => '2',
'skill' => '2',
),
5 =>
array (
'name' => 3,
'age' => 3,
'skill' => 3,
),
)
Parameters:
connect [line 316]
array connect(
[array
$columns = array()])
|
|
header and row relationship builder Attempts to create a relationship for every single cell that was captured and its corresponding header. The sample below shows how a connection/relationship is built. sample of a csv file "my_cool.csv" name,age,skill
john,13,knows magic
tanaka,8,makes sushi
jose,5,dances salsa
php implementation
$csv->load('my_cool.csv');
die('file has headers and rows with different lengths
cannot connect');
}
array (
0 =>
array (
'name' => 'john',
'age' => '13',
'skill' => 'knows magic',
),
1 =>
array (
'name' => 'tanaka',
'age' => '8',
'skill' => 'makes sushi',
),
2 =>
array (
'name' => 'jose',
'age' => '5',
'skill' => 'dances salsa',
),
)
You can pass a collection of headers in an array to build a connection for those columns only!
array (
0 =>
array (
'age' => '13',
),
1 =>
array (
'age' => '8',
),
2 =>
array (
'age' => '5',
),
)
Parameters:
countHeaders [line 225]
header counter retrives the total number of loaded headers
countRows [line 1332]
row counter This function will exclude the headers sample of a csv file "my_cool.csv" name,age,skill
john,13,knows magic
tanaka,8,makes sushi
jose,5,dances salsa
php implementation
$csv->load('my_cool.csv');
createHeaders [line 1965]
boolean createHeaders(
string
$prefix)
|
|
header creator uses prefix and creates a header for each column suffixed by a numeric value by default the first row is interpreted as headers but if we have a csv file with data only and no headers it becomes really annoying to work with the current loaded data. this function will create a set dinamically generated headers and make the current headers accessable with the row handling functions Note: that the csv file contains only data but no headers sample of a csv file "my_cool.csv" john,13,knows magic
tanaka,8,makes sushi
jose,5,dances salsa
checks if the csv file was loaded
if (!$csv->load('my_cool.csv')) {
die('can not load csv file');
}
dump current headers
standard output array (
0 => 'john',
1 => '13',
2 => 'knows magic',
)
generate headers named 'column' suffixed by a number and interpret the previous headers as rows.
dump current headers
standard output array (
0 => 'column_1',
1 => 'column_2',
2 => 'column_3',
)
build a relationship and dump it
output array (
0 =>
array (
'column_1' => 'john',
'column_2' => '13',
'column_3' => 'knows magic',
),
1 =>
array (
'column_1' => 'tanaka',
'column_2' => '8',
'column_3' => 'makes sushi',
),
2 =>
array (
'column_1' => 'jose',
'column_2' => '5',
'column_3' => 'dances salsa',
),
)
Parameters:
fillCell [line 1043]
boolean fillCell(
integer
$x, integer
$y, mixed
$value)
|
|
cell value filler replaces the value of a specific cell sample of a csv file "my_cool.csv" name,age,skill
john,13,knows magic
tanaka,8,makes sushi
jose,5,dances salsa
php implementation
// load the csv file
$csv->load('my_cool.csv');
// find out if the given coordinate is valid
// if so grab that cell and dump it
// replace the value of that cell
$csv->fillCell(1 , 1 , 'new value'); // true
// output the new value of the cell
}
now lets try to grab the whole row // show the whole row
standard output array (
0 => 'tanaka',
1 => 'new value',
2 => 'makes sushi',
)
Parameters:
fillColumn [line 742]
void fillColumn(
mixed
$column, [mixed
$values = null])
|
|
collumn data injector fills alll the data in the given column with $values sample of a csv file "my_cool.csv" name,age,skill
john,13,knows magic
tanaka,8,makes sushi
jose,5,dances salsa
php implementation
$csv->load('my_cool.csv');
// if the csv file loads
if ($csv->load('my_cool.csv')) {
// grab all data within the age column
// rename all values in it with the number 99
// grab all data within the age column
// rename each value in a column independently
$data = array(1, 2, 3);
}
standard output array (
0 => '13',
1 => '8',
2 => '5',
)
true
array (
0 => 99,
1 => 99,
2 => 99,
)
array (
0 => 1,
1 => 2,
2 => 3,
)
Parameters:
fillRow [line 1584]
boolean fillRow(
integer
$row, mixed
$values)
|
|
fillRow Replaces the contents of cells in one given row with $values. sample of a csv file "my_cool.csv" name,age,skill
john,13,knows magic
tanaka,8,makes sushi
jose,5,dances salsa
if we load the csv file and fill the second row with new data? // load the library
require_once 'File/CSV/DataSource.php';
// load csv file
$csv->load('my_cool.csv');
// fill exitent row
output true
now let's dump whatever we have changed
output array (
0 =>
array (
'name' => 'john',
'age' => '13',
'skill' => 'knows magic',
),
1 =>
array (
'name' => 'x',
'age' => 'x',
'skill' => 'x',
),
2 =>
array (
'name' => 'jose',
'age' => '5',
'skill' => 'dances salsa',
),
)
now lets try to fill the row with specific data for each cell
output true
and dump the results
output array (
0 =>
array (
'name' => 'john',
'age' => '13',
'skill' => 'knows magic',
),
1 =>
array (
'name' => 1,
'age' => 2,
'skill' => 3,
),
2 =>
array (
'name' => 'jose',
'age' => '5',
'skill' => 'dances salsa',
),
)
Parameters:
flush [line 2257]
object data flusher tells this object to forget all data loaded and start from scratch
getAsymmetricRows [line 412]
array getAsymmetricRows(
)
|
|
asymmetric data fetcher finds the rows that do not match the headers length lets assume that we add one more row to our csv file. that has only two values. Something like name,age,skill
john,13,knows magic
tanaka,8,makes sushi
jose,5,dances salsa
niki,6
Then in our php code $csv->load('my_cool.csv');
The result array (
0 =>
array (
0 => 'niki',
1 => '6',
),
)
getCell [line 971]
mixed|false getCell(
integer
$x, integer
$y)
|
|
cell fetcher gets the value of a specific cell by given coordinates Note: That indexes start with zero, and headers are not searched! For example if we are trying to grab the cell that is in the second row and the third column name,age,skill
john,13,knows magic
tanaka,8,makes sushi
jose,5,dances salsa
we would do something like
and get the following results 'makes sushi'
Parameters:
getColumn [line 529]
array getColumn(
string
$name)
|
|
column fetcher gets all the data for a specific column identified by $name Note $name is the same as the items returned by getHeaders() sample of a csv file "my_cool.csv" name,age,skill
john,13,knows magic
tanaka,8,makes sushi
jose,5,dances salsa
php implementation
$csv->load('my_cool.csv');
the above example outputs something like array (
0 => 'john',
1 => 'tanaka',
2 => 'jose',
)
Parameters:
getHeaders [line 212]
header fetcher gets csv headers into an array
array (
0 => 'name',
1 => 'age',
2 => 'skill',
)
getRawArray [line 1845]
raw data as array Gets the data that was retrived from the csv file as an array Note: that changes and alterations made to rows, columns and values will also reflect on what this function retrives.
getRow [line 1174]
array getRow(
integer
$number)
|
|
row fetcher Note: first row is zero sample of a csv file "my_cool.csv" name,age,skill
john,13,knows magic
tanaka,8,makes sushi
jose,5,dances salsa
load the library and csv file require_once 'File/CSV/DataSource.php';
$csv->load('my_cool.csv');
lets dump currently loaded data
output array (
0 =>
array (
'name' => 'john',
'age' => '13',
'skill' => 'knows magic',
),
1 =>
array (
'name' => 'tanaka',
'age' => '8',
'skill' => 'makes sushi',
),
2 =>
array (
'name' => 'jose',
'age' => '5',
'skill' => 'dances salsa',
),
)
Now let's fetch the second row
output array (
0 => 'tanaka',
1 => '8',
2 => 'makes sushi',
)
Parameters:
getRows [line 1288]
array getRows(
[array
$range = array()])
|
|
multiple row fetcher Extracts a rows in the following fashion - all rows if no $range argument is given
- a range of rows identified by their key
- if rows in range are not found nothing is retrived instead
- if no rows were found an empty array is returned
sample of a csv file "my_cool.csv" name,age,skill
john,13,knows magic
tanaka,8,makes sushi
jose,5,dances salsa
load the library and csv file require_once 'File/CSV/DataSource.php';
$csv->load('my_cool.csv');
lets dump currently loaded data
output array (
0 =>
array (
'name' => 'john',
'age' => '13',
'skill' => 'knows magic',
),
1 =>
array (
'name' => 'tanaka',
'age' => '8',
'skill' => 'makes sushi',
),
2 =>
array (
'name' => 'jose',
'age' => '5',
'skill' => 'dances salsa',
),
)
now get the second and thirdh row
output array (
0 =>
array (
0 => 'tanaka',
1 => '8',
2 => 'makes sushi',
),
1 =>
array (
0 => 'jose',
1 => '5',
2 => 'dances salsa',
),
)
now lets try something odd and the goodie third row
output array (
0 =>
array (
0 => 'jose',
1 => '5',
2 => 'dances salsa',
),
)
Parameters:
hasCell [line 1093]
void hasCell(
mixed
$x, mixed
$y)
|
|
checks if a coordinate is valid sample of a csv file "my_cool.csv" name,age,skill
john,13,knows magic
tanaka,8,makes sushi
jose,5,dances salsa
load the csv file
// loaded
find out if a coordinate is valid
check again for a know valid coordinate and grab that cell
Parameters:
hasColumn [line 579]
boolean hasColumn(
string
$string)
|
|
column existance checker checks if a column exists, columns are identified by their header name. sample of a csv file "my_cool.csv" name,age,skill
john,13,knows magic
tanaka,8,makes sushi
jose,5,dances salsa
php implementation
$csv->load('my_cool.csv');
now lets check if the columns exist
Parameters:
hasRow [line 1684]
boolean hasRow(
mixed
$number)
|
|
row existance checker Scans currently loaded dataset and checks if a given row identified by $number exists sample of a csv file "my_cool.csv" name,age,skill
john,13,knows magic
tanaka,8,makes sushi
jose,5,dances salsa
load library and csv file require_once 'File/CSV/DataSource.php';
$csv->load('my_cool.csv');
build a relationship and dump it so we can see the rows we will be working with
output array (
0 =>
array (
'name' => 'john',
'age' => '13',
'skill' => 'knows magic',
),
1 => // THIS ROW EXISTS!!!
array (
'name' => 'tanaka',
'age' => '8',
'skill' => 'makes sushi',
),
2 =>
array (
'name' => 'jose',
'age' => '5',
'skill' => 'dances salsa',
),
)
now lets check for row existance
output true
false
false
Parameters:
isSymmetric [line 359]
data length/symmetry checker tells if the headers and all of the contents length match. Note: there is a lot of methods that won't work if data is not symmetric this method is very important!
load [line 158]
boolean load(
string
$filename)
|
|
csv file loader indicates the object which file is to be loaded require_once 'File/CSV/DataSource.php';
$csv->load('my_cool.csv');
array (
0 =>
array (
'name' => 'john',
'age' => '13',
'skill' => 'knows magic',
),
1 =>
array (
'name' => 'tanaka',
'age' => '8',
'skill' => 'makes sushi',
),
2 =>
array (
'name' => 'jose',
'age' => '5',
'skill' => 'dances salsa',
),
)
Parameters:
moveHeadersToRows [line 2214]
void moveHeadersToRows(
)
|
|
header relocator
parse [line 2136]
csv parser reads csv data and transforms it into php-data
removeColumn [line 874]
boolean removeColumn(
string
$name)
|
|
column remover Completly removes a whole column identified by $name Note: that this function will only work if data is symmetric. sample of a csv file "my_cool.csv" name,age,skill
john,13,knows magic
tanaka,8,makes sushi
jose,5,dances salsa
load the library and csv file require_once 'File/CSV/DataSource.php';
$csv->load('my_cool.csv');
lets dump currently loaded data
output array (
0 =>
array (
'name' => 'john',
'age' => '13',
'skill' => 'knows magic',
),
1 =>
array (
'name' => 'tanaka',
'age' => '8',
'skill' => 'makes sushi',
),
2 =>
array (
'name' => 'jose',
'age' => '5',
'skill' => 'dances salsa',
),
)
and now let's remove the second column
output true
those changes made let's dump the data again and see what we got array (
0 =>
array (
'name' => 'john',
'skill' => 'knows magic',
),
1 =>
array (
'name' => 'tanaka',
'skill' => 'makes sushi',
),
2 =>
array (
'name' => 'jose',
'skill' => 'dances salsa',
),
)
Parameters:
removeEmpty [line 2173]
empty row remover removes all records that have been defined but have no data.
removeRow [line 1783]
boolean removeRow(
mixed
$number)
|
|
row remover removes one row from the current data set. sample of a csv file "my_cool.csv" name,age,skill
john,13,knows magic
tanaka,8,makes sushi
jose,5,dances salsa
first let's load the file and output whatever was retrived. require_once 'File/CSV/DataSource.php';
$csv->load('my_cool.csv');
output array (
0 =>
array (
'name' => 'john',
'age' => '13',
'skill' => 'knows magic',
),
1 =>
array (
'name' => 'tanaka',
'age' => '8',
'skill' => 'makes sushi',
),
2 =>
array (
'name' => 'jose',
'age' => '5',
'skill' => 'dances salsa',
),
)
now lets remove the second row
output true
now lets dump again the data and see what changes have been made
output array (
0 =>
array (
'name' => 'john',
'age' => '13',
'skill' => 'knows magic',
),
1 =>
array (
'name' => 'jose',
'age' => '5',
'skill' => 'dances salsa',
),
)
Parameters:
resetKeys [line 2239]
void resetKeys(
array
&$array)
|
|
array key reseter makes sure that an array's keys are setted in a correct numerical order Note: that this function does not return anything, all changes are made to the original array as a reference
Parameters:
setHeaders [line 2112]
boolean setHeaders(
array
$list)
|
|
header injector uses a $list of values which wil be used to replace current headers. Note: that given $list must match the length of all rows. known as symmetric. see isSymmetric() and getAsymmetricRows() methods Also, that current headers will be used as first row of data and consecuently all rows order will change with this action. sample of a csv file "my_cool.csv" name,age,skill
john,13,knows magic
tanaka,8,makes sushi
jose,5,dances salsa
load the library and csv file require_once 'File/CSV/DataSource.php';
$csv->load('my_cool.csv');
lets dump currently loaded data
output array (
0 =>
array (
'name' => 'john',
'age' => '13',
'skill' => 'knows magic',
),
1 =>
array (
'name' => 'tanaka',
'age' => '8',
'skill' => 'makes sushi',
),
2 =>
array (
'name' => 'jose',
'age' => '5',
'skill' => 'dances salsa',
),
)
And now lets create a new set of headers and attempt to inject them into the current loaded dataset $new_headers = array('a', 'b', 'c');
output true
Now lets try the same with some headers that do not match the current headers length. (this should fail) $new_headers = array('a', 'b');
output false
now let's dump whatever we have changed
output array (
0 =>
array (
'a' => 'name',
'b' => 'age',
'c' => 'skill',
),
1 =>
array (
'a' => 'john',
'b' => '13',
'c' => 'knows magic',
),
2 =>
array (
'a' => 'tanaka',
'b' => '8',
'c' => 'makes sushi',
),
3 =>
array (
'a' => 'jose',
'b' => '5',
'c' => 'dances salsa',
),
)
Parameters:
settings [line 187]
boolean settings(
mixed
$array)
|
|
settings alterator lets you define different settings for scanning Given array will override the internal settings $settings = array(
'delimiter' => ',',
'eol' => ";",
'length' => 999999,
'escape' => '"'
);
Parameters:
symmetrize [line 436]
array symmetrize(
[mixed
$value = ''])
|
|
all rows length equalizer makes the length of all rows and headers the same. If no $value is given all unexistent cells will be filled with empty spaces
Parameters:
validates [line 2193]
csv file validator checks wheather if the given csv file is valid or not
walkColumn [line 914]
boolean walkColumn(
string
$name, string
$callback)
|
|
column walker goes through the whole column and executes a callback for each one of the cells in it. Note: callback functions get the value of the cell as an argument, and whatever that callback returns will be used to replace the current value of that cell.
Parameters:
walkGrid [line 476]
void walkGrid(
string
$callback)
|
|
grid walker travels through the whole dataset executing a callback per each cell Note: callback functions get the value of the cell as an argument, and whatever that callback returns will be used to replace the current value of that cell.
Parameters:
walkRow [line 1818]
boolean walkRow(
string|integer
$row, string
$callback)
|
|
row walker goes through one full row of data and executes a callback function per each cell in that row. Note: callback functions get the value of the cell as an argument, and whatever that callback returns will be used to replace the current value of that cell.
Parameters:
Documentation generated on Mon, 11 Mar 2019 15:36:10 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|
|