Verifying elements for an item type

I have a plugin that uses a custom item type containing some custom elements. In some cases, the type and elements will be added via a plugin and in some cases, they may be added manually.

I'm looking for a method to verify 1) that the item type and elements exist, and 2) that they are associated.

For step one, I know I can use element_exists() and get_record() but, for step two, how can I ensure that the elements are assigned to the type?

Thanks -- E

PS: by "assigned," I mean that if the user creates an item with the custom item type, the item admin form will reveal the custom elements for the type.

Answering my own question, looks like the best/only way would be to list the elements for the type like so, creating an array of assigned elements ...

$itemType=get_record('ItemType',array('name'=>'My Item Type'));
$elements=$itemType->Elements;

Ok, while the above solution still holds, I wonder if I may have hit a bug of some sort in implementation.

I'm looping through my list of required elements and verifying that they are assigned to the custom item type using the code below.

$it=get_record('ItemType',array('name'=>'My Item Type'));
$type_exists = (bool)$it;
$text_type = ($type_exists) ? '<li>The item type exists.</li>' : '<li>Please create the item type.</li>';
$missing_elements=0;
$text_elements=null;

if($type_exists){

	// array of currently assigned elements
	$elementsForType=$it->Elements;
	$a=array();
	foreach($elementsForType as $e) {
		$a[]=$e['name'];
	}

	// array of required elements
	$reqElementsForType = array('Subtitle', 'Lede', 'Story', 'Sponsor', 'Factoid', 'Related Resources', 'Official Website', 'Street Address', 'Access Information');

	// check to see if the required elements exist AND are assigned to the item type
	foreach($reqElementsForType as $element){
		if(element_exists('Item Type Metadata',$element)){
			$assigned=(bool)array_search($element, $a);
			$text_elements .='<li>The "'.$element.'" element exists'.($assigned ? ' and is properly assigned' : ' but MAY NOT BE PROPERLY ASSIGNED to the item type').'. </li>';
		}else{
			$missing_elements++;
			$text_elements .='<li>The "'.$element.'" element does not exist. Please update the item type record to include the element "'.$element.'"</li>';
		}
	}
}

For some reason, I'm getting a false negative on the assignment for the 'Subtitle' element, which exists and is assigned but is coming back as not assigned.

I checked the database and verified that the element_id (95) is indeed assigned to the item_type_id (24). It looks like all the other assigned elements in that regard.

id 	item_type_id 	element_id 		order
128 	24 			95 			1
129 	24 			96 			2
130 	24 			97 			3
131 	24 			98 			4
132 	24 			99 			5
133 	24 			100 			6
134 	24 			101 			7
135 	24 			102 			8
136 	24 			103 			9

So I'm sort of stumped as to what might be causing this result. Any ideas?

Oops, I was using array_search() – which returns the key/position – instead of in_array() – which returns the value. Since I was converting the results using (bool), the first item (with position 0) was interpreted by PHP as false. In other words...

$assigned=(bool)array_search($element, $a); // NO!
$assigned=(bool)in_array($element, $a); // YES!