Permalink
Browse files

Fixed CS for sluggable behavior

  • Loading branch information...
1 parent e76f854 commit 66f9eda293039015ffbba4e94f82dc3c5859d657 @willdurand willdurand committed Jun 11, 2012
@@ -18,69 +18,68 @@
*/
class SluggableBehavior extends Behavior
{
- // default parameters value
- protected $parameters = array(
- 'slug_column' => 'slug',
- 'slug_pattern' => '',
- 'replace_pattern' => '/\W+/', // Tip: use '/[^\\pL\\d]+/u' instead if you're in PHP5.3
- 'replacement' => '-',
- 'separator' => '-',
- 'permanent' => 'false',
- 'scope_column' => ''
- );
-
- /**
- * Add the slug_column to the current table
- */
- public function modifyTable()
- {
- if(!$this->getTable()->containsColumn($this->getParameter('slug_column'))) {
- $this->getTable()->addColumn(array(
- 'name' => $this->getParameter('slug_column'),
- 'type' => 'VARCHAR',
- 'size' => 255
- ));
- // add a unique to column
- $unique = new Unique($this->getColumnForParameter('slug_column'));
- $unique->setName($this->getTable()->getCommonName() . '_slug');
- $unique->addColumn($this->getTable()->getColumn($this->getParameter('slug_column')));
-
+ // default parameters value
+ protected $parameters = array(
+ 'slug_column' => 'slug',
+ 'slug_pattern' => '',
+ 'replace_pattern' => '/\W+/', // Tip: use '/[^\\pL\\d]+/u' instead if you're in PHP5.3
+ 'replacement' => '-',
+ 'separator' => '-',
+ 'permanent' => 'false',
+ 'scope_column' => ''
+ );
+
+ /**
+ * Add the slug_column to the current table
+ */
+ public function modifyTable()
+ {
+ if (!$this->getTable()->containsColumn($this->getParameter('slug_column'))) {
+ $this->getTable()->addColumn(array(
+ 'name' => $this->getParameter('slug_column'),
+ 'type' => 'VARCHAR',
+ 'size' => 255
+ ));
+ // add a unique to column
+ $unique = new Unique($this->getColumnForParameter('slug_column'));
+ $unique->setName($this->getTable()->getCommonName() . '_slug');
+ $unique->addColumn($this->getTable()->getColumn($this->getParameter('slug_column')));
if ($this->getParameter('scope_column')) {
$unique->addColumn($this->getTable()->getColumn($this->getParameter('scope_column')));
- }
- $this->getTable()->addUnique($unique);
- }
- }
-
- /**
- * Get the getter of the column of the behavior
- *
- * @return string The related getter, e.g. 'getSlug'
- */
- protected function getColumnGetter()
- {
- return 'get' . $this->getColumnForParameter('slug_column')->getPhpName();
- }
-
- /**
- * Get the setter of the column of the behavior
- *
- * @return string The related setter, e.g. 'setSlug'
- */
- protected function getColumnSetter()
- {
- return 'set' . $this->getColumnForParameter('slug_column')->getPhpName();
- }
-
- /**
- * Add code in ObjectBuilder::preSave
- *
- * @return string The code to put at the hook
- */
- public function preSave($builder)
- {
- $const = $builder->getColumnConstant($this->getColumnForParameter('slug_column'));
- $script = "
+ }
+ $this->getTable()->addUnique($unique);
+ }
+ }
+
+ /**
+ * Get the getter of the column of the behavior
+ *
+ * @return string The related getter, e.g. 'getSlug'
+ */
+ protected function getColumnGetter()
+ {
+ return 'get' . $this->getColumnForParameter('slug_column')->getPhpName();
+ }
+
+ /**
+ * Get the setter of the column of the behavior
+ *
+ * @return string The related setter, e.g. 'setSlug'
+ */
+ protected function getColumnSetter()
+ {
+ return 'set' . $this->getColumnForParameter('slug_column')->getPhpName();
+ }
+
+ /**
+ * Add code in ObjectBuilder::preSave
+ *
+ * @return string The code to put at the hook
+ */
+ public function preSave($builder)
+ {
+ $const = $builder->getColumnConstant($this->getColumnForParameter('slug_column'));
+ $script = "
if (\$this->isColumnModified($const) && \$this->{$this->getColumnGetter()}()) {
\$this->{$this->getColumnSetter()}(\$this->makeSlugUnique(\$this->{$this->getColumnGetter()}()));";
if ($this->getParameter('permanent') == 'true') {
@@ -269,28 +268,28 @@ public function addMakeSlugUnique(&$script)
*/
protected function makeSlugUnique(\$slug, \$separator = '" . $this->getParameter('separator') ."', \$increment = 0)
{
- \$slug2 = empty(\$increment) ? \$slug : \$slug . \$separator . \$increment;
- \$slugAlreadyExists = " . $this->builder->getStubQueryBuilder()->getClassname() . "::create()
- ->filterBySlug(\$slug2)
- ->prune(\$this)";
-
- if($this->getParameter('scope_column')) {
- $getter = 'get' . $this->getColumnForParameter('scope_column')->getPhpName();
- $script .="
- ->filterBy('{$this->getColumnForParameter('scope_column')->getPhpName()}', \$this->{$getter}())";
- }
- // watch out: some of the columns may be hidden by the soft_delete behavior
- if ($this->table->hasBehavior('soft_delete')) {
- $script .= "
- ->includeDeleted()";
- }
- $script .= "
- ->count();
- if (\$slugAlreadyExists) {
- return \$this->makeSlugUnique(\$slug, \$separator, ++\$increment);
- } else {
- return \$slug2;
- }
+ \$slug2 = empty(\$increment) ? \$slug : \$slug . \$separator . \$increment;
+ \$slugAlreadyExists = " . $this->builder->getStubQueryBuilder()->getClassname() . "::create()
+ ->filterBySlug(\$slug2)
+ ->prune(\$this)";
+
+ if ($this->getParameter('scope_column')) {
+ $getter = 'get' . $this->getColumnForParameter('scope_column')->getPhpName();
+ $script .="
+ ->filterBy('{$this->getColumnForParameter('scope_column')->getPhpName()}', \$this->{$getter}())";
+ }
+ // watch out: some of the columns may be hidden by the soft_delete behavior
+ if ($this->table->hasBehavior('soft_delete')) {
+ $script .= "
+ ->includeDeleted()";
+ }
+ $script .= "
+ ->count();
+ if (\$slugAlreadyExists) {
+ return \$this->makeSlugUnique(\$slug, \$separator, ++\$increment);
+ } else {
+ return \$slug2;
+ }
}
";
}
@@ -270,8 +270,8 @@ public function testUniqueViolationWithoutScope()
$this->fail('Exception expected');
} catch (Exception $e) {
- $this->assertTrue(true, 'Exception successfully thrown');
- }
+ $this->assertTrue(true, 'Exception successfully thrown');
+ }
}
public function testNoUniqueViolationWithScope()

0 comments on commit 66f9eda

Please sign in to comment.