Created
          May 31, 2012 10:01 
        
      - 
      
- 
        Save beberlei/2842371 to your computer and use it in GitHub Desktop. 
    Symfony Form PropertyPath for DataMapping
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | vendor | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | { | |
| "require": {"symfony/form": "dev-master"} | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | { | |
| "hash": "cbb9759cdc4abf62b9bfde5d826357da", | |
| "packages": [ | |
| { | |
| "package": "symfony/event-dispatcher", | |
| "version": "dev-master", | |
| "alias-pretty-version": "2.1.x-dev", | |
| "alias-version": "2.1.9999999.9999999-dev" | |
| }, | |
| { | |
| "package": "symfony/event-dispatcher", | |
| "version": "dev-master", | |
| "source-reference": "30d3f5da80c2aeab15bcdb5a7d448d15bc294b23" | |
| }, | |
| { | |
| "package": "symfony/form", | |
| "version": "dev-master", | |
| "alias-pretty-version": "2.1.x-dev", | |
| "alias-version": "2.1.9999999.9999999-dev" | |
| }, | |
| { | |
| "package": "symfony/form", | |
| "version": "dev-master", | |
| "source-reference": "96755781bf3859c2fe4551dc8bce8a8ab3935321" | |
| }, | |
| { | |
| "package": "symfony/locale", | |
| "version": "dev-master", | |
| "alias-pretty-version": "2.1.x-dev", | |
| "alias-version": "2.1.9999999.9999999-dev" | |
| }, | |
| { | |
| "package": "symfony/locale", | |
| "version": "dev-master", | |
| "source-reference": "57539eb3caacf17c883a5fbe1bfdc7a98dedc854" | |
| }, | |
| { | |
| "package": "symfony/options-resolver", | |
| "version": "dev-master", | |
| "alias-pretty-version": "2.1.x-dev", | |
| "alias-version": "2.1.9999999.9999999-dev" | |
| }, | |
| { | |
| "package": "symfony/options-resolver", | |
| "version": "dev-master", | |
| "source-reference": "addf0f914af769838e2c4ada01ceb3e16d62bf83" | |
| } | |
| ], | |
| "packages-dev": null, | |
| "aliases": [ | |
| ], | |
| "minimum-stability": "dev", | |
| "stability-flags": { | |
| "symfony/form": 20 | |
| } | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <?php | |
| require_once "vendor/autoload.php"; | |
| use Symfony\Component\Form\Util\PropertyPath; | |
| class Serializer | |
| { | |
| private $paths; | |
| public function __construct(array $paths) | |
| { | |
| foreach ($paths as $name => $path) { | |
| $this->paths[$name] = new PropertyPath($path); | |
| } | |
| } | |
| public function unserialize(array $data, $object) | |
| { | |
| foreach ($data as $k => $v) { | |
| $this->paths[$k]->setValue($object, $v); | |
| } | |
| } | |
| } | |
| class TestObj | |
| { | |
| public $bar; | |
| public $baz = array(); | |
| } | |
| $obj = new TestObj; | |
| $serializer = new Serializer(array('foo' => 'bar', 'bar' => 'baz[0]', 'baz' => 'baz[1]')); | |
| $serializer->unserialize(array('foo' => 1, 'bar' => 2, 'baz' => 3), $obj); | |
| var_dump($obj); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
Output is
object(TestObj)#1 (2) {
["bar"]=>
int(1)
["baz"]=>
array(2) {
[0]=>
int(2)
[1]=>
int(3)
}
}