Results 1 to 5 of 5

Thread: Validating that keys exist in an array

  1. #1
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default Validating that keys exist in an array

    I want to provide an array of parameters to a function. I am not so concerned with the arrays values as I am with the keys, as validation on the values will have been done prior. Actually it is unlikely that the array will be missing any values by this point, but I thought I better do at least a little validation here.

    Here is what I have. Is there a better way.

    PHP Code:
        public function addLink($params) {
            
            if ( !
    is_array($params) )
                throw new 
    Exception('addLink must receive an array.');
                
            
            
    $validKeys = array('l_url''l_title''l_description');
            
            foreach ( 
    $validKeys as $key ) {
                
                if ( !
    array_key_exists($key$params))
                    throw new 
    Exception('Check the $params supplied to addLink()');
                    
            }

            
    $this->insert($params); 
        
        } 

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    PHP convention would have you just ignore extraneous keys. Use the ones you need, and discard any others.

    Additionally, I think the 'l_' prefix is a little redundant. The function is called addLink(); you're hardly going to be talking about the title of a book of Greek philosophy, are you?

    You can discard unnecessary keys by combining array_intersect_key() with array_fill_keys():
    Code:
    function array_only($arr, $keys) {
      return array_intersect_key($arr, array_fill_keys($keys, true));
    }
    Your function then becomes:
    Code:
        public function addLink($params) {
            if (!is_array($params))
                throw new Exception('addLink must receive an array.');
    
            return $this->insert(array_only($params, array('url', 'title', 'description')));
        }
    Last edited by Twey; 02-15-2009 at 11:31 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    I like the idea.

    The keys are database columns, so I have to use the l_ prefix.

    Requirements:

    l_url, l_description, l_title MUST be keys in the array.
    They must have values.

    PHP Code:
    insert(array('l_url'=>'www.dynamicdrive.com',
        
    'l_description'=>'Great Forums',
        
    'l_title'=>'Dynamic Drive')
    ); 
    Will work. I don't think anything else will.

    This class is part of the Model, so perhaps I am misguided in even trying to do any validation here. I'm just getting into real Model View Controller architecture. I have a lot to figure out.

    What do you think Twey? I am starting to think that the validation shouldn't be here at all.

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    The keys are database columns, so I have to use the l_ prefix.
    Just because you have to use it internally doesn't mean you have to expose it to the user. Add them on later.
    This class is part of the Model, so perhaps I am misguided in even trying to do any validation here. I'm just getting into real Model View Controller architecture. I have a lot to figure out.
    The arguments surrounding MVC and validation are complex, and no-one's entirely agreed on how it's best done. Putting the validation in the models is a valid choice (the usual approach, I believe, is to let the developer add whatever they like to the model, and have an isValid() call or similar to tell them whether they got it right; of course, trying to save the model when the data is invalid should result in an exception). This isn't really validation in that sense, though. Validation is about user input; what you're 'validating' here is a different kind of input, from the user of your library rather than the user of your application. The user of your library is a developer, running it on their own server, and therefore can be trusted to provide sane input. Any 'validation' you do is just to ease debugging.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    I don't know if you are familar with Zend Framework, but this is what I have been trying to learn for the past couple days.

    As chance would have it, I just received this in an email from the ZF mailing list. Someone else was asking a similar question to mine.
    PHP Code:
    public function insert(array $data)
       {
           
    $data array_intersect_key($dataarray_fill_keys($this->info('cols'), ''));
           return 
    parent::insert($data);
       }

       public function 
    update(array $data$where)
       {
           
    $data array_intersect_key($dataarray_fill_keys($this->info('cols'), ''));
           return 
    parent::update($data$where);
       } 
    In the above example $this is an instance of Zend_Db_Table. Basicly it turns a table from your database into an object. All it takes to do this is extending Zend_Db_Table, giving the new class the same name as your table, and supplying one variable that has the same name as your table. It is two lines of code, then you can call $this->insert() like above to do an insert query as long as you are feeding it an array with key values that match your column names. This is Awesome.

    J

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •