PHP Classes

Redbox Hydrate: Popular object variables from array data

Recommend this page to a friend!
  Info   View files Example   View files View files (15)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 57 This week: 1All time: 10,511 This week: 560Up
Version License PHP version Categories
redbox-hydrate 1.0MIT/X Consortium ...5PHP 5, Data types, Language
Description 

Author

This class can popular object variables from array data.

It takes an object and an array with data values and changes the object variables to the values in the array.

The class uses reflection to determine if the specified variables in the array exists and if they are protected or private, the class changes the accessibility of the variables to set them to the given values.

Picture of Johnny Mast
  Performance   Level  
Name: Johnny Mast <contact>
Classes: 10 packages by
Country: The Netherlands The Netherlands
Age: 41
All time rank: 121834 in The Netherlands The Netherlands
Week rank: 106 Up4 in The Netherlands The Netherlands Up
Innovation award
Innovation award
Nominee: 5x

Example

<?php
require 'autoload.php';

use
Redbox\Hydrate\Hydrator;
use function
Redbox\Hydrate\Helpers\Hydrate;

class
User {
    protected
$username = '';
    private
$password = '';

   
/**
     * @return string
     */
   
public function getUsername()
    {
        return
$this->username;
    }

   
/**
     * @return string
     */
   
public function getPassword()
    {
        return
$this->password;
    }
}

/**
 * Method 1
 */
$result1 = Hydrator::hydrate(new User())->with(
    [
       
'username' => 'abc',
       
'password' => 'pass'
   
]
);

/**
 * Method 2
 */
$result2 = Hydrate(new User())->with(
    [
       
'username' => 'abc',
       
'password' => 'pass'
   
]
);

/**
 * Method 3
 */
$hydrator = new Hydrator(new User());
$result3 = $hydrator->with(
    [
       
'username' => 'abcs',
       
'password' => 'pass'
   
]
);

unset(
$hydrator);

/**
 * Dump whatever you want
 */
//var_dump($result1);
//var_dump($result2);
//var_dump($result3);


Details

redbox-logo-klein

Build Status codecov Scrutinizer Code Quality PHP 7.1 Ready HHVM Ready Twitter URL

Redbox-hydrate

Hydrating an object is fancy talk for populating properties on PHP objects. This Hydrator helps you to quickly hydrate new class instances and returns the populated result. *Please note* it does not mether if you have private or protected properties in your class it will take care of that for you. Look in the examples folder for basic examples.

Examples

I have gone to great length to provide a special (read sexy) and tiny API for you. You can use any kind of API style you see fit in your operation for example.

Static class method

In this example we will hydrate a new instance of class User and return it as $result1 using with with() function.

use Redbox\Hydrate\Hydrator;

/
 * Method 1
 */
$result1 = Hydrator::hydrate(new User())->with(
    [
        'username' => 'abc',
        'password' => 'pass'
    ]
);

Hydrate function

use function Redbox\Hydrate\Helpers\Hydrate;

/
 * Method 2
 */
$result2 = Hydrate(new User())->with(
    [
        'username' => 'abc',
        'password' => 'pass'
    ]
);

Instantiate a new Hydrator

Plain old PHP, create your instance of the hydrator then call the with( ) method.

*Please note* There is something off with this method. If your doing this inside a loop it might consume some memory because of all the instances you create. Please don't forget to use the unset function if you are inside any kind of loop.

use Redbox\Hydrate\Hydrator;

/
 * Method 3
 */
$hydrator = new Hydrator(new User());
$result3 = $hydrator->with(
    [
        'username' => 'abcs',
        'password' => 'pass'
    ]
);
unset($hydrator);

System Requirements

Below are the minimum requirements to use this package:

  • PHP >= 5.40 (But higher is always better)
  • Composer for autoloading

Installation

Using composer:

$ composer require redbox/hydrate

Installation trough archive download

If you download the package from a website (for example github.io or phpclasses.org or any other) you will need composer installed on your machine. The reason for this is that Redbox-hydrate comes without the vendor directory which is required to run the package.

First of all if you don't have composer installed you can find it here. Follow the instructions and please don't get intimidated in fact its really really easy to install.

In the this sample i will assume you have composer installed (on any machine). Go to the package root (where composer.json is located) and execute the following command.

$ composer install  --no-dev

Now your almost ready to go. In your project require the redbox-hydrate.php (located in the package root). Assuming that Redbox-hydrate was installed in ./lib/redbox-hydrate/ your php file would look like this.

<?php
require 'lib/redbox-hydrate/redbox-hydrate.php';
// more of your nice code below

And you are ready use Redbox-hydrate in your application.

Author

Redbox-hydrate is created and maintained by Johnny Mast. For feature requests or suggestions you could consider sending me an e-mail.

License

Redbox-hydrate is released under the MIT public license.

<https://github.com/johnnymast/redbox-hydrator/blob/master/LICENSE.md>

Enjoy

Oh and if you've come down this far, you might as well follow me on twitter.


  Files folder image Files  
File Role Description
Files folder imageexamples (2 files)
Files folder imagesrc (1 file, 1 directory)
Files folder imagetests (3 files)
Accessible without login Plain text file .scrutinizer.yml Data Auxiliary data
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE.md Lic. License text
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file redbox-hydrate.php Aux. Auxiliary script

  Files folder image Files  /  examples  
File Role Description
  Accessible without login Plain text file autoload.php Aux. Auxiliary script
  Accessible without login Plain text file example.php Example Demo

  Files folder image Files  /  src  
File Role Description
Files folder imagehelpers (1 file)
  Plain text file Hydrator.php Class Class source

  Files folder image Files  /  src  /  helpers  
File Role Description
  Accessible without login Plain text file Hydrator.php Example Example script

  Files folder image Files  /  tests  
File Role Description
  Plain text file HydratorBasicTests.php Class Class source
  Plain text file HydratorExceptionTests.php Class Class source
  Plain text file SampleUserClass.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:57
This week:1
All time:10,511
This week:560Up