Class luya\web\JsonLd

Inheritanceluya\web\JsonLd » yii\base\BaseObject
Implementsyii\base\Configurable
Available since version1.0.0
Source Code https://github.com/luyadev/luya/blob/master/core/web/JsonLd.php

Registerin Microdata as JsonLD.

In order to register a json ld tag just call:

JsonLd::person()
   ->setGivenName('Albert')
   ->setFamilyName('Einstein')
   ->setBirthPlace('Ulm, Germany');

Or any other tags. This will register the json ld output into the layout file of the view.

See also https://schema.org/docs/schemas.html.

Public Methods

Hide inherited methods

Method Description Defined By
__call() Calls the named method which is not a class method. yii\base\BaseObject
__construct() Constructor. yii\base\BaseObject
__get() Returns the value of an object property. yii\base\BaseObject
__isset() Checks if a property is set, i.e. defined and not null. yii\base\BaseObject
__set() Sets value of an object property. yii\base\BaseObject
__unset() Sets an object property to null. yii\base\BaseObject
addGraph() Register graph data. luya\web\JsonLd
aggregateRating() Register new Aggregated Rating. luya\web\JsonLd
article() Register new Article. luya\web\JsonLd
blogPosting() Register new Blog Posting. luya\web\JsonLd
canGetProperty() Returns a value indicating whether a property can be read. yii\base\BaseObject
canSetProperty() Returns a value indicating whether a property can be set. yii\base\BaseObject
className() Returns the fully qualified name of this class. yii\base\BaseObject
comment() Register new Comment. luya\web\JsonLd
contactPoint() Register new Contact Point. luya\web\JsonLd
country() Register new Country. luya\web\JsonLd
creativeWork() Register new CreativeWork. luya\web\JsonLd
event() Register new Event. luya\web\JsonLd
hasMethod() Returns a value indicating whether a method is defined. yii\base\BaseObject
hasProperty() Returns a value indicating whether a property is defined. yii\base\BaseObject
imageObject() Register new Image Object. luya\web\JsonLd
init() Initializes the object. yii\base\BaseObject
liveBlogPosting() Register new Live Blog Posting. luya\web\JsonLd
offer() Register new Offer. luya\web\JsonLd
organization() Register new Organization. luya\web\JsonLd
person() Register new Person. luya\web\JsonLd
place() Register new Place luya\web\JsonLd
postalAddress() Register new Postal Address. luya\web\JsonLd
propertyValue() Register new Property Value. luya\web\JsonLd
rating() Register new Rating. luya\web\JsonLd
reset() Reset the JsonLd Data. luya\web\JsonLd
socialMediaPosting() Register new Social Media Posting. luya\web\JsonLd
thing() Register new Thing. luya\web\JsonLd

Protected Methods

Hide inherited methods

Method Description Defined By
registerView() Register the view file an observe the event which then reads the data from @graph params key. luya\web\JsonLd

Method Details

Hide inherited methods

__call() public method

Defined in: yii\base\BaseObject::__call()

Calls the named method which is not a class method.

Do not call this method directly as it is a PHP magic method that will be implicitly called when an unknown method is being invoked.

public mixed __call ( $name, $params )
$name string

The method name

$params array

Method parameters

return mixed

The method return value

throws yii\base\UnknownMethodException

when calling unknown method

                public function __call($name, $params)
{
    throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}

            
__construct() public method

Defined in: yii\base\BaseObject::__construct()

Constructor.

The default implementation does two things:

  • Initializes the object with the given configuration $config.
  • Call init().

If this method is overridden in a child class, it is recommended that

  • the last parameter of the constructor is a configuration array, like $config here.
  • call the parent implementation at the end of the constructor.
public void __construct ( $config = [] )
$config array

Name-value pairs that will be used to initialize the object properties

                public function __construct($config = [])
{
    if (!empty($config)) {
        Yii::configure($this, $config);
    }
    $this->init();
}

            
__get() public method

Defined in: yii\base\BaseObject::__get()

Returns the value of an object property.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing $value = $object->property;.

See also __set().

public mixed __get ( $name )
$name string

The property name

return mixed

The property value

throws yii\base\UnknownPropertyException

if the property is not defined

throws yii\base\InvalidCallException

if the property is write-only

                public function __get($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        return $this->$getter();
    } elseif (method_exists($this, 'set' . $name)) {
        throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
    }
    throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}

            
__isset() public method

Defined in: yii\base\BaseObject::__isset()

Checks if a property is set, i.e. defined and not null.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing isset($object->property).

Note that if the property is not defined, false will be returned.

See also https://www.php.net/manual/en/function.isset.php.

public boolean __isset ( $name )
$name string

The property name or the event name

return boolean

Whether the named property is set (not null).

                public function __isset($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        return $this->$getter() !== null;
    }
    return false;
}

            
__set() public method

Defined in: yii\base\BaseObject::__set()

Sets value of an object property.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing $object->property = $value;.

See also __get().

public void __set ( $name, $value )
$name string

The property name or the event name

$value mixed

The property value

throws yii\base\UnknownPropertyException

if the property is not defined

throws yii\base\InvalidCallException

if the property is read-only

                public function __set($name, $value)
{
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) {
        $this->$setter($value);
    } elseif (method_exists($this, 'get' . $name)) {
        throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
    } else {
        throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
    }
}

            
__unset() public method

Defined in: yii\base\BaseObject::__unset()

Sets an object property to null.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing unset($object->property).

Note that if the property is not defined, this method will do nothing. If the property is read-only, it will throw an exception.

See also https://www.php.net/manual/en/function.unset.php.

public void __unset ( $name )
$name string

The property name

throws yii\base\InvalidCallException

if the property is read only.

                public function __unset($name)
{
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) {
        $this->$setter(null);
    } elseif (method_exists($this, 'get' . $name)) {
        throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
    }
}

            
addGraph() public static method

Register graph data.

public static array|object addGraph ( $data )
$data luya\web\jsonld\BaseThing|array

Can be either an array or an object based on {{luya\web\jsonld\BaseThing}} which contains the Arrayable Inteface.

                public static function addGraph($data)
{
    self::registerView();
    
    if (is_scalar($data)) {
        throw new Exception("Data must be either an array or an object of type luya\web\jsonld\BaseThing.");
    }
    
    Yii::$app->view->params['@context'] = 'https://schema.org';
    Yii::$app->view->params['@graph'][] = $data;
    
    return $data;
}

            
aggregateRating() public static method (available since version 1.0.3)

Register new Aggregated Rating.

public static luya\web\jsonld\AggregateRating aggregateRating ( array $config = [] )
$config array

                public static function aggregateRating(array $config = [])
{
    return self::addGraph((new AggregateRating($config)));
}

            
article() public static method (available since version 1.0.1)

Register new Article.

An article, such as a news article or piece of investigative report. Newspapers and magazines have articles of many different types and this is intended to cover them all.

public static luya\web\jsonld\Article article ( array $config = [] )
$config array

Optional config array to provided article data via setter methods.

                public static function article(array $config = [])
{
    return self::addGraph((new Article($config)));
}

            
blogPosting() public static method (available since version 1.0.1)

Register new Blog Posting.

public static luya\web\jsonld\BlogPosting blogPosting ( array $config = [] )
$config array

Optional config array to provided blog posting data via setter methods.

                public static function blogPosting(array $config = [])
{
    return self::addGraph((new BlogPosting($config)));
}

            
canGetProperty() public method

Defined in: yii\base\BaseObject::canGetProperty()

Returns a value indicating whether a property can be read.

A property is readable if:

  • the class has a getter method associated with the specified name (in this case, property name is case-insensitive);
  • the class has a member variable with the specified name (when $checkVars is true);

See also canSetProperty().

public boolean canGetProperty ( $name, $checkVars true )
$name string

The property name

$checkVars boolean

Whether to treat member variables as properties

return boolean

Whether the property can be read

                public function canGetProperty($name, $checkVars = true)
{
    return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
}

            
canSetProperty() public method

Defined in: yii\base\BaseObject::canSetProperty()

Returns a value indicating whether a property can be set.

A property is writable if:

  • the class has a setter method associated with the specified name (in this case, property name is case-insensitive);
  • the class has a member variable with the specified name (when $checkVars is true);

See also canGetProperty().

public boolean canSetProperty ( $name, $checkVars true )
$name string

The property name

$checkVars boolean

Whether to treat member variables as properties

return boolean

Whether the property can be written

                public function canSetProperty($name, $checkVars = true)
{
    return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
}

            
className() public static method
Deprecated since 2.0.14. On PHP >=5.5, use ::class instead.

Defined in: yii\base\BaseObject::className()

Returns the fully qualified name of this class.

public static string className ( )
return string

The fully qualified name of this class.

                public static function className()
{
    return get_called_class();
}

            
comment() public static method (available since version 1.0.3)

Register new Comment.

public static luya\web\jsonld\Comment comment ( array $config = [] )
$config array

                public static function comment(array $config = [])
{
    return self::addGraph((new Comment($config)));
}

            
contactPoint() public static method (available since version 1.0.3)

Register new Contact Point.

This is mainly used for addresses or user coordinates like email, telephone etc.

public static luya\web\jsonld\ContactPoint contactPoint ( array $config = [] )
$config array

                public static function contactPoint(array $config = [])
{
    return self::addGraph((new ContactPoint($config)));
}

            
country() public static method (available since version 1.0.3)

Register new Country.

public static luya\web\jsonld\Country country ( array $config = [] )
$config array

                public static function country(array $config = [])
{
    return self::addGraph((new Country($config)));
}

            
creativeWork() public static method (available since version 1.0.1)

Register new CreativeWork.

The most generic kind of creative work, including books, movies, photographs, software programs, etc.

public static luya\web\jsonld\CreativeWork creativeWork ( array $config = [] )
$config array

Optional config array to provided creative work data via setter methods.

                public static function creativeWork(array $config = [])
{
    return self::addGraph((new CreativeWork($config)));
}

            
event() public static method

Register new Event.

An event happening at a certain time and location, such as a concert, lecture, or festival.

public static luya\web\jsonld\Event event ( array $config = [] )
$config array

                public static function event(array $config = [])
{
    return self::addGraph((new Event($config)));
}

            
hasMethod() public method

Defined in: yii\base\BaseObject::hasMethod()

Returns a value indicating whether a method is defined.

The default implementation is a call to php function method_exists(). You may override this method when you implemented the php magic method __call().

public boolean hasMethod ( $name )
$name string

The method name

return boolean

Whether the method is defined

                public function hasMethod($name)
{
    return method_exists($this, $name);
}

            
hasProperty() public method

Defined in: yii\base\BaseObject::hasProperty()

Returns a value indicating whether a property is defined.

A property is defined if:

  • the class has a getter or setter method associated with the specified name (in this case, property name is case-insensitive);
  • the class has a member variable with the specified name (when $checkVars is true);

See also:

public boolean hasProperty ( $name, $checkVars true )
$name string

The property name

$checkVars boolean

Whether to treat member variables as properties

return boolean

Whether the property is defined

                public function hasProperty($name, $checkVars = true)
{
    return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}

            
imageObject() public static method (available since version 1.0.3)

Register new Image Object.

public static luya\web\jsonld\ImageObject imageObject ( array $config = [] )
$config array

                public static function imageObject(array $config = [])
{
    return self::addGraph((new ImageObject($config)));
}

            
init() public method

Defined in: yii\base\BaseObject::init()

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the given configuration.

public void init ( )

                public function init()
{
}

            
liveBlogPosting() public static method (available since version 1.0.1)

Register new Live Blog Posting.

A blog post intended to provide a rolling textual coverage of an ongoing event through continuous updates.

public static luya\web\jsonld\LiveBlogPosting liveBlogPosting ( array $config = [] )
$config array

Optional config array to provided live blog posting data via setter methods.

                public static function liveBlogPosting(array $config = [])
{
    return self::addGraph((new LiveBlogPosting($config)));
}

            
offer() public static method (available since version 1.0.3)

Register new Offer.

This is used for selling products.

public static luya\web\jsonld\Offer offer ( array $config = [] )
$config array

                public static function offer(array $config = [])
{
    return self::addGraph((new Offer($config)));
}

            
organization() public static method

Register new Organization.

An organization such as a school, NGO, corporation, club, etc.

public static luya\web\jsonld\Organization organization ( array $config = [] )
$config array

Optional config array to provided organization data via setter methods.

                public static function organization(array $config = [])
{
    return self::addGraph((new Organization($config)));
}

            
person() public static method

Register new Person.

A person (alive, dead, undead, or fictional).

public static luya\web\jsonld\Person person ( array $config = [] )
$config array

Optional config array to provided person data via setter methods.

                public static function person(array $config = [])
{
    return self::addGraph((new Person($config)));
}

            
place() public static method

Register new Place

Entities that have a somewhat fixed, physical extension.

public static luya\web\jsonld\Place place ( array $config = [] )
$config array

Optional config array to provided place data via setter methods.

                public static function place(array $config = [])
{
    return self::addGraph((new Place($config)));
}

            
postalAddress() public static method (available since version 1.0.3)

Register new Postal Address.

public static luya\web\jsonld\PostalAddress postalAddress ( array $config = [] )
$config array

                public static function postalAddress(array $config = [])
{
    return self::addGraph((new PostalAddress()));
}

            
propertyValue() public static method (available since version 1.0.3)

Register new Property Value.

public static luya\web\jsonld\PropertyValue propertyValue ( array $config = [] )
$config array

                public static function propertyValue(array $config = [])
{
    return self::addGraph((new PropertyValue($config)));
}

            
rating() public static method (available since version 1.0.3)

Register new Rating.

public static luya\web\jsonld\Rating rating ( array $config = [] )
$config array

                public static function rating(array $config = [])
{
    return self::addGraph((new Rating($config)));
}

            
registerView() protected static method

Register the view file an observe the event which then reads the data from @graph params key.

protected static void registerView ( )

                protected static function registerView()
{
    if (self::$_view === null) {
        Yii::$app->view->on(View::EVENT_BEGIN_BODY, function ($event) {
            echo '<script type="application/ld+json">' . Json::encode($event->sender->params) . '</script>';
        });
                
        self::$_view = true;
    }
}

            
reset() public static method

Reset the JsonLd Data.

This method is mainly usefull when working with unit tests for JsonLd.

public static void reset ( )

                public static function reset()
{
    self::$_view = null;
    Yii::$app->view->params['@graph'] = [];
}

            
socialMediaPosting() public static method (available since version 1.0.1)

Register new Social Media Posting.

A post to a social media platform, including blog posts, tweets, Facebook posts, etc.

public static luya\web\jsonld\SocialMediaPosting socialMediaPosting ( array $config = [] )
$config array

Optional config array to provided social media posting data via setter methods.

                public static function socialMediaPosting(array $config = [])
{
    return self::addGraph((new SocialMediaPosting($config)));
}

            
thing() public static method

Register new Thing.

public static luya\web\jsonld\Thing thing ( array $config = [] )
$config array

Optional config array to provided person data via setter methods.

                public static function thing(array $config = [])
{
    return self::addGraph((new Thing($config)));
}