Source for file microdata.php
Documentation is available at microdata.php
* @package Joomla.Platform
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
* Joomla Platform class for interacting with Microdata semantics.
* @package Joomla.Platform
* Array with all available Types and Properties
protected static $types =
null;
* The Human value or Machine value
* Used to check if a Fallback must be used
* Used to check if the Microdata semantics output are enabled or disabled
* Initialize the class and setup the default Type
* @param string $type Optional, Fallback to Thing Type
* @param boolean $flag Enable or disable microdata output
if ($this->enabled = (boolean)
$flag)
// Fallback to Thing Type
* Load all Types and Properties from the types.json file
$path =
JPATH_PLATFORM .
'/joomla/microdata/types.json';
static::$types =
json_decode(file_get_contents($path), true);
* Enable or Disable Microdata semantics output
* @param boolean $flag Enable or disable microdata output
* @return JMicrodata Instance of $this
public function enable($flag =
true)
* Return true if Microdata semantics output are enabled
return ($this->enabled) ?
true :
false;
* Set a new Schema.org Type
* @param string $type The Type to be setup
* @return JMicrodata Instance of $this
$this->type =
static::sanitizeType($type);
// If the given Type isn't available, fallback to Thing
if (!static::isTypeAvailable($this->type))
* Return the current Type name
* @param string $property The Property
* @return JMicrodata Instance of $this
$property =
static::sanitizeProperty($property);
// Control if the Property exist in the given Type and setup it, if not leave NULL
if (static::isPropertyInType($this->type, $property))
$this->property =
$property;
* Return the property variable
* Setup a Text value or Content value for the Microdata
* @param string $value The human value or marchine value to be used
* @param string $machineValue The machine value
* @return JMicrodata Instance of $this
public function content($value, $machineValue =
null)
* Return the content variable
* Setup a Fallback Type and Property
* @param string $type The Fallback Type
* @param string $property The Fallback Property
* @return JMicrodata Instance of $this
public function fallback($type, $property)
// If the given Type isn't available, fallback to Thing
if (!static::isTypeAvailable($this->fallbackType))
$this->fallbackType =
'Thing';
// Control if the Property exist in the given Type and setup it, if not leave NULL
if (static::isPropertyInType($this->fallbackType, $property))
$this->fallbackProperty =
$property;
$this->fallbackProperty =
null;
* Return the fallbackType variable
* Return the fallbackProperty variable
* This function handle the logic of a Microdata intelligent display.
* Check if the Type, Property are available, if not check for a Fallback,
* then reset all params for the next use and
* return the Microdata HTML
* @param string $displayType Optional, 'inline', available ['inline'|'span'|'div'|meta]
* @param boolean $emptyOutput Return an empty string if the microdata output is disabled and there is a $content value
public function display($displayType =
'', $emptyOutput =
false)
// Initialize the HTML to output
// Control if the Microdata output is enabled, otherwise return the content or empty string
return ($emptyOutput) ?
'' :
$html;
// If the property is wrong for the current Type check if Fallback available, otherwise return empty HTML
// Process and return the HTML the way the user expects to
$html =
static::htmlSpan($html, $this->property);
$html =
static::htmlDiv($html, $this->property);
$html =
static::htmlMeta($html, $this->property);
// Default $displayType = 'inline'
$html =
static::htmlProperty($this->property);
* Process and return the HTML in an automatic way,
* with the Property expected Types and display the Microdata in the right way,
* check if the Property is normal, nested or must be rendered in a metadata tag
switch (static::getExpectedDisplayType($this->type, $this->property))
// Retrive the expected nested Type of the Property
$nestedType =
static::getExpectedTypes($this->type, $this->property);
// If there is a Fallback Type then probably it could be the expectedType
$nestedType =
$nestedType[0];
// Check if a Content is available, otherwise Fallback to an 'inline' display type
$html =
static::htmlSpan(
$html =
static::htmlSpan(
$html =
static::htmlProperty($this->property) .
' ' .
static::htmlScope($nestedType);
$html .=
' ' .
static::htmlProperty($nestedProperty);
// Check if the Content value is available, otherwise Fallback to an 'inline' display Type
$html =
static::htmlMeta($html, $this->property) .
$this->content;
$html =
static::htmlProperty($this->property);
* Default expected display type = 'normal'
* Check if the Content value is available,
* otherwise Fallback to an 'inline' display Type
$html =
static::htmlSpan($this->content, $this->property);
$html =
static::htmlProperty($this->property);
// Process and return the HTML the way the user expects to
// Default $displayType = 'inline'
$html =
static::htmlScope($type::scope()) .
' ' .
static::htmlProperty($this->fallbackProperty);
* Process and return the HTML in an automatic way,
* with the Property expected Types an display the Microdata in the right way,
* check if the Property is nested or must be rendered in a metadata tag
// Check if the Content value is available, otherwise Fallback to an 'inline' display Type
* Default expected display type = 'normal'
* Check if the Content value is available,
* otherwise Fallback to an 'inline' display Type
* Return the HTML of the current Scope
// Control if the Microdata output is enabled, otherwise return the content or empty string
return static::htmlScope($this->type);
* Return the sanitized Type
* @param string $type The Type to sanitize
* Return the sanitized Property
* @param string $property The Property to sanitize
* Return an array with all Types and Properties
* Return an array with all available Types
* Return the expected types of the Property
* @param string $type The Type to process
* @param string $property The Property to process
$tmp =
static::$types[$type]['properties'];
// Check if the Property is in the Type
if (isset
($tmp[$property]))
return $tmp[$property]['expectedTypes'];
// Check if the Property is inherit
$extendedType =
static::$types[$type]['extends'];
if (!empty($extendedType))
return static::getExpectedTypes($extendedType, $property);
* Return the expected display type of the [normal|nested|meta]
* In wich way to display the Property:
* normal -> itemprop="name"
* nested -> itemprop="director" itemscope itemtype="http://schema.org/Person"
* meta -> <meta itemprop="datePublished" content="1991-05-01">
* @param string $type The Type where to find the Property
* @param string $property The Property to process
$expectedTypes =
static::getExpectedTypes($type, $property);
// Retrieve the first expected type
$type =
$expectedTypes[0];
// Check if it's a meta display
if ($type ===
'Date' ||
$type ===
'DateTime' ||
$property ===
'interactionCount')
// Check if it's a normal display
if ($type ===
'Text' ||
$type ===
'URL' ||
$type ===
'Boolean' ||
$type ===
'Number')
// Otherwise it's a nested display
* Recursive function, control if the given Type has the given Property
* @param string $type The Type where to check
* @param string $property The Property to check
if (!static::isTypeAvailable($type))
// Control if the Property exists, and return true
// Recursive: Check if the Property is inherit
$extendedType =
static::$types[$type]['extends'];
if (!empty($extendedType))
return static::isPropertyInType($extendedType, $property);
* Control if the given Type class is available
* @param string $type The Type to check
* Return the microdata in a <meta> tag with the machine content inside.
* @param string $content The machine content to display
* @param string $property The Property
* @param string $scope Optional, the Type scope to display
* @param boolean $inverse Optional, default = false, inverse the $scope with the $property
public static function htmlMeta($content, $property, $scope =
'', $inverse =
false)
// Control if the Property has allready the itemprop
if (stripos($property, 'itemprop') !==
0)
$property =
static::htmlProperty($property);
// Control if the Scope have allready the itemtype
if (!empty($scope) &&
stripos($scope, 'itemscope') !==
0)
$scope =
static::htmlScope($scope);
$tmp =
join(' ', array($property, $scope));
$tmp =
join(' ', array($scope, $property));
return "<meta $tmp content='$content'/>";
* Return the microdata in an <span> tag.
* @param string $content The human value
* @param string $property Optional, the human value to display
* @param string $scope Optional, the Type scope to display
* @param boolean $inverse Optional, default = false, inverse the $scope with the $property
public static function htmlSpan($content, $property =
'', $scope =
'', $inverse =
false)
// Control if the Property has allready the itemprop
if (!empty($property) &&
stripos($property, 'itemprop') !==
0)
$property =
static::htmlProperty($property);
// Control if the Scope have allready the itemtype
if (!empty($scope) &&
stripos($scope, 'itemscope') !==
0)
$scope =
static::htmlScope($scope);
$tmp =
join(' ', array($property, $scope));
$tmp =
join(' ', array($scope, $property));
$tmp =
($tmp) ?
' ' .
$tmp :
'';
return "<span$tmp>$content</span>";
* Return the microdata in an <div> tag.
* @param string $content The human value
* @param string $property Optional, the human value to display
* @param string $scope Optional, the Type scope to display
* @param boolean $inverse Optional, default = false, inverse the $scope with the $property
public static function htmlDiv($content, $property =
'', $scope =
'', $inverse =
false)
// Control if the Property has allready the itemprop
if (!empty($property) &&
stripos($property, 'itemprop') !==
0)
$property =
static::htmlProperty($property);
// Control if the Scope have allready the itemtype
if (!empty($scope) &&
stripos($scope, 'itemscope') !==
0)
$scope =
static::htmlScope($scope);
$tmp =
join(' ', array($property, $scope));
$tmp =
join(' ', array($scope, $property));
$tmp =
($tmp) ?
' ' .
$tmp :
'';
return "<div$tmp>$content</div>";
* @param string $scope The Scope to process
$scope =
'https://schema.org/' .
ucfirst($scope);
return "itemscope itemtype='$scope'";
* Return the HTML Property
* @param string $property The Property to process
return "itemprop='$property'";
Documentation generated on Tue, 19 Nov 2013 15:08:12 +0100 by phpDocumentor 1.4.3