Skip to content

Instantly share code, notes, and snippets.

@guilhermeblanco
Forked from rdohms/RpcApi.php
Created February 26, 2012 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save guilhermeblanco/1918391 to your computer and use it in GitHub Desktop.
Save guilhermeblanco/1918391 to your computer and use it in GitHub Desktop.
<?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