diff --git a/README.md b/README.md index 6854f99..57c641a 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # fos-rest-behavior +TODO diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..666ef68 --- /dev/null +++ b/composer.json @@ -0,0 +1,13 @@ +{ + "name": "deblan/fos-rest-behavior", + "type": "propel-behavior", + "authors": [ + { + "name": "Simon Vieille", + "email": "simon@deblan.fr" + } + ], + "require": { + "jms/serializer-bundle": "~1.1" + } +} diff --git a/src/Deblan/Propel/Behavior/.FOSRestBehavior.php.swo b/src/Deblan/Propel/Behavior/.FOSRestBehavior.php.swo new file mode 100644 index 0000000..cd188de Binary files /dev/null and b/src/Deblan/Propel/Behavior/.FOSRestBehavior.php.swo differ diff --git a/src/Deblan/Propel/Behavior/FOSRestBehavior.php b/src/Deblan/Propel/Behavior/FOSRestBehavior.php new file mode 100644 index 0000000..cff8cdd --- /dev/null +++ b/src/Deblan/Propel/Behavior/FOSRestBehavior.php @@ -0,0 +1,136 @@ + + */ +class FOSRestBehavior extends Behavior +{ + /** + * {@inheritdoc} + */ + public function objectFilter(&$script) + { + $script = preg_replace( + '#(\s+)\*/(\s+)abstract class#', + '$1'.$this->getClassAnnotations().'*/$2abstract class', + $script + ); + } + + /** + * {@inheritdoc} + */ + public function objectMethods() + { + $script = ''; + + foreach ($this->getTable()->getColumns() as $column) { + $name = ucfirst($column->getName()); + + try { + $groups = $this->transformGroups($this->getParameter($column->getName())); + $getter = $this->getColumnGetter($column); + $newGetter = preg_replace('/^get/', 'getRest', $getter); + $script.= $this->generateRestMethod($column, $newGetter, $getter, $groups); + } catch (Exception $e) { + } + } + + return $script; + } + + /** + * Transform the group list to parameter format of annotation + * + * @param string $groups + * @return string + */ + protected function transformGroups($groups) + { + $data = []; + + foreach (explode(',', $groups) as $group) { + $group = trim($group); + + if ('' !== $group) { + $data[] = '"'.$group.'"'; + } + } + + return implode(', ', $data); + } + + /** + * Generates rest method + * + * @param Column $column + * @param string $newGetter + * @param string $getter + * @param string $groups + * @return string + */ + public function generateRestMethod(Column $column, $newGetter, $getter, $groups) + { + $annotations = [ + '/**', + ' * @JMS\Serializer\Annotation\SerializedName("'.$column->getName().'")', + ]; + + if ($groups) { + $annotations[] = ' * @JMS\Serializer\Annotation\Groups('.$groups.')'; + } + + $annotations[] = ' * @JMS\Serializer\Annotation\VirtualProperty'; + $annotations[] = '*/'; + + $annotations = implode("\n", $annotations); + + $method = <<$getter(); +} + + +EOS; + + return $method; + } + + /** + * Get the getter of one of the columns of the behavior + * + * @param string $column One of the behavior columns, 'create_column' or 'update_column' + * @return string The related getter, 'getCreatedOn' or 'getUpdatedOn' + */ + protected function getColumnGetter($column) + { + return 'get' . $column->getPhpName(); + } + + /** + * Generates code for annotations + * + * @return string + */ + protected function getClassAnnotations() + { + $annotations = []; + + foreach (['@JMS\Serializer\Annotation\ExclusionPolicy("all")'] as $annotation) { + $annotations[] = ' * '.$annotation; + } + + return sprintf("%s\n", implode("\n", $annotations)); + } +} +