From c9db5474dfc42d1e24239fa28d7e2ae8fb107d17 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Sun, 20 Mar 2016 17:23:57 +0100 Subject: [PATCH] init --- README.md | 1 + composer.json | 13 ++ .../Propel/Behavior/.FOSRestBehavior.php.swo | Bin 0 -> 12288 bytes .../Propel/Behavior/FOSRestBehavior.php | 136 ++++++++++++++++++ 4 files changed, 150 insertions(+) create mode 100644 composer.json create mode 100644 src/Deblan/Propel/Behavior/.FOSRestBehavior.php.swo create mode 100644 src/Deblan/Propel/Behavior/FOSRestBehavior.php 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 0000000000000000000000000000000000000000..cd188de79beb9a0deabf3330fa77b312870b66d5 GIT binary patch literal 12288 zcmeI2Uu+ab9LEO`MHCg(hzT*nakzJ-y#ti^r=?a98cj+YS}@WpWV<)lTin}Scjstp zK{UQ-^g(nEhZVpt#dT<1`g*SzB75Xgl8S@wm7z-E+7z-E+7z-E+7z-E+ z7z_N5EWpDhNP-~h;jbzmiU`&vRc*aW`3 zhLAJhW$+?60j5C=B5)Yw00qmy?<+9|@Gf`@oB}5S13SPyU@f?KHTn(CgKxlDa0Z+P zBVZ5M1~TB>3PL^vAAr-~VK4ytz@@7Q`4*f9uYp&=GhhZB1SQZ5dO$b07i z5OaSMSPm{PBjhh|348@U1Mh${;3zl(M!^BF1bhwO`4oHto&`^Xr@)ioI9RkCBNRnl z#j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\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)); + } +} +