Skip to content

Instantly share code, notes, and snippets.

@zebba
Last active August 29, 2015 14:04

Revisions

  1. zebba revised this gist Jul 23, 2014. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions Entity\Foo.php
    Original file line number Diff line number Diff line change
    @@ -19,9 +19,7 @@ static public function fromArray(array $data)
    if (array_key_exists('id', $properties)) { unset($properties['id']); }

    foreach ($properties as $k) {
    $k = str_replace(' ', '', ucwords(str_replace('_', ' ', strtolower($k))));

    $setter = sprintf('set%s', ucwords($k));
    $setter = sprintf('set%s', $k);

    if (array_key_exists($k, $data) && method_exists($o, $setter)) {
    $o->{$setter}($data[$k]);
  2. zebba revised this gist Jul 23, 2014. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion Entity\Foo.php
    Original file line number Diff line number Diff line change
    @@ -13,12 +13,14 @@ static public function fromArray(array $data)
    $reflection = new \ReflectionClass($o);
    $properties = $reflection->getProperties();
    $properties = array_map(function ($v) { /* @var $v \ReflectionProperty */
    return str_replace(' ', '', ucwords(str_replace('_', ' ', strtolower($v->getName()))));
    return $v->getName();
    }, $properties);

    if (array_key_exists('id', $properties)) { unset($properties['id']); }

    foreach ($properties as $k) {
    $k = str_replace(' ', '', ucwords(str_replace('_', ' ', strtolower($k))));

    $setter = sprintf('set%s', ucwords($k));

    if (array_key_exists($k, $data) && method_exists($o, $setter)) {
  3. zebba revised this gist Jul 23, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Entity\Foo.php
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@ static public function fromArray(array $data)
    $reflection = new \ReflectionClass($o);
    $properties = $reflection->getProperties();
    $properties = array_map(function ($v) { /* @var $v \ReflectionProperty */
    return $v->getName();
    return str_replace(' ', '', ucwords(str_replace('_', ' ', strtolower($v->getName()))));
    }, $properties);

    if (array_key_exists('id', $properties)) { unset($properties['id']); }
  4. zebba renamed this gist Jul 23, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. zebba revised this gist Jul 23, 2014. 2 changed files with 35 additions and 30 deletions.
    35 changes: 35 additions & 0 deletions Entity\Foo
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    <?php

    // ...

    class Foo
    {
    // ...

    static public function fromArray(array $data)
    {
    $o = new self();

    $reflection = new \ReflectionClass($o);
    $properties = $reflection->getProperties();
    $properties = array_map(function ($v) { /* @var $v \ReflectionProperty */
    return $v->getName();
    }, $properties);

    if (array_key_exists('id', $properties)) { unset($properties['id']); }

    foreach ($properties as $k) {
    $setter = sprintf('set%s', ucwords($k));

    if (array_key_exists($k, $data) && method_exists($o, $setter)) {
    $o->{$setter}($data[$k]);
    }
    }

    return $o;
    }

    // ...
    }

    ?>
    30 changes: 0 additions & 30 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -1,30 +0,0 @@
    <?php

    // ...

    static public function fromArray(array $data)
    {
    $o = new self();

    $reflection = new \ReflectionClass($o);
    $properties = $reflection->getProperties();
    $properties = array_map(function ($v) { /* @var $v \ReflectionProperty */
    return $v->getName();
    }, $properties);

    if (array_key_exists('id', $properties)) { unset($properties['id']); }

    foreach ($properties as $k) {
    $setter = sprintf('set%s', ucwords($k));

    if (array_key_exists($k, $data) && method_exists($o, $setter)) {
    $o->{$setter}($data[$k]);
    }
    }

    return $o;
    }

    // ...

    ?>
  6. zebba created this gist Jul 23, 2014.
    30 changes: 30 additions & 0 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    <?php

    // ...

    static public function fromArray(array $data)
    {
    $o = new self();

    $reflection = new \ReflectionClass($o);
    $properties = $reflection->getProperties();
    $properties = array_map(function ($v) { /* @var $v \ReflectionProperty */
    return $v->getName();
    }, $properties);

    if (array_key_exists('id', $properties)) { unset($properties['id']); }

    foreach ($properties as $k) {
    $setter = sprintf('set%s', ucwords($k));

    if (array_key_exists($k, $data) && method_exists($o, $setter)) {
    $o->{$setter}($data[$k]);
    }
    }

    return $o;
    }

    // ...

    ?>