10 defined(
'JPATH_PLATFORM') or die;
27 protected static $types = null;
35 protected $type = null;
43 protected $property = null;
51 protected $content = null;
59 protected $machineContent = null;
67 protected $fallbackType = null;
75 protected $fallbackProperty = null;
83 protected $fallback =
false;
91 protected $enabled =
true;
101 public function __construct($type =
'', $flag =
true)
103 if ($this->enabled = (
boolean) $flag)
111 $this->setType($type);
122 protected static function loadTypes()
127 $path = JPATH_PLATFORM .
'/joomla/microdata/types.json';
128 static::$types = json_decode(file_get_contents($path),
true);
141 public function enable($flag =
true)
143 $this->enabled = (boolean) $flag;
155 public function isEnabled()
157 return ($this->enabled) ?
true :
false;
169 public function setType($type)
177 $this->type = static::sanitizeType($type);
180 if (!static::isTypeAvailable($this->type))
182 $this->type =
'Thing';
195 public function getType()
209 public function property($property)
217 $property = static::sanitizeProperty($property);
220 if (static::isPropertyInType($this->type, $property))
222 $this->
property = $property;
226 $this->fallback =
true;
239 public function getProperty()
241 return $this->property;
254 public function content($value, $machineValue = null)
256 $this->content = $value;
257 $this->machineContent = $machineValue;
269 public function getContent()
271 return $this->content;
284 public function fallback($type, $property)
292 $this->fallbackType = static::sanitizeType($type);
295 if (!static::isTypeAvailable($this->fallbackType))
297 $this->fallbackType =
'Thing';
301 if (static::isPropertyInType($this->fallbackType, $property))
303 $this->fallbackProperty = $property;
307 $this->fallbackProperty = null;
320 public function getFallbackType()
322 return $this->fallbackType;
332 public function getFallbackProperty()
334 return $this->fallbackProperty;
350 public function display($displayType =
'', $emptyOutput =
false)
353 $html = ($this->content !== null) ? $this->content :
'';
358 return ($emptyOutput) ?
'' : $html;
362 if ($this->property && !$this->fallback)
367 switch ($displayType)
370 $html = static::htmlSpan($html, $this->property);
374 $html = static::htmlDiv($html, $this->property);
378 $html = ($this->machineContent !== null) ? $this->machineContent : $html;
379 $html = static::htmlMeta($html, $this->property);
384 $html = static::htmlProperty($this->property);
395 switch (static::getExpectedDisplayType($this->type, $this->property))
399 $nestedType = static::getExpectedTypes($this->type, $this->property);
400 $nestedProperty =
'';
403 if (in_array($this->fallbackType, $nestedType))
405 $nestedType = $this->fallbackType;
407 if ($this->fallbackProperty)
409 $nestedProperty = $this->fallbackProperty;
414 $nestedType = $nestedType[0];
418 if ($this->content !== null)
422 $html = static::htmlSpan(
428 $html = static::htmlSpan(
437 $html = static::htmlProperty($this->property) .
' ' . static::htmlScope($nestedType);
441 $html .=
' ' . static::htmlProperty($nestedProperty);
449 if ($this->content !== null)
451 $html = ($this->machineContent !== null) ? $this->machineContent : $this->content;
452 $html = static::htmlMeta($html, $this->property) . $this->content;
456 $html = static::htmlProperty($this->property);
467 if ($this->content !== null)
469 $html = static::htmlSpan($this->content, $this->property);
473 $html = static::htmlProperty($this->property);
480 elseif ($this->fallbackProperty)
485 switch ($displayType)
488 $html = static::htmlSpan($html, $this->fallbackProperty, $this->fallbackType);
492 $html = static::htmlDiv($html, $this->fallbackProperty, $this->fallbackType);
496 $html = ($this->machineContent !== null) ? $this->machineContent : $html;
497 $html = static::htmlMeta($html, $this->fallbackProperty, $this->fallbackType);
502 $html = static::htmlScope($type::scope()) .
' ' . static::htmlProperty($this->fallbackProperty);
513 switch (static::getExpectedDisplayType($this->fallbackType, $this->fallbackProperty))
517 if ($this->content !== null)
519 $html = ($this->machineContent !== null) ? $this->machineContent : $this->content;
520 $html = static::htmlMeta($html, $this->fallbackProperty, $this->fallbackType);
524 $html = static::htmlScope($this->fallbackType) .
' ' . static::htmlProperty($this->fallbackProperty);
535 if ($this->content !== null)
537 $html = static::htmlSpan($this->content, $this->fallbackProperty);
538 $html = static::htmlSpan($html,
'', $this->fallbackType);
542 $html = static::htmlScope($this->fallbackType) .
' ' . static::htmlProperty($this->fallbackProperty);
549 elseif (!$this->fallbackProperty && $this->fallbackType !== null)
551 $html = static::htmlScope($this->fallbackType);
555 $this->content = null;
556 $this->
property = null;
557 $this->fallbackProperty = null;
558 $this->fallbackType = null;
559 $this->fallback =
false;
571 public function displayScope()
579 return static::htmlScope($this->type);
591 public static function sanitizeType($type)
593 return ucfirst(trim($type));
605 public static function sanitizeProperty($property)
607 return lcfirst(trim($property));
617 public static function getTypes()
621 return static::$types;
631 public static function getAvailableTypes()
635 return array_keys(static::$types);
648 public static function getExpectedTypes($type, $property)
652 $tmp = static::$types[$type][
'properties'];
655 if (isset($tmp[$property]))
657 return $tmp[$property][
'expectedTypes'];
661 $extendedType = static::$types[$type][
'extends'];
664 if (!empty($extendedType))
666 return static::getExpectedTypes($extendedType, $property);
686 protected static function getExpectedDisplayType($type, $property)
688 $expectedTypes = static::getExpectedTypes($type, $property);
691 $type = $expectedTypes[0];
694 if ($type ===
'Date' || $type ===
'DateTime' || $property ===
'interactionCount')
700 if ($type ===
'Text' || $type ===
'URL' || $type ===
'Boolean' || $type ===
'Number')
719 public static function isPropertyInType($type, $property)
721 if (!static::isTypeAvailable($type))
727 if (array_key_exists($property, static::$types[$type][
'properties']))
733 $extendedType = static::$types[$type][
'extends'];
735 if (!empty($extendedType))
737 return static::isPropertyInType($extendedType, $property);
752 public static function isTypeAvailable($type)
756 return (array_key_exists($type, static::$types)) ?
true :
false;
771 public static function htmlMeta($content, $property, $scope =
'', $inverse =
false)
774 if (stripos($property,
'itemprop') !== 0)
776 $property = static::htmlProperty($property);
780 if (!empty($scope) && stripos($scope,
'itemscope') !== 0)
782 $scope = static::htmlScope($scope);
787 $tmp = join(
' ', array($property, $scope));
791 $tmp = join(
' ', array($scope, $property));
796 return "<meta $tmp content='$content'/>";
811 public static function htmlSpan($content, $property =
'', $scope =
'', $inverse =
false)
814 if (!empty($property) && stripos($property,
'itemprop') !== 0)
816 $property = static::htmlProperty($property);
820 if (!empty($scope) && stripos($scope,
'itemscope') !== 0)
822 $scope = static::htmlScope($scope);
827 $tmp = join(
' ', array($property, $scope));
831 $tmp = join(
' ', array($scope, $property));
835 $tmp = ($tmp) ?
' ' . $tmp :
'';
837 return "<span$tmp>$content</span>";
852 public static function htmlDiv($content, $property =
'', $scope =
'', $inverse =
false)
855 if (!empty($property) && stripos($property,
'itemprop') !== 0)
857 $property = static::htmlProperty($property);
861 if (!empty($scope) && stripos($scope,
'itemscope') !== 0)
863 $scope = static::htmlScope($scope);
868 $tmp = join(
' ', array($property, $scope));
872 $tmp = join(
' ', array($scope, $property));
876 $tmp = ($tmp) ?
' ' . $tmp :
'';
878 return "<div$tmp>$content</div>";
890 public static function htmlScope($scope)
892 if (stripos($scope,
'http') !== 0)
894 $scope =
'https://schema.org/' . ucfirst($scope);
897 return "itemscope itemtype='$scope'";
909 public static function htmlProperty($property)
911 return "itemprop='$property'";