PHP Class Lazer\Classes\Core_Database

There are classes to use JSON files like file database. Using style was inspired by ORM classes.
Author: Grzegorz Kuźnik
Inheritance: implements IteratorAggregate, implements Countable
Show file Open project: greg0/lazer-database

Protected Properties

Property Type Description
$currentId ID of current row if setted
$currentKey Key if current row if setted
$data Contain returned data from file as object or array of objects
$name Name of file (table)
$pending array Pending functions with values
$resetKeys integer Information about to reset keys in array or not to
$set Object with setted data

Public Methods

Method Description
__get ( string $name ) : mixed Returning variable from Object
__isset ( string $name ) : boolean Check if the given field exists
__set ( string $name, mixed $value ) Validating fields and setting variables to current operations
addFields ( array $fields ) Add new fields to table, array schema like in create() function
andWhere ( string $field, string $op, mixed $value ) : Core_Database Alias for where()
asArray ( string $key = null, string $value = null ) : array Returning data as indexed or assoc array.
config ( ) : object Returning object with config for table
count ( ) : mixed Return count in integer or array of integers (if grouped)
create ( string $name, array $fields ) Creating new table
debug ( ) Debug functions, prints whole query with values
delete ( ) : boolean Deleting loaded data
deleteFields ( array $fields ) Delete fields from array
fields ( ) : array Return array with names of fields
find ( integer $id = NULL ) : Core_Database Returns one row with specified ID
findAll ( ) Make data ready to read
getIterator ( ) : ArrayIterator Iterator for Data
groupBy ( string $column ) : Core_Database Grouping results by one field
lastId ( ) : integer Returning last ID from table
limit ( integer $number, integer $offset ) : Core_Database Limit returned data
name ( ) : string Returns table name
orWhere ( string $field, string $op, mixed $value ) : Core_Database Alias for where(), setting OR for searching
orderBy ( string $key, string $direction = 'ASC' ) : Core_Database Sorting data by field
relations ( string | null $tableName = null ) : array Returning assoc array with relationed tables
remove ( string $name ) : boolean | Lazer\Classes\LazerException Removing table with config
save ( ) Saving inserted or updated data
schema ( ) : array Returning assoc array with types of fields
table ( string $name ) : Lazer\Classes\Database Factory pattern
where ( string $field, string $op, mixed $value ) : Core_Database Where function, like SQL
with ( string $table ) : Core_Database JOIN other tables

Protected Methods

Method Description
clearKeyInfo ( ) Set NULL for currentId and currentKey
clearQuery ( ) Clear info about previous queries
getData ( ) : array Get rows from table
getRowKey ( integer $id ) : integer Returns array key of row with specified ID
groupByPending ( ) Grouping array pending method
limitPending ( ) Pending function for limit()
orderByPending ( ) Sort an array of objects by more than one field.
pending ( ) Execute pending functions
setData ( ) Setting data to Database::$data
setFields ( ) Setting fields with default values
setPending ( ) Set pending functions in right order with default values (Empty).
wherePending ( ) : boolean Filter function for array_filter() in where()
withPending ( ) Pending function for with(), joining other tables to current

Method Details

__get() public method

Returning variable from Object
public __get ( string $name ) : mixed
$name string Field name
return mixed Field value

__isset() public method

Check if the given field exists
public __isset ( string $name ) : boolean
$name string Field name
return boolean True if the field exists, false otherwise

__set() public method

Validating fields and setting variables to current operations
public __set ( string $name, mixed $value )
$name string Field name
$value mixed Field value

addFields() public method

Add new fields to table, array schema like in create() function
public addFields ( array $fields )
$fields array Associative array

andWhere() public method

Alias for where()
public andWhere ( string $field, string $op, mixed $value ) : Core_Database
$field string Field name
$op string Operator
$value mixed Field value
return Core_Database

asArray() public method

Returning data as indexed or assoc array.
public asArray ( string $key = null, string $value = null ) : array
$key string Field that will be the key, NULL for Indexed
$value string Field that will be the value
return array

clearKeyInfo() protected method

Set NULL for currentId and currentKey
protected clearKeyInfo ( )

clearQuery() protected method

Clear info about previous queries
protected clearQuery ( )

config() public method

Returning object with config for table
public config ( ) : object
return object Config

