-
-
Save rdohms/1918623 to your computer and use it in GitHub Desktop.
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
<?php | |
function validateProducts($products) { | |
// Check to make sure that our valid fields are in there | |
$requiredFields = array( | |
'price', | |
'name', | |
'description', | |
'type', | |
); | |
$valid = true; | |
foreach ($products as $rawProduct) { | |
$fields = array_keys($rawProduct); | |
foreach ($requiredFields as $requiredField) { | |
if (!in_array($requiredField, $fields)) { | |
$valid = false; | |
} | |
} | |
} | |
return $valid; | |
} | |
?> | |
// ---------------- REWRITTEN | |
<?php | |
function validateProductList($products) | |
{ | |
$invalidProducts = array_filter($products, 'isInvalidProduct'); | |
return (count($invalidProducts) === 0); | |
} | |
function isInvalidProduct($rawProduct) | |
{ | |
$requiredFields = array('price', 'name', 'description', 'type'); | |
$fields = array_keys($rawProduct); | |
$missingFields = array_diff($requiredFields, $fields); | |
return (count($missingFields) > 0); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment