dev-master
9999999-devKeep your data in native php types!
MIT
The Development Requires
by Dave Widmer
data native
Wallogit.com
2017 © Pedro Peláez
Keep your data in native php types!
PHP likes to send data back as strings when pulling from datasources. This library will help you convert everything into a native php format., (*1)
The easiest way to learn about this library is to see an example. We'll pretend that we are converting a row from a database., (*2)
The property map is an array of 'property name' => 'mapping name' pairs. See the Casting section for more information on what is available., (*3)
``` php class UserObject extends Wid\Data\NativeObject { protected function init() { $this->setPropertyMap(array( 'id' => 'Integer', 'active' => 'Boolean', 'joined_at' => 'DateTime', 'roles' => function($arg){ return explode(',', $arg); } ));, (*4)
parent::init(); }
}, (*5)
$databaseRow = array( 'id' => '1', 'username' => 'dwidmer', 'active' => '0', 'joined_at' => '2014-01-15 12:00:00', 'roles' => 'super,admin,user' );, (*6)
$user = new UserObject($databaseRow); $user->toArray();, (*7)
/* //// Output //// */, (*8)
// 'id' => (int) 1 // 'username' => (string) 'dwidmer' // 'active' => (boolean) false, // 'joined_at' => \DateTime // 'roles' => array('super', 'admin', 'user'), (*9)
It's really that easy! ## Casting Below is a list of casting types available in the library Mapping Name | Transforms to --------------|------------- Boolean | boolean Integer | int Float | float DateTime | \DateTime Date | \DateTime Time | \DateTime Timestamp | \DateTime You can also pass in anything that can be called by `call_user_func` as the second argument in the propery map (see `roles` in the example) and the data can be casted that way. In our example it is turning a string into an array, exploding on the commas. ## Property Access So how do I get at the data? Well each of the following lines will work. ``` php $id = $user['id']; // Array Access $id = $user->id; // Object Notation $id = $user->get('id'); // Getter
get() has the added bonus of being able to add a default value as the second
parameter. This is null if not explicitly set., (*10)
And setting?, (*11)
You can set with Array Access, Object Notation or a Setter, just like when you are getting data out. All of the methods below will work., (*12)
php
$user['active'] = true;
$user->active = true;
$user->set('active', true);, (*13)
Getting our data into native php has a lot of advantages, but the one I had in
mind when develping this library is for API development. When we send our data
it should be in native data types (i.e. booleans shouldn't come back as '0').
P.S. - you can call toJSON() on any NativeObject and get the json representation
of it., (*14)
See some things that can be improved or want to help add some features?, (*15)
composer install
Developed by Dave Widmer, (*16)
Keep your data in native php types!
MIT
data native