count() public method

Return count in integer or array of integers (if grouped)
public count ( ) : mixed
return mixed

create() public static method

For example few fields: Database::create('news', array( 'title' => 'string', 'content' => 'string', 'rating' => 'double', 'author' => 'integer' )); Types of field: - boolean - integer - string - double (also for float type) ID field isn't required (it will be created automatically) but you can specify it at first place.
public static create ( string $name, array $fields )
$name string Table name
$fields array Field configuration

debug() public method

Debug functions, prints whole query with values
public debug ( )

delete() public method

Deleting loaded data
public delete ( ) : boolean
return boolean

deleteFields() public method

Delete fields from array
public deleteFields ( array $fields )
$fields array Indexed array

fields() public method

Return array with names of fields
public fields ( ) : array
return array Fields

find() public method

Returns one row with specified ID
public find ( integer $id = NULL ) : Core_Database
$id integer Row ID
return Core_Database

findAll() public method

Make data ready to read
public findAll ( )

getData() protected method

Get rows from table
protected getData ( ) : array
return array

getIterator() public method

Iterator for Data

getRowKey() protected method

Returns array key of row with specified ID
protected getRowKey ( integer $id ) : integer
$id integer Row ID
return integer Row key

groupBy() public method

Grouping results by one field
public groupBy ( string $column ) : Core_Database
$column string
return Core_Database

groupByPending() protected method

Grouping array pending method
protected groupByPending ( )

lastId() public method

Returning last ID from table
public lastId ( ) : integer
return integer Last ID

limit() public method

Should be used at the end of chain, before end method
public limit ( integer $number, integer $offset ) : Core_Database
$number integer Limit number
$offset integer Offset number
return Core_Database

limitPending() protected method

Pending function for limit()
protected limitPending ( )

name() public method

Returns table name
public name ( ) : string
return string table name

orWhere() public method

Alias for where(), setting OR for searching
public orWhere ( string $field, string $op, mixed $value ) : Core_Database
$field string Field name
$op string Operator
$value mixed Field value
return Core_Database

orderBy() public method

Sorting data by field
public orderBy ( string $key, string $direction = 'ASC' ) : Core_Database
$key string Field name
$direction string ASC|DESC
return Core_Database

orderByPending() protected method

@
protected orderByPending ( )

pending() protected method

Execute pending functions
protected pending ( )

relations() public method

Returning assoc array with relationed tables
public relations ( string | null $tableName = null ) : array
$tableName string | null
return array Fields type

remove() public static method

Removing table with config
public static remove ( string $name ) : boolean | Lazer\Classes\LazerException
$name string Table name
return boolean | Lazer\Classes\LazerException

save() public method

Saving inserted or updated data
public save ( )

schema() public method

Returning assoc array with types of fields
public schema ( ) : array
return array Fields type

setData() protected method

Setting data to Database::$data
protected setData ( )

setFields() protected method

Setting fields with default values
protected setFields ( )

setPending() protected method

Set pending functions in right order with default values (Empty).
protected setPending ( )

table() public static method

Factory pattern
public static table ( string $name ) : Lazer\Classes\Database
$name string Name of table
return Lazer\Classes\Database

where() public method

Operators: - Standard operators (=, !=, >, <, >=, <=) - IN (only for array value) - NOT IN (only for array value)
public where ( string $field, string $op, mixed $value ) : Core_Database
$field string Field name
$op string Operator
$value mixed Field value
return Core_Database

wherePending() protected method

Filter function for array_filter() in where()
protected wherePending ( ) : boolean
return boolean

with() public method

JOIN other tables
public with ( string $table ) : Core_Database
$table string relations separated by :
return Core_Database

withPending() protected method

Pending function for with(), joining other tables to current
protected withPending ( )

Property Details

$currentId protected property

ID of current row if setted
protected $currentId

$currentKey protected property

Key if current row if setted
protected $currentKey

$data protected property

Contain returned data from file as object or array of objects
protected $data

$name protected property

Name of file (table)
protected $name

$pending protected property

Pending functions with values
See also: Lazer\Classes\Core_Database::setPending()
protected array $pending
return array

$resetKeys protected property

Information about to reset keys in array or not to
protected int $resetKeys
return integer

$set protected property

Object with setted data
protected $set