Download the PHP package dlin/array-converter-bundle without Composer

On this page you can find all versions of the php package dlin/array-converter-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package array-converter-bundle

Dlin Array Conversion Bundle

Dlin Symfony Array Conversion Bundle

Building a RESTFul API with Symfony2 ? You might have tried the FriendsOfSymfony/FOSRestBundle Bundle.

If you are like me, you will probably think that FriendsOfSymfony/FOSRestBundle is awesome but it is complicated and it does not support the flexibility you really want.

Let's think again what you really need for building a RESTful API:

Version

0.2


Installation

Installation using Composer

Add to your composer.json:

{
    "require" :  {
        "dlin/array-conversion-bundle": "dev-master"
    }
}

Enable the bundle in you AppKernel.php

public function registerBundles()
{
    $bundles = array(
    ...
    new Dlin\Bundle\ArrayConversionBuddle\DlinArrayConversionBundle(),
    ...
}

Annotation

There aer only two annotation options: 'groups' and 'key'.

The annotation is applicable to all properties and methods, public or not.

If a method is annotated it will be called with no parameter when converting to an array. The returned value is used as the field value of the resulting array, while the method name by default will be used as the array index. When 'hydrating' an Entity/object, the method is called with one parameter. It is normal to add a getter function to a 'read' group, and a setter functio to a 'write' group. However, the definition of groups are entirely up to you, the developer.

If an property is annotated, it will be get/set when converting between the Entity and array. Please note that private properties will also be updated.

One can use the key option to specify a diffent key name for the property or method, and the group option to group multiple properties together to be refered later for permission control.

If multiple property or method share the same key. The last one will overwrite others when converting toArray. However, all fields and methods will be set/called when 'hydrating' fromArray.

Example:

use \Dlin\Bundle\ArrayConversionBundle\Annotation\ArrayConversion;

class PersonEntity {

    /**
     * If 'key' not given, it use 'firstName' by default
     * @ArrayConversion( groups={"read", "write"})
     */
    private $firstName;

    /**
     * You can use a different array key by specifying a different key value
     * In this example, 'last' will be used as key instead of 'lastName' in the resulting array
     * @ArrayConversion( key="last", groups={"read", "write"})
     */
    private $lastName;

    /**
     * @ArrayConversion( key="age", groups={ "write"})
     */
    private $age;

    /**
     * You can also convert the result of a getter function into the resulting array,
     * If the 'key' is not specified, the function name will be used (i.e 'getFullName') as the key in the resulting array
     * @ArrayConversion(key="fullName", groups={"read"})
     */
    public function getFullName(){

        return trim($this->firstName.' '.$this->lastName);
    }

    /**
     * You can also do this for setter function.
     * This function is called when the 'fromArray' service method is called. This is useful when you want to assign values to object
     * using setter functions instead of setting values for the private properties directly.
     * @ArrayConversion( key="age", groups={ "write"})
     */
    public function setFullName($fullname){
        ...
    }

    ...
}

Usage

Geting the service in a controller

$converter =  $this->get('dlin.array_converter');

Getting the service in a ContainerAwareService

$converter = $this->container->get('dlin.array_converter');

Using the method "toArray"

The "toArray" method converts an annotated object into an array. It accepts 3 parameters:

  1. The object
  2. Array of group names. properties with matching group name with go to the result array. You can prefix a group name with '-' to exclude fields with that group. E.g. ['user', '-adminuser'] will include properties marked as in the user group but not in the adminuser group. If a property is marked as in both groups, it will NOT go into the resulting array.
  3. Array of property keys to include/exclude. You can override the group selection by passing this to the 'toArray' method. E.g. ['username', '-password'] will include the property 'username' and exclude 'password' regardless how group names match.

  4. A boolean value. Skip properties with null/false/empty values. Default is true.

Examples

$person = new PersonEntity();
$person->setFirstName('Hello');
$person->setLastName('Kitty');
$person->setAge(12);

#at least one group must given, otherwise empty array returns
$res = $converter->toArray($person, array('read'));

//$this->assertEquals($res['firstName'], $person->getFirstName());
//$this->assertEquals($res['last'], $person->getLastName());
//$this->assertEquals($res['fullName'], $person->getFullName());

Using the method "fromArray"

The "fromArray" method 'hydrate' an object using values of a given array. It accepts 2 parameters

  1. The object/Entity
  2. Array of group names. properties with matching group name with be hydrated if value is found from the given array. You can prefix a group name with '-' to exclude fields with that group. E.g. ['user', '-adminuser'] will update properties marked as in the user group but not in the adminuser group. If a property is marked as in both groups, it will not be updated

Unlike the "toArray" method, it does not accept the third parameter to override group selection.

$person = new PersonEntity();
$person->setFirstName('Hello');
$person->setLastName('Kitty');
$person->setAge(12);

$array = array('firstName'=>'New Name', 'age'=>13);

$converter->fromArray($person, $array, array('write')); #must specify a or more group

//$this->assertEquals("New Name", $person->getFirstName());
//$this->assertEquals(13, $person->getAge());

Notes

License

MIT

Free Software, Yeah!


All versions of array-converter-bundle with dependencies

PHP Build Version
Package Version
Requires php Version >=5.3.8
jms/metadata Version ~1.5.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package dlin/array-converter-bundle contains the following files

Loading the files please wait ....