File: LanguageTaggedString.php

package info (click to toggle)
php-ml-json-ld 1.2.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 628 kB
  • sloc: php: 4,521; makefile: 21
file content (95 lines) | stat: -rw-r--r-- 2,139 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php

/*
 * (c) Markus Lanthaler <mail@markus-lanthaler.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace ML\JsonLD;

use stdClass as JsonObject;

/**
 * A LanguageTaggedString is a string which is tagged with a language.
 *
 * @author Markus Lanthaler <mail@markus-lanthaler.com>
 */
final class LanguageTaggedString extends Value
{
    /**
     * The language code associated with the string. Language codes are tags
     * according to {@link http://tools.ietf.org/html/bcp47 BCP47}.
     *
     * @var string
     */
    private $language;

    /**
     * Constructor
     *
     * @param string $value    The string's value.
     * @param string $language The string's language.
     */
    public function __construct($value, $language)
    {
        $this->setValue($value);
        $this->setLanguage($language);
    }

    /**
     * Set the language
     *
     * @param string $language The language.
     *
     * @return self
     *
     * @throws \InvalidArgumentException If the language is not a string. No
     *                                   further checks are currently done.
     */
    public function setLanguage($language)
    {
        if (!is_string($language)) {
            throw new \InvalidArgumentException('language must be a string.');
        }

        $this->language = $language;

        return $this;
    }

    /**
     * Get the language
     *
     * @return string The language.
     */
    public function getLanguage()
    {
        return $this->language;
    }

    /**
     * {@inheritdoc}
     */
    public function toJsonLd($useNativeTypes = true)
    {
        $result = new JsonObject();
        $result->{'@value'} = $this->value;
        $result->{'@language'} = $this->language;

        return $result;
    }

    /**
     * {@inheritdoc}
     */
    public function equals($other)
    {
        if (get_class($this) !== get_class($other)) {
            return false;
        }

        return ($this->value === $other->value) && ($this->language === $other->language);
    }
}