Skip to content

Commit

Permalink
Merge pull request #5 from gdubost/cr/133910
Browse files Browse the repository at this point in the history
[CR#133910] Ajout du trait setProperties
  • Loading branch information
Guillaume DUBOST authored Apr 17, 2019
2 parents 0333779 + bc8e0be commit 3a6d902
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
19 changes: 19 additions & 0 deletions Tests/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use ETNA\Doctrine\Extensions\CreatedAt;
use ETNA\Doctrine\Extensions\UpdatedAt;
use ETNA\Doctrine\Extensions\DoNotDelete;
use ETNA\Doctrine\Extensions\SetProperties;

/**
* @Entity(repositoryClass="Tests\ModelRepository")
Expand All @@ -18,6 +19,24 @@ class Model implements \JsonSerializable
use CreatedAt;
use UpdatedAt;
use DoNotDelete;
use SetProperties;

private $model_value;

public function __construct()
{
$this->model_value = '';
}

public function setModelValue($value)
{
$this->model_value = $value;
}

public function getModelValue()
{
return $this->model_value;
}

public function populate($date)
{
Expand Down
7 changes: 7 additions & 0 deletions Tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,11 @@ public function testSetDeletedAt()
// On récupère un objet
$this->assertEquals($this->datetime, $this->model->getDeletedAt());
}

public function testSetProperties()
{
$this->assertEquals($this->model->getModelValue(), '');
$this->model->setProperties(["model_value" => "super value"]);
$this->assertEquals($this->model->getModelValue(), "super value");
}
}
6 changes: 5 additions & 1 deletion phpmd.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
xsi:noNamespaceSchemaLocation=" http://pmd.sf.net/ruleset_xml_schema.xsd">
<description> phpmd ruleset for doctrine extensions</description>
<rule ref="rulesets/codesize.xml" />
<rule ref="rulesets/controversial.xml" />

<rule ref="rulesets/controversial.xml/Superglobals" />
<rule ref="rulesets/controversial.xml/CamelCaseClassName" />
<rule ref="rulesets/controversial.xml/CamelCaseMethodName" />

<rule ref="rulesets/design.xml" />
<!-- <rule ref="rulesets/naming.xml" /> -->
<rule ref="rulesets/naming.xml/ShortVariable">
Expand Down
28 changes: 28 additions & 0 deletions src/SetProperties.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace ETNA\Doctrine\Extensions;

/**
* Ce trait permet aux classes concernées de setter ses propriétés depuis un array associatif
*/
trait SetProperties
{
/**
* Sette toute les valeurs d'un array associatif à $this.
*
* @param array $request_data
*
* @return self
*/
public function setProperties(array $request_data)
{
foreach ($request_data as $field_name => $field_value) {
$setter_name = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $field_name)));
if (method_exists($this, $setter_name)) {
$this->{$setter_name}($field_value);
}
}

return $this;
}
}

0 comments on commit 3a6d902

Please sign in to comment.