Created
January 7, 2011 15:09
-
-
Save andho/769565 to your computer and use it in GitHub Desktop.
Demonstrating check for required fields with use of an array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public function __construct($data) { | |
| $address = array('name', 'post_code'); | |
| $business = array('name', 'address', 'post_code', 'intro'); | |
| if ($data['type] == 'business') $fields = $business; | |
| else $fields = $address; | |
| if (count(array_intersect($fields, array_keys($data))) != count($fields)) { | |
| throw new Exception("Required data was not supplied to the Address object"); | |
| } | |
| $keys = array_intersect($fields, array_keys($data)); | |
| $data2 = array(); | |
| foreach ($keys as $key) { | |
| $data2[$key] = $data[$key]; | |
| } | |
| foreach ($data2 as $key=>$value) { | |
| $method = 'set' . ucfirst($key); | |
| $this->$method($value); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment