/**
 * Retrieves the list of supported properties for the specified type.
 * @uses wl_entity_taxonomy_get_custom_fields() to retrieve all custom fields (type properties)
 * @uses wl_build_full_schema_uri_from_schema_slug() to convert a schema slug to full uri
 *
 * @param $type_name string Name of the type (e.g. Type, for the http://schema.org/Type)
 *
 * @return array The method returns an array of supported properties for the type, e.g. (‘startDate’, ‘endDate’) for an Event.
 * You can call wl_schema_get_property_expected_type on each to know which data type they expect.
 */
function wl_schema_get_type_properties($type_name)
{
    // Build full schema uri if necessary
    $type_name = wl_build_full_schema_uri_from_schema_slug($type_name);
    // Get all custom fields
    $all_types_and_fields = wl_entity_taxonomy_get_custom_fields();
    $schema_root_address = 'http://schema.org/';
    $type_properties = array();
    // Search for the entity type which has the requested name as uri
    if (isset($all_types_and_fields[$type_name])) {
        foreach ($all_types_and_fields[$type_name] as $field) {
            // Convert to schema slug and store in array
            $type_properties[] = str_replace($schema_root_address, '', $field['predicate']);
        }
    }
    return $type_properties;
}
/**
 * Retrieve entity property constraints, starting from the schema.org's property name
 * or from the WL_CUSTOM_FIELD_xxx name.
 *
 * @param $property_name as defined by schema.org or WL internal constants
 *
 * @return array containing constraint(s) or null (in case of error or no constraint).
 */
function wl_get_meta_constraints($property_name)
{
    // Property name must be defined.
    if (!isset($property_name) || is_null($property_name)) {
        return null;
    }
    // store eventual schema name in  different variable
    $property_schema_name = wl_build_full_schema_uri_from_schema_slug($property_name);
    // Get WL taxonomy mapping.
    $types = wl_entity_taxonomy_get_custom_fields();
    // Loop over types
    foreach ($types as $type) {
        // Loop over custom fields of this type
        foreach ($type as $property => $field) {
            if (isset($field['constraints']) && !empty($field['constraints'])) {
                // Is this the property we are searhing for?
                if ($property == $property_name || $field['predicate'] == $property_schema_name) {
                    return $field['constraints'];
                }
            }
        }
    }
    return null;
}
/**
 * Retrieves the property expected type, according to the schema.org specifications, where:
 * 
 * @param $property_name string Name of the property (e.g. name, for the http://schema.org/name property)
 * 
 * @return array of allowed types or NULL in case of property not found.
 * 
 * The following types are supported (defined as constants):
 * - WL_DATA_TYPE_URI
 * - WL_DATA_TYPE_DATE
 * - WL_DATA_TYPE_INTEGER
 * - WL_DATA_TYPE_DOUBLE
 * - WL_DATA_TYPE_BOOLEAN
 * - WL_DATA_TYPE_STRING
 * - a schema.org URI when the property type supports a schema.org entity (e.g. http://schema.org/Place)
 */
function wl_schema_get_property_expected_type($property_name)
{
    // This is the actual structure of a custom_field.
    /*
     * WL_CUSTOM_FIELD_LOCATION       => array(
     *      'predicate'   => 'http://schema.org/location',
     *      'type'        => WL_DATA_TYPE_URI,
     *      'export_type' => 'http://schema.org/PostalAddress',
     *      'constraints' => array(
     *              'uri_type' => 'Place'
     *      )
     *  )
     */
    // Build full schema uri if necessary
    $property_name = wl_build_full_schema_uri_from_schema_slug($property_name);
    // Get all custom fields
    $all_types_and_fields = wl_entity_taxonomy_get_custom_fields();
    $expected_types = null;
    // Search for the entity type which has the requested name as uri
    $found = false;
    foreach ($all_types_and_fields as $type_fields) {
        foreach ($type_fields as $field) {
            if ($field['predicate'] == $property_name) {
                $expected_types = array();
                // Does the property accept a specific schema type?
                if (isset($field['constraints']) && isset($field['constraints']['uri_type'])) {
                    // Take note of expected schema type
                    $expected_types[] = wl_build_full_schema_uri_from_schema_slug($field['constraints']['uri_type']);
                } else {
                    // Take note of expected type
                    $expected_types[] = $field['type'];
                }
                // We found the property, we can exit the cycles
                $found = true;
            }
            if ($found) {
                break;
            }
        }
        if ($found) {
            break;
        }
    }
    return $expected_types;
}