Skip to content

Instantly share code, notes, and snippets.

@rdohms
Forked from chartjes/RpcApi.php
Created February 23, 2012 21:18
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 rdohms/1895105 to your computer and use it in GitHub Desktop.
Save rdohms/1895105 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 validateProducts($storeData) {
// Check to make sure that our valid fields are in there
$requiredFields = array('price','name','description','type');
foreach ($storeData['products'] as $rawProduct) {
if ( ! validateSingleProduct($rawProduct, $requiredFields)) return false;
}
return true;
}
function validateSingleProduct($product, $requiredFields)
{
$fields = array_keys($rawProduct);
$missingFields = array_diff($requiredFields, $fields);
if (count($missingFields) > 0) return false;
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment