PHP code example of jimmy4fingers / simple-data-mapper

1. Go to this page and download the library: Download jimmy4fingers/simple-data-mapper library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jimmy4fingers / simple-data-mapper example snippets




// form data
$postData = [
    'my-form-field-name' => 'jim',
    'my-form-field-email' => '[email protected]',
    'my-form-field-age' => 36
];

// data to insert
$entityData = [
    'name' => $postData['my-form-field-name'],
    'email_address' => $postData['my-form-field-email'],
    'users_age' => $postData['my-form-field-age']
];



use DataMapper\Map;
use DataMapper\MapCollection;
use DataMapper\Mapper;

$map = new Map();
$collection = new MapCollection();
$mapper = new Mapper();

// post data example
$data = [
    'my-form-field-name' => 'jim'
];

// create a collection of mappings
$collection->add(
    // [application reference], [lookup key]
    $map->set('my-form-field-name', 'name')
);

// pass data into the mapper along with the collection
$mapper->set($collection, $data);

$name = $mapper->get('my-form-field-name')->getData();
// $name = 'jim';




use DataMapper\Map;
use DataMapper\MapCollection;
use DataMapper\Mapper;

$map = new Map();
$collection = new MapCollection();
$mapper = new Mapper();

// loaded data example
$data = ['name' => 'jimmy'];

// create a collection of mappings
$collection->add($map->set('my-form-field-name', 'name'));

// the last parameter tells the mapper to map data using the lookup key
$mapper->set($collection, $data, true);

$name = $mapper->get('my-form-field-name')->getData();
// $name = 'jimmy';


use DataMapper\Map;
use DataMapper\MapCollection;
use DataMapper\Mapper;

$map = new Map();
$collection = new MapCollection();
$mapper = new Mapper();

$ucwordsCallback = function ($data) {
    return ucwords($data);
};

$maps = [
    $map->set('first-name','first_names')->setOnMap($ucwordsCallback),
    $map->set('last-name','last_name')->setOnMap($ucwordsCallback),
    $map->set('email','email_address'),
    $map->set('address_line1','line_1'),
    $map->set('address_line1','line_2'),
    $map->set('address_line1','line_3')
];
foreach($maps as $map) {
    $collection->add($map);
}

// posted data example
$postData = [
    'first-name' => 'jimmy jams',
    'last-name' => 'higgins',
];

$mapper->set($collection, $postData);

$myData = $mapper->getArray();
// $myData = ['first-name' => 'Jimmy Jams' etc...];

 
 // data your mapping
 $post = [
     'jsonData' => ['city1', 'city2', 'city3']
];
 
 $map = new Map();
 $collection = new MapCollection();
 $mapper = new Mapper();
 
 $cb1 = function ($data) {
     $data = json_decode($data,true);
     if (is_array($data) && array_key_exists(0,$data))
        $data = $data[0];
     return $data;
 };
 $cb2 = function ($data) {
      $data = json_decode($data,true);
      if (is_array($data) && array_key_exists(1,$data))
         $data = $data[1];
      return $data;
  };
  
 $maps = [
     $map->set('city1','city_1')->setDataFrom('jsonData')->setOnMap($cb1),
     $map->set('city2','city_2')->setDataFrom('jsonData')->setOnMap($cb2)
 ];
 foreach($maps as $map) {
     $collection->add($map);
 }
 
 $mapper->set($collection, $post);
 
 $myData = $mapper->getArray();
 // $myData = ['city1' => 'city1', 'city2' => 'city2']
 
 


use DataMapper\Map;
use DataMapper\MapCollection;
use DataMapper\Mapper;

$map = new Map();
$collection = new MapCollection();
$mapper = new Mapper();

$maps = [
    $map->set('first-name','first_names')->setValidation('['first-name'=>'bob', 'last-name'=>'']);

$validation = new ValidationObject();
$validation->validateMapped($mapper->get()); // Mapper::get returns MapInterface[]
if ($validation->isValid()) {
    //...
}