coen-hyde / Shanty-Mongo

Shanty Mongo is a mongodb library for the Zend Framework. Its intention is to make working with mongodb documents as natural and as simple as possible. In particular allowing embedded documents to also have custom document classes.
Other
200 stars 52 forks source link

Filter issue #36

Open tholder opened 13 years ago

tholder commented 13 years ago

What am I doing wrong with this filter?

<? class Name extends Shanty_Mongo_Document {

protected static $_requirements = array(
    'forename' => array('Filter:Zend_Filter_StringTrim')
);

..

I keep getting "No requirement exists for 'Filter:Zend_Filter_StringTrim'" thrown. I've tried adding in 'Required' and that doesn't seem to work.

The documentation is great but a little sparse on Filters so perhaps I'm justing being stupid!

Thanks Tom

tholder commented 13 years ago

Just realised I had an auto loader issue. Seems like the code is always looking for ZendFilter meaning the correct syntax is:

protected static $_requirements = array( 'forename' => array('Filter:StringTrim') );

This strikes me as a bit daft because it means I can't write my own custom filters unless I dump them in to my Zend lib?

Thanks, Tom

coen-hyde commented 13 years ago

Hi Tom,

You can actually write your own filters but it's a little tedious at the moment. I've been meaning to fix this so you can provide a class prefix or dir of your own filters.

To write your own you would first need to write your own requirement creator. Look in the Shanty_Mongo::init() method for how the current requirement creators work.

Cheers, Coen

tholder commented 13 years ago

Cool thanks. Would it not be enough in that method to replace:

    static::storeRequirementCreator('/^Validator:([A-Za-z]+[\w\-:]*)$/', function($data, $options = null) {
        $instanceClass = 'Zend_Validate_'.$data[1];

With:

    static::storeRequirementCreator('/^Validator:([A-Za-z]+[\w\-:]*)$/', function($data, $options = null) {
        $instanceClass = $data[1];

It should load Zend_Validate_StringTrim based on the namespacing?

Sorry, I guess there's good reason, I'll dig deeper and try and get a better understanding.

Tom

coen-hyde commented 13 years ago

Shanty Mongo doesn't use namespacing currently but even if it did the current namespace would be Shanty/Mongo so I don't believe 'StringTrim' would load Zend_Validate_StringTrim.

You can easily override the default validator/filter requirement creator by providing our own function to do exactly what you want.

Shanty_Mongo::storeRequirementCreator('/^Validator:([A-Za-z]+[\w-:]*)$/', function($data, $options = null) { $instanceClass = $data[1]; // etc });

If you come up with an elegant solution to protect backwards compatibility and also allow the ability to provide your own classes please let me know.

tholder commented 13 years ago

Sorry my use of the term namespacing was wrong. What I mean is, in a correctly setup zend project, the auto loading should take care of it.

I will try what you suggested though.

Thanks for the help.

coen-hyde commented 13 years ago

Ah yes. And in fact that is an assumption that Shanty Mongo makes. The reason it was implemented the way it was, was to save time when writing requirements so you didn't have to type out the full class name which imo would get very repetitive after a while. Maybe a list of Zend filters/validators need to exist and if the data doesn't match any of those then assume it's a class name.

tholder commented 13 years ago

To be honest, whilst it's a bit of a pain they're long the class names, it makes refactoring really simple to have the full names in. I've just gone through a major refactor of a codebase and having generic class names that are found all over has killed me. I'd rather just specify the full zend name and then I know what it's doing at my level of the code rather than going in under the hood.

I develop like an idiot for idiots :)