Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closure for "reserved" option does not work #70

Closed
nimbol opened this issue Oct 31, 2014 · 1 comment
Closed

Closure for "reserved" option does not work #70

nimbol opened this issue Oct 31, 2014 · 1 comment

Comments

@nimbol
Copy link

nimbol commented Oct 31, 2014

Hi,

I've implemented the SluggableTrait on one of my classes and I wanted to dynamically get the reserved slugs using a closure, like so:

<?php

namespace Project\Page\Model;

use Cviebrock\EloquentSluggable\SluggableInterface;
use Cviebrock\EloquentSluggable\SluggableTrait;

class StaticPage extends \Eloquent implements SluggableInterface
{

    use SluggableTrait;

    protected $sluggable = array(
        'build_from' => ['title'],
        'save_to'    => 'slug',
        'separator'  => '-',
    );

    public function __construct(array $attributes = array())
    {
        parent::__construct($attributes);

        $this->sluggable['reserved'] = function() {
            $result = [];
            // ??? -> $$$
            return $result;
        };
    }

}

// And the following is added just so you can see it in action:
$page = new StaticPage;
$page->title = 'About';
$page->sluggify();

Now, when trying to generate the slug, an UnexpectedValueException is thrown: Sluggable reserved is not null, an array, or a closure that returns null/array.

This happens because the class (or the trait) is namespaced, but the type checks (SluggableTrait line 57 and 87) say:

        // check for reserved names
        if ( $reserved instanceof Closure )

Thanks to PHP's namespaces, this should have been:

        // check for reserved names
        if ( $reserved instanceof \Closure )

I can submit a pull request with the fix for this, but I cannot provide unittests because I can't get them to work on my machine.

Cheers!

@cviebrock
Copy link
Owner

Yeah, that looks like a simple oversight on my part. MR it and I’ll accept.

Thanks!

On Oct 31, 2014, at 10:10 AM, nimbol notifications@github.com wrote:

Hi,

I've implemented the SluggableTrait on one of my classes and I wanted to dynamically get the reserved slugs using a closure, like so:

['title'], 'save_to' => 'slug', 'separator' => '-', ); public function __construct(array $attributes = array()) { parent::__construct($attributes); $this->sluggable['reserved'] = function() { $result = []; // ??? -> $$$ return $result; }; } ``` } // And the following is added just so you can see it in action: $page = new StaticPage; $page->title = 'About'; $page->sluggify(); Now, when trying to generate the slug, an UnexpectedValueException is thrown: Sluggable reserved is not null, an array, or a closure that returns null/array. This happens because the class (or the trait) is namespaced, but the type checks (SluggableTrait line 57 and 87) say: ``` // check for reserved names if ( $reserved instanceof Closure ) ``` Thanks to PHP's namespaces, this should have been: ``` // check for reserved names if ( $reserved instanceof \Closure ) ``` I can submit a pull request with the fix for this, but I cannot provide unittests because I can't get them to work on my machine. Cheers! — Reply to this email directly or view it on GitHub.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants