-
-
Save guilhermeblanco/1918391 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($storeData) { | |
// Check to make sure that our valid fields are in there | |
$requiredFields = array( | |
'price', | |
'name', | |
'description', | |
'type', | |
); | |
$valid = true; | |
foreach ($storeData['products'] as $rawProduct) { | |
$fields = array_keys($rawProduct); | |
foreach ($requiredFields as $requiredField) { | |
if (!in_array($requiredField, $fields)) { | |
$valid = false; | |
} | |
} | |
} | |
return $valid; | |
} | |
?> | |
// ---------------- REWRITTEN | |
<?php | |
function validateProductList($storeData) | |
{ | |
$validatedProductList = array_filter($storeData['products'], 'validateProduct'); | |
// If the amount of products differ from validated products, one of them failed | |
return (count($storeData['products']) === count($validatedProductList)); | |
} | |
function validateProduct($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