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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973
|
<?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;
use ML\JsonLD\Exception\JsonLdException;
use ML\JsonLD\Exception\InvalidQuadException;
use ML\IRI\IRI;
/**
* Processor processes JSON-LD documents as specified by the JSON-LD
* specification.
*
* @author Markus Lanthaler <mail@markus-lanthaler.com>
*/
class Processor
{
/** Timeout for retrieving remote documents in seconds */
const REMOTE_TIMEOUT = 10;
/** Maximum number of recursion that are allowed to resolve an IRI */
const CONTEXT_MAX_IRI_RECURSIONS = 10;
/**
* @var array A list of all defined keywords
*/
private static $keywords = array('@context', '@id', '@value', '@language', '@type',
'@container', '@list', '@set', '@graph', '@reverse',
'@base', '@vocab', '@index', '@null');
// TODO Introduce @null supported just for framing
/**
* @var array Framing options keywords
*/
private static $framingKeywords = array('@explicit', '@default', '@embed',
//'@omitDefault', // TODO Is this really needed?
'@embedChildren'); // TODO How should this be called?
// TODO Add @preserve, @null?? Update spec keyword list
/**
* @var IRI The base IRI
*/
private $baseIri = null;
/**
* Compact arrays with just one element to a scalar
*
* If set to true, arrays holding just one element are compacted to
* scalars, otherwise the arrays are kept as arrays.
*
* @var bool
*/
private $compactArrays;
/**
* Optimize compacted output
*
* If set to true, the processor is free to optimize the result to produce
* an even compacter representation than the algorithm described by the
* official JSON-LD specification.
*
* @var bool
*/
private $optimize;
/**
* Use native types when converting from RDF
*
* If set to true, the processor will try to convert datatyped literals
* to native types instead of using the expanded object form when
* converting from RDF. xsd:boolean values will be converted to booleans
* whereas xsd:integer and xsd:double values will be converted to numbers.
*
* @var bool
*/
private $useNativeTypes;
/**
* Use rdf:type instead of \@type when converting from RDF
*
* If set to true, the JSON-LD processor will use the expanded rdf:type
* IRI as the property instead of \@type when converting from RDF.
*
* @var bool
*/
private $useRdfType;
/**
* Produce generalized RDF
*
* Unless set to true, triples/quads with a blank node predicate are
* dropped when converting to RDF.
*
* @var bool
*/
private $generalizedRdf;
/**
* @var array Blank node map
*/
private $blankNodeMap = array();
/**
* @var integer Blank node counter
*/
private $blankNodeCounter = 0;
/**
* @var DocumentFactoryInterface The factory to create new documents
*/
private $documentFactory = null;
/**
* @var DocumentLoaderInterface The document loader
*/
private $documentLoader = null;
/**
* Constructor
*
* The options parameter must be passed and all off the following properties
* have to be set:
*
* <dl>
* <dl>base</dl>
* <dt>The base IRI.</dt>
*
* <dl>compactArrays</dl>
* <dt>If set to true, arrays holding just one element are compacted
* to scalars, otherwise the arrays are kept as arrays.</dt>
*
* <dl>optimize</dl>
* <dt>If set to true, the processor is free to optimize the result to
* produce an even compacter representation than the algorithm
* described by the official JSON-LD specification.</dt>
*
* <dl>useNativeTypes</dl>
* <dt>If set to true, the processor will try to convert datatyped
* literals to native types instead of using the expanded object form
* when converting from RDF. <em>xsd:boolean</em> values will be
* converted to booleans whereas <em>xsd:integer</em> and
* <em>xsd:double</em> values will be converted to numbers.</dt>
*
* <dl>useRdfType</dl>
* <dt>If set to true, the JSON-LD processor will use the expanded
* <em>rdf:type</em> IRI as the property instead of <em>@type</em>
* when converting from RDF.</dt>
* </dl>
*
* @param JsonObject $options Options to configure the various algorithms.
*/
public function __construct($options)
{
$this->baseIri = new IRI($options->base);
$this->compactArrays = (bool) $options->compactArrays;
$this->optimize = (bool) $options->optimize;
$this->useNativeTypes = (bool) $options->useNativeTypes;
$this->useRdfType = (bool) $options->useRdfType;
$this->generalizedRdf = (bool) $options->produceGeneralizedRdf;
$this->documentFactory = $options->documentFactory;
$this->documentLoader = $options->documentLoader;
}
/**
* Parses a JSON-LD document to a PHP value
*
* @param string $document A JSON-LD document.
*
* @return mixed A PHP value.
*
* @throws JsonLdException If the JSON-LD document is not valid.
*/
public static function parse($document)
{
if (function_exists('mb_detect_encoding') &&
(false === mb_detect_encoding($document, 'UTF-8', true))) {
throw new JsonLdException(
JsonLdException::LOADING_DOCUMENT_FAILED,
'The JSON-LD document does not appear to be valid UTF-8.'
);
}
$data = json_decode($document, false, 512);
switch (json_last_error()) {
case JSON_ERROR_NONE:
break; // no error
case JSON_ERROR_DEPTH:
throw new JsonLdException(
JsonLdException::LOADING_DOCUMENT_FAILED,
'The maximum stack depth has been exceeded.'
);
case JSON_ERROR_STATE_MISMATCH:
throw new JsonLdException(
JsonLdException::LOADING_DOCUMENT_FAILED,
'Invalid or malformed JSON.'
);
case JSON_ERROR_CTRL_CHAR:
throw new JsonLdException(
JsonLdException::LOADING_DOCUMENT_FAILED,
'Control character error (possibly incorrectly encoded).'
);
case JSON_ERROR_SYNTAX:
throw new JsonLdException(
JsonLdException::LOADING_DOCUMENT_FAILED,
'Syntax error, malformed JSON.'
);
case JSON_ERROR_UTF8:
throw new JsonLdException(
JsonLdException::LOADING_DOCUMENT_FAILED,
'Malformed UTF-8 characters (possibly incorrectly encoded).'
);
default:
throw new JsonLdException(
JsonLdException::LOADING_DOCUMENT_FAILED,
'Unknown error while parsing JSON.'
);
}
return (empty($data)) ? null : $data;
}
/**
* Parses a JSON-LD document and returns it as a Document
*
* @param array|JsonObject $input The JSON-LD document to process.
*
* @return Document The parsed JSON-LD document.
*
* @throws JsonLdException If the JSON-LD input document is invalid.
*/
public function getDocument($input)
{
$nodeMap = new JsonObject();
$nodeMap->{'-' . JsonLD::DEFAULT_GRAPH} = new JsonObject();
$this->generateNodeMap($nodeMap, $input);
// We need to keep track of blank nodes as they are renamed when
// inserted into the Document
$nodes = array();
if (null === $this->documentFactory) {
$this->documentFactory = new DefaultDocumentFactory();
}
$document = $this->documentFactory->createDocument($this->baseIri);
foreach ($nodeMap as $graphName => &$nodes) {
$graphName = substr($graphName, 1);
if (JsonLD::DEFAULT_GRAPH === $graphName) {
$graph = $document->getGraph();
} else {
$graph = $document->createGraph($graphName);
}
foreach ($nodes as $id => &$item) {
$node = $graph->createNode($item->{'@id'}, true);
unset($item->{'@id'});
// Process node type as it needs to be handled differently than
// other properties
// TODO Could this be avoided by enforcing rdf:type instead of @type?
if (property_exists($item, '@type')) {
foreach ($item->{'@type'} as $type) {
$node->addType($graph->createNode($type), true);
}
unset($item->{'@type'});
}
foreach ($item as $property => $values) {
foreach ($values as $value) {
if (property_exists($value, '@value')) {
$node->addPropertyValue($property, Value::fromJsonLd($value));
} elseif (property_exists($value, '@id')) {
$node->addPropertyValue(
$property,
$graph->createNode($value->{'@id'}, true)
);
} else {
// TODO Handle lists
throw new \Exception('Lists are not supported by getDocument() yet');
}
}
}
}
}
unset($nodeMap);
return $document;
}
/**
* Expands a JSON-LD document
*
* @param mixed $element A JSON-LD element to be expanded.
* @param array $activectx The active context.
* @param null|string $activeprty The active property.
* @param boolean $frame True if a frame is being expanded, otherwise false.
*
* @return mixed The expanded document.
*
* @throws JsonLdException
*/
public function expand(&$element, $activectx = array(), $activeprty = null, $frame = false)
{
if (is_scalar($element)) {
if ((null === $activeprty) || ('@graph' === $activeprty)) {
$element = null;
} else {
$element = $this->expandValue($element, $activectx, $activeprty);
}
return;
}
if (null === $element) {
return;
}
if (is_array($element)) {
$result = array();
foreach ($element as &$item) {
$this->expand($item, $activectx, $activeprty, $frame);
// Check for lists of lists
if (('@list' === $this->getPropertyDefinition($activectx, $activeprty, '@container')) ||
('@list' === $activeprty)) {
if (is_array($item) || (is_object($item) && property_exists($item, '@list'))) {
throw new JsonLdException(
JsonLdException::LIST_OF_LISTS,
"List of lists detected in property \"$activeprty\".",
$element
);
}
}
if (is_array($item)) {
$result = array_merge($result, $item);
} elseif (null !== $item) {
$result[] = $item;
}
}
$element = $result;
return;
}
// Otherwise it's an object. Process its local context if available
if (property_exists($element, '@context')) {
$this->processContext($element->{'@context'}, $activectx);
unset($element->{'@context'});
}
$properties = get_object_vars($element);
ksort($properties);
$element = new JsonObject();
foreach ($properties as $property => $value) {
$expProperty = $this->expandIri($property, $activectx, false, true);
// Make sure to keep framing keywords if a frame is being expanded
if ($frame && in_array($expProperty, self::$framingKeywords)) {
// and that the default value is expanded
if ('@default' === $expProperty) {
$this->expand($value, $activectx, $activeprty, $frame);
}
self::setProperty($element, $expProperty, $value, JsonLdException::COLLIDING_KEYWORDS);
continue;
}
if (in_array($expProperty, self::$keywords)) {
if ('@reverse' === $activeprty) {
throw new JsonLdException(
JsonLdException::INVALID_REVERSE_PROPERTY_MAP,
'No keywords or keyword aliases are allowed in @reverse-maps, found ' . $expProperty
);
}
$this->expandKeywordValue($element, $activeprty, $expProperty, $value, $activectx, $frame);
continue;
} elseif (false === strpos($expProperty, ':')) {
// the expanded property is neither a keyword nor an IRI
continue;
}
$propertyContainer = $this->getPropertyDefinition($activectx, $property, '@container');
if (is_object($value) && in_array($propertyContainer, array('@language', '@index'))) {
$result = array();
$value = (array) $value; // makes it easier to order the key-value pairs
ksort($value);
if ('@language' === $propertyContainer) {
foreach ($value as $key => $val) {
// TODO Make sure key is a valid language tag
if (false === is_array($val)) {
$val = array($val);
}
foreach ($val as $item) {
if (false === is_string($item)) {
throw new JsonLdException(
JsonLdException::INVALID_LANGUAGE_MAP_VALUE,
"Detected invalid value in $property->$key: it must be a string as it " .
"is part of a language map.",
$item
);
}
$result[] = (object) array(
'@value' => $item,
'@language' => strtolower($key)
);
}
}
} else {
// @container: @index
foreach ($value as $key => $val) {
if (false === is_array($val)) {
$val = array($val);
}
$this->expand($val, $activectx, $property, $frame);
foreach ($val as $item) {
if (false === property_exists($item, '@index')) {
$item->{'@index'} = $key;
}
$result[] = $item;
}
}
}
$value = $result;
} else {
$this->expand($value, $activectx, $property, $frame);
}
// Remove properties with null values
if (null === $value) {
continue;
}
// If property has an @list container and value is not yet an
// expanded @list-object, transform it to one
if (('@list' === $propertyContainer) &&
((false === is_object($value) || (false === property_exists($value, '@list'))))) {
if (false === is_array($value)) {
$value = array($value);
}
$obj = new JsonObject();
$obj->{'@list'} = $value;
$value = $obj;
}
$target = $element;
if ($this->getPropertyDefinition($activectx, $property, '@reverse')) {
if (false === property_exists($target, '@reverse')) {
$target->{'@reverse'} = new JsonObject();
}
$target = $target->{'@reverse'};
if (false === is_array($value)) {
$value = array($value);
}
foreach ($value as $val) {
if (property_exists($val, '@value') || property_exists($val, '@list')) {
throw new JsonLdException(
JsonLdException::INVALID_REVERSE_PROPERTY_VALUE,
'Detected invalid value in @reverse-map (only nodes are allowed',
$val
);
}
}
}
self::mergeIntoProperty($target, $expProperty, $value, true);
}
// All properties have been processed. Make sure the result is valid
// and optimize it where possible
$numProps = count(get_object_vars($element));
// Remove free-floating nodes
if ((false === $frame) && ((null === $activeprty) || ('@graph' === $activeprty)) &&
(((0 === $numProps) || property_exists($element, '@value') || property_exists($element, '@list') ||
((1 === $numProps) && property_exists($element, '@id'))))) {
$element = null;
return;
}
// Indexes are allowed everywhere
if (property_exists($element, '@index')) {
$numProps--;
}
if (property_exists($element, '@value')) {
$numProps--; // @value
if (property_exists($element, '@language')) {
if (false === $frame) {
if (false === is_string($element->{'@language'})) {
throw new JsonLdException(
JsonLdException::INVALID_LANGUAGE_TAGGED_STRING,
'Invalid value for @language detected (must be a string).',
$element
);
}
if (false === is_string($element->{'@value'})) {
throw new JsonLdException(
JsonLdException::INVALID_LANGUAGE_TAGGED_VALUE,
'Only strings can be language tagged.',
$element
);
}
}
$numProps--;
} elseif (property_exists($element, '@type')) {
if ((false === $frame) && ((false === is_string($element->{'@type'})) ||
(false === strpos($element->{'@type'}, ':')) ||
('_:' === substr($element->{'@type'}, 0, 2)))) {
throw new JsonLdException(
JsonLdException::INVALID_TYPED_VALUE,
'Invalid value for @type detected (must be an IRI).',
$element
);
}
$numProps--;
}
if ($numProps > 0) {
throw new JsonLdException(
JsonLdException::INVALID_VALUE_OBJECT,
'Detected an invalid @value object.',
$element
);
} elseif (null === $element->{'@value'}) {
// object has just an @value property that is null, can be replaced with that value
$element = $element->{'@value'};
}
return;
}
// Not an @value object, make sure @type is an array
if (property_exists($element, '@type') && (false === is_array($element->{'@type'}))) {
$element->{'@type'} = array($element->{'@type'});
}
if (($numProps > 1) && ((property_exists($element, '@list') || property_exists($element, '@set')))) {
throw new JsonLdException(
JsonLdException::INVALID_SET_OR_LIST_OBJECT,
'An object with a @list or @set property can\'t contain other properties.',
$element
);
} elseif (property_exists($element, '@set')) {
// @set objects can be optimized away as they are just syntactic sugar
$element = $element->{'@set'};
} elseif (($numProps === 1) && (false === $frame) && property_exists($element, '@language')) {
// if there's just @language and nothing else and we are not expanding a frame, drop whole object
$element = null;
}
}
/**
* Expands the value of a keyword
*
* @param JsonObject $element The object this property-value pair is part of.
* @param string $activeprty The active property.
* @param string $keyword The keyword whose value is being expanded.
* @param mixed $value The value to expand.
* @param array $activectx The active context.
* @param boolean $frame True if a frame is being expanded, otherwise false.
*
* @throws JsonLdException
*/
private function expandKeywordValue(&$element, $activeprty, $keyword, $value, $activectx, $frame)
{
// Ignore all null values except for @value as in that case it is
// needed to determine what @type means
if ((null === $value) && ('@value' !== $keyword)) {
return;
}
if ('@id' === $keyword) {
if (false === is_string($value)) {
throw new JsonLdException(
JsonLdException::INVALID_ID_VALUE,
'Invalid value for @id detected (must be a string).',
$element
);
}
$value = $this->expandIri($value, $activectx, true);
self::setProperty($element, $keyword, $value, JsonLdException::COLLIDING_KEYWORDS);
return;
}
if ('@type' === $keyword) {
if (is_string($value)) {
$value = $this->expandIri($value, $activectx, true, true);
self::setProperty($element, $keyword, $value, JsonLdException::COLLIDING_KEYWORDS);
return;
}
if (false === is_array($value)) {
$value = array($value);
}
$result = array();
foreach ($value as $item) {
if (is_string($item)) {
$result[] = $this->expandIri($item, $activectx, true, true);
} else {
if (false === $frame) {
throw new JsonLdException(
JsonLdException::INVALID_TYPE_VALUE,
"Invalid value for $keyword detected.",
$value
);
}
self::mergeIntoProperty($element, $keyword, $item);
}
}
// Don't keep empty arrays
if (count($result) >= 1) {
self::mergeIntoProperty($element, $keyword, $result, true);
}
}
if (('@value' === $keyword)) {
if (false === $frame) {
if ((null !== $value) && (false === is_scalar($value))) {
// we need to preserve @value: null to distinguish values form nodes
throw new JsonLdException(
JsonLdException::INVALID_VALUE_OBJECT_VALUE,
"Invalid value for @value detected (must be a scalar).",
$value
);
}
} elseif (false === is_array($value)) {
$value = array($value);
}
self::setProperty($element, $keyword, $value, JsonLdException::COLLIDING_KEYWORDS);
return;
}
if (('@language' === $keyword) || ('@index' === $keyword)) {
if (false === $frame) {
if (false === is_string($value)) {
throw ('@language' === $keyword)
? new JsonLdException(
JsonLdException::INVALID_LANGUAGE_TAGGED_STRING,
'@language must be a string',
$value
)
: new JsonLdException(
JsonLdException::INVALID_INDEX_VALUE,
'@index must be a string',
$value
);
}
} elseif (false === is_array($value)) {
$value = array($value);
}
self::setProperty($element, $keyword, $value, JsonLdException::COLLIDING_KEYWORDS);
return;
}
// TODO Optimize the following code, there's a lot of repetition, only the $activeprty param is changing
if ('@list' === $keyword) {
if ((null === $activeprty) || ('@graph' === $activeprty)) {
return;
}
$this->expand($value, $activectx, $activeprty, $frame);
if (false === is_array($value)) {
$value = array($value);
}
foreach ($value as $val) {
if (is_object($val) && property_exists($val, '@list')) {
throw new JsonLdException(JsonLdException::LIST_OF_LISTS, 'List of lists detected.', $element);
}
}
self::mergeIntoProperty($element, $keyword, $value, true);
return;
}
if ('@set' === $keyword) {
$this->expand($value, $activectx, $activeprty, $frame);
self::mergeIntoProperty($element, $keyword, $value, true);
return;
}
if ('@reverse' === $keyword) {
if (false === is_object($value)) {
throw new JsonLdException(
JsonLdException::INVALID_REVERSE_VALUE,
'Detected invalid value for @reverse (must be an object).',
$value
);
}
$this->expand($value, $activectx, $keyword, $frame);
// Do not create @reverse-containers inside @reverse containers
if (property_exists($value, $keyword)) {
foreach (get_object_vars($value->{$keyword}) as $prop => $val) {
self::mergeIntoProperty($element, $prop, $val, true);
}
unset($value->{$keyword});
}
$value = get_object_vars($value);
if ((count($value) > 0) && (false === property_exists($element, $keyword))) {
$element->{$keyword} = new JsonObject();
}
foreach ($value as $prop => $val) {
foreach ($val as $v) {
if (property_exists($v, '@value') || property_exists($v, '@list')) {
throw new JsonLdException(
JsonLdException::INVALID_REVERSE_PROPERTY_VALUE,
'Detected invalid value in @reverse-map (only nodes are allowed',
$v
);
}
self::mergeIntoProperty($element->{$keyword}, $prop, $v, true);
}
}
return;
}
if ('@graph' === $keyword) {
$this->expand($value, $activectx, $keyword, $frame);
self::mergeIntoProperty($element, $keyword, $value, true);
return;
}
}
/**
* Expands a scalar value
*
* @param mixed $value The value to expand.
* @param array $activectx The active context.
* @param string $activeprty The active property.
*
* @return JsonObject The expanded value.
*/
private function expandValue($value, $activectx, $activeprty)
{
$def = $this->getPropertyDefinition($activectx, $activeprty);
$result = new JsonObject();
if ('@id' === $def['@type']) {
$result->{'@id'} = $this->expandIri($value, $activectx, true);
} elseif ('@vocab' === $def['@type']) {
$result->{'@id'} = $this->expandIri($value, $activectx, true, true);
} else {
$result->{'@value'} = $value;
if (isset($def['@type'])) {
$result->{'@type'} = $def['@type'];
} elseif (isset($def['@language']) && is_string($result->{'@value'})) {
$result->{'@language'} = $def['@language'];
}
}
return $result;
}
/**
* Expands a JSON-LD IRI value (term, compact IRI, IRI) to an absolute
* IRI and relabels blank nodes
*
* @param mixed $value The value to be expanded to an absolute IRI.
* @param array $activectx The active context.
* @param bool $relativeIri Specifies whether $value should be treated as
* relative IRI against the base IRI or not.
* @param bool $vocabRelative Specifies whether $value is relative to @vocab
* if set or not.
* @param null|JsonObject $localctx If the IRI is being expanded as part of context
* processing, the current local context has to be
* passed as well.
* @param array $path A path of already processed terms to detect
* circular dependencies
*
* @return string The expanded IRI.
*/
private function expandIri(
$value,
$activectx,
$relativeIri = false,
$vocabRelative = false,
$localctx = null,
$path = array()
) {
if ((null === $value) || in_array($value, self::$keywords)) {
return $value;
}
if ($localctx) {
if (in_array($value, $path)) {
throw new JsonLdException(
JsonLdException::CYCLIC_IRI_MAPPING,
'Cycle in context definition detected: ' . join(' -> ', $path) . ' -> ' . $path[0],
$localctx
);
} else {
$path[] = $value;
if (count($path) >= self::CONTEXT_MAX_IRI_RECURSIONS) {
throw new JsonLdException(
JsonLdException::UNSPECIFIED,
'Too many recursions in term definition: ' . join(' -> ', $path) . ' -> ' . $path[0],
$localctx
);
}
}
if (isset($localctx->{$value})) {
$nested = null;
if (is_string($localctx->{$value})) {
$nested = $localctx->{$value};
} elseif (isset($localctx->{$value}->{'@id'})) {
$nested = $localctx->{$value}->{'@id'};
}
if ($nested && (end($path) !== $nested)) {
return $this->expandIri($nested, $activectx, false, true, $localctx, $path);
}
}
}
// Terms apply only for vocab-relative IRIs
if ((true === $vocabRelative) && array_key_exists($value, $activectx)) {
return $activectx[$value]['@id'];
}
if (false !== strpos($value, ':')) {
list($prefix, $suffix) = explode(':', $value, 2);
if (('_' === $prefix) || ('//' === substr($suffix, 0, 2))) {
// Safety measure to prevent reassigned of, e.g., http://
// the "_" prefix is reserved for blank nodes and can't be expanded
return $value;
}
if ($localctx) {
$prefix = $this->expandIri($prefix, $activectx, false, true, $localctx, $path);
// If prefix contains a colon, we have successfully expanded it
if (false !== strpos($prefix, ':')) {
return $prefix . $suffix;
}
} elseif (array_key_exists($prefix, $activectx)) {
// compact IRI
return $activectx[$prefix]['@id'] . $suffix;
}
} else {
if ($vocabRelative && array_key_exists('@vocab', $activectx)) {
return $activectx['@vocab'] . $value;
} elseif (($relativeIri) && (null !== $activectx['@base'])) {
return (string) $activectx['@base']->resolve($value);
}
}
// can't expand it, return as is
return $value;
}
/**
* Compacts a JSON-LD document
*
* Attention: This method must be called with an expanded element,
* otherwise it might not work.
*
* @param mixed $element A JSON-LD element to be compacted.
* @param array $activectx The active context.
* @param array $inversectx The inverse context.
* @param null|string $activeprty The active property.
*
* @return mixed The compacted JSON-LD document.
*/
public function compact(&$element, $activectx = array(), $inversectx = array(), $activeprty = null)
{
if (is_array($element)) {
$result = array();
foreach ($element as &$item) {
$this->compact($item, $activectx, $inversectx, $activeprty);
if (null !== $item) {
$result[] = $item;
}
}
if ($this->compactArrays && (1 === count($result))) {
$element = $result[0];
} else {
$element = $result;
}
return;
}
if (false === is_object($element)) {
// element is already in compact form, nothing else to do
return;
}
if (property_exists($element, '@value') || property_exists($element, '@id')) {
$def = $this->getPropertyDefinition($activectx, $activeprty);
$element = $this->compactValue($element, $def, $activectx, $inversectx);
if (false === is_object($element)) {
return;
}
}
// Otherwise, compact all properties
$properties = get_object_vars($element);
ksort($properties);
$inReverse = ('@reverse' === $activeprty);
$element = new JsonObject();
foreach ($properties as $property => $value) {
if (in_array($property, self::$keywords)) {
if ('@id' === $property) {
$value = $this->compactIri($value, $activectx, $inversectx);
} elseif ('@type' === $property) {
if (is_string($value)) {
$value = $this->compactIri($value, $activectx, $inversectx, null, true);
} else {
foreach ($value as &$iri) {
$iri = $this->compactIri($iri, $activectx, $inversectx, null, true);
}
if ($this->compactArrays && (1 === count($value))) {
$value = $value[0];
}
}
} elseif (('@graph' === $property) || ('@list' === $property)) {
$this->compact($value, $activectx, $inversectx, $property);
if (false === is_array($value)) {
$value = array($value);
}
} elseif ('@reverse' === $property) {
$this->compact($value, $activectx, $inversectx, $property);
// Move reverse properties out of the map into element
foreach (get_object_vars($value) as $prop => $val) {
if ($this->getPropertyDefinition($activectx, $prop, '@reverse')) {
$alwaysArray = ('@set' === $this->getPropertyDefinition($activectx, $prop, '@container'));
self::mergeIntoProperty($element, $prop, $val, $alwaysArray);
unset($value->{$prop});
}
}
if (0 === count(get_object_vars($value))) {
continue; // no properties left in the @reverse-map
}
}
// Get the keyword alias from the inverse context if available
$activeprty = (isset($inversectx[$property]['term']))
? $inversectx[$property]['term']
: $property;
self::setProperty($element, $activeprty, $value, JsonLdException::COLLIDING_KEYWORDS);
// ... continue with next property
continue;
}
// handle @null-objects as used in framing
if (is_object($value) && property_exists($value, '@null')) {
$activeprty = $this->compactIri($property, $activectx, $inversectx, null, true, $inReverse);
if (false === property_exists($element, $activeprty)) {
$element->{$activeprty} = null;
}
continue;
}
// Make sure that empty arrays are preserved
if (0 === count($value)) {
$activeprty = $this->compactIri($property, $activectx, $inversectx, null, true, $inReverse);
self::mergeIntoProperty($element, $activeprty, $value);
// ... continue with next property
continue;
}
// Compact every item in value separately as they could map to different terms
foreach ($value as $item) {
$activeprty = $this->compactIri($property, $activectx, $inversectx, $item, true, $inReverse);
$def = $this->getPropertyDefinition($activectx, $activeprty);
if (in_array($def['@container'], array('@language', '@index'))) {
if (false === property_exists($element, $activeprty)) {
$element->{$activeprty} = new JsonObject();
}
$def[$def['@container']] = $item->{$def['@container']};
$item = $this->compactValue($item, $def, $activectx, $inversectx);
$this->compact($item, $activectx, $inversectx, $activeprty);
self::mergeIntoProperty($element->{$activeprty}, $def[$def['@container']], $item);
continue;
}
if (is_object($item)) {
if (property_exists($item, '@list')) {
$this->compact($item->{'@list'}, $activectx, $inversectx, $activeprty);
if (false === is_array($item->{'@list'})) {
$item->{'@list'} = array($item->{'@list'});
}
if ('@list' === $def['@container']) {
// a term can just hold one list if it has a @list container
// (we don't support lists of lists)
self::setProperty(
$element,
$activeprty,
$item->{'@list'},
JsonLdException::COMPACTION_TO_LIST_OF_LISTS
);
continue; // ... continue with next value
} else {
$result = new JsonObject();
$alias = $this->compactIri('@list', $activectx, $inversectx, null, true);
$result->{$alias} = $item->{'@list'};
if (isset($item->{'@index'})) {
$alias = $this->compactIri('@index', $activectx, $inversectx, null, true);
$result->{$alias} = $item->{'@index'};
}
$item = $result;
}
} else {
$this->compact($item, $activectx, $inversectx, $activeprty);
}
}
// Merge value back into resulting object making sure that value is always
// an array if a container is set or compactArrays is set to false
$asArray = ((false === $this->compactArrays) || (false === $def['compactArrays']));
self::mergeIntoProperty($element, $activeprty, $item, $asArray);
}
}
}
/**
* Compacts a value
*
* The passed property definition must be an associative array
* containing the following data:
*
* <code>
* @type => type IRI or null
* @language => language code or null
* @index => index string or null
* @container => the container: @set, @list, @language, or @index
* </code>
*
* @param mixed $value The value to compact (arrays are not allowed!).
* @param array $definition The active property's definition.
* @param array $activectx The active context.
* @param array $inversectx The inverse context.
*
* @return mixed The compacted value.
*/
private function compactValue($value, $definition, $activectx, $inversectx)
{
if ('@index' === $definition['@container']) {
unset($value->{'@index'});
}
$numProperties = count(get_object_vars($value));
// @id object
if (property_exists($value, '@id')) {
if (1 === $numProperties) {
if ('@id' === $definition['@type']) {
return $this->compactIri($value->{'@id'}, $activectx, $inversectx);
}
if ('@vocab' === $definition['@type']) {
return $this->compactIri($value->{'@id'}, $activectx, $inversectx, null, true);
}
}
return $value;
}
// @value object
$criterion = (isset($value->{'@type'})) ? '@type' : null;
$criterion = (isset($value->{'@language'})) ? '@language' : $criterion;
if (null !== $criterion) {
if ((2 === $numProperties) && ($value->{$criterion} === $definition[$criterion])) {
return $value->{'@value'};
}
return $value;
}
// the object has neither a @type nor a @language property
// check the active property's definition
if (is_string($value->{'@value'}) && (null !== $definition['@language'])) {
// if the property is language tagged or there's a default language,
// we can't compact the value if it is a string
return $value;
}
// we can compact the value
return (1 === $numProperties) ? $value->{'@value'} : $value;
}
/**
* Compacts an absolute IRI (or aliases a keyword)
*
* If the IRI couldn't be compacted, the IRI is returned as is.
*
* @param mixed $iri The IRI to be compacted.
* @param array $activectx The active context.
* @param array $inversectx The inverse context.
* @param mixed $value The value of the property to compact.
* @param bool $vocabRelative If `true` is passed, this method tries
* to convert the IRI to an IRI relative to
* `@vocab`; otherwise, that fall back
* mechanism is disabled.
* @param bool $reverse Is the IRI used within a @reverse container?
*
* @return string Returns the compacted IRI on success; otherwise the
* IRI is returned as is.
*/
private function compactIri($iri, $activectx, $inversectx, $value = null, $vocabRelative = false, $reverse = false)
{
if ((true === $vocabRelative) && array_key_exists($iri, $inversectx)) {
if (null !== $value) {
$valueProfile = $this->getValueProfile($value, $inversectx);
$container = ('@list' === $valueProfile['@container'])
? array('@list', '@null')
: array($valueProfile['@container'], '@set', '@null');
if (null === $valueProfile['typeLang']) {
$typeOrLang = array('@null');
$typeOrLangValue = array('@null');
} else {
$typeOrLang = array($valueProfile['typeLang'], '@null');
$typeOrLangValue = array();
if (true === $reverse) {
$typeOrLangValue[] = '@reverse';
}
if (('@type' === $valueProfile['typeLang']) && ('@id' === $valueProfile['typeLangValue'])) {
array_push($typeOrLangValue, '@id', '@vocab', '@null');
} elseif (('@type' === $valueProfile['typeLang']) &&
('@vocab' === $valueProfile['typeLangValue'])) {
array_push($typeOrLangValue, '@vocab', '@id', '@null');
} else {
$typeOrLangValue = array($valueProfile['typeLangValue'], '@null');
}
}
$result = $this->queryInverseContext($inversectx[$iri], $container, $typeOrLang, $typeOrLangValue);
if (null !== $result) {
return $result;
}
} elseif (isset($inversectx[$iri]['term'])) {
return $inversectx[$iri]['term'];
}
}
// Compact using @vocab
if ($vocabRelative && isset($activectx['@vocab']) && (0 === strpos($iri, $activectx['@vocab'])) &&
(false !== ($vocabIri = substr($iri, strlen($activectx['@vocab'])))) &&
(false === isset($activectx[$vocabIri]))) {
return $vocabIri;
}
// Try to compact to a compact IRI
foreach ($inversectx as $termIri => $def) {
$termIriLen = strlen($termIri);
if (isset($def['term']) && (0 === strncmp($iri, $termIri, $termIriLen))) {
$compactIri = substr($iri, $termIriLen);
if (false !== $compactIri && '' !== $compactIri) {
$compactIri = $def['term'] . ':' . $compactIri;
if (false === isset($activectx[$compactIri]) ||
((false === $vocabRelative) && ($iri === $activectx[$compactIri]['@id']))) {
return $compactIri;
}
}
}
}
// Last resort, convert to a relative IRI
if ((false === $vocabRelative) && (null !== $activectx['@base'])) {
return (string) $activectx['@base']->baseFor($iri);
}
// IRI couldn't be compacted, return as is
return $iri;
}
/**
* Verifies whether two JSON-LD subtrees are equal not
*
* Please note that two unlabeled blank nodes will never be equal by
* definition.
*
* @param mixed $a The first subtree.
* @param mixed $b The second subree.
*
* @return bool Returns true if the two subtrees are equal; otherwise
* false.
*/
private static function subtreeEquals($a, $b)
{
if (gettype($a) !== gettype($b)) {
return false;
}
if (is_scalar($a)) {
return ($a === $b);
}
if (is_array($a)) {
$len = count($a);
if ($len !== count($b)) {
return false;
}
// TODO Ignore order for sets?
for ($i = 0; $i < $len; $i++) {
if (false === self::subtreeEquals($a[$i], $b[$i])) {
return false;
}
}
return true;
}
if (!property_exists($a, '@id') &&
!property_exists($a, '@value') &&
!property_exists($a, '@list')) {
// Blank nodes can never match as they can't be identified
return false;
}
$properties = array_keys(get_object_vars($a));
if (count($properties) !== count(get_object_vars($b))) {
return false;
}
foreach ($properties as $property) {
if ((false === property_exists($b, $property)) ||
(false === self::subtreeEquals($a->{$property}, $b->{$property}))) {
return false;
}
}
return true;
}
/**
* Calculates a value profile
*
* A value profile represent the schema of the value ignoring the
* concrete value. It is an associative array containing the following
* keys-value pairs:
*
* * `@container`: the container, defaults to `@set`
* * `typeLang`: is set to `@type` for typed values or `@language` for
* (language-tagged) strings; for all other values it is set to
* `null`
* * `typeLangValue`: set to the type of a typed value or the language
* of a language-tagged string (`@null` for all other strings); for
* all other values it is set to `null`
*
* @param JsonObject $value The value.
* @param array $inversectx The inverse context.
*
* @return array The value profile.
*/
private function getValueProfile(JsonObject $value, $inversectx)
{
$valueProfile = array(
'@container' => '@set',
'typeLang' => '@type',
'typeLangValue' => '@id'
);
if (property_exists($value, '@index')) {
$valueProfile['@container'] = '@index';
}
if (property_exists($value, '@id')) {
if (isset($inversectx[$value->{'@id'}]['term'])) {
$valueProfile['typeLangValue'] = '@vocab';
} else {
$valueProfile['typeLangValue'] = '@id';
}
return $valueProfile;
}
if (property_exists($value, '@value')) {
if (property_exists($value, '@type')) {
$valueProfile['typeLang'] = '@type';
$valueProfile['typeLangValue'] = $value->{'@type'};
} elseif (property_exists($value, '@language')) {
$valueProfile['typeLang'] = '@language';
$valueProfile['typeLangValue'] = $value->{'@language'};
if (false === property_exists($value, '@index')) {
$valueProfile['@container'] = '@language';
}
} else {
$valueProfile['typeLang'] = '@language';
$valueProfile['typeLangValue'] = '@null';
}
return $valueProfile;
}
if (property_exists($value, '@list')) {
$len = count($value->{'@list'});
if ($len > 0) {
$valueProfile = $this->getValueProfile($value->{'@list'}[0], $inversectx);
}
if (false === property_exists($value, '@index')) {
$valueProfile['@container'] = '@list';
}
for ($i = $len - 1; $i > 0; $i--) {
$profile = $this->getValueProfile($value->{'@list'}[$i], $inversectx);
if (($valueProfile['typeLang'] !== $profile['typeLang']) ||
($valueProfile['typeLangValue'] !== $profile['typeLangValue'])) {
$valueProfile['typeLang'] = null;
$valueProfile['typeLangValue'] = null;
return $valueProfile;
}
}
}
return $valueProfile;
}
/**
* Queries the inverse context to find the term for a given query
* path (= value profile)
*
* @param array $inversectx The inverse context (or a subtree thereof)
* @param string[] $containers
* @param string[] $typeOrLangs
* @param string[] $typeOrLangValues
*
* @return null|string The best matching term or null if none was found.
*/
private function queryInverseContext($inversectx, $containers, $typeOrLangs, $typeOrLangValues)
{
foreach ($containers as $container) {
foreach ($typeOrLangs as $typeOrLang) {
foreach ($typeOrLangValues as $typeOrLangValue) {
if (isset($inversectx[$container][$typeOrLang][$typeOrLangValue])) {
return $inversectx[$container][$typeOrLang][$typeOrLangValue];
}
}
}
}
return null;
}
/**
* Returns a property's definition
*
* The result will be in the form
*
* <code>
* array('@type' => type or null,
* '@language' => language or null,
* '@container' => container or null,
* 'isKeyword' => true or false)
* </code>
*
* If `$only` is set, only the value of that key of the array
* above will be returned.
*
* @param array $activectx The active context.
* @param string $property The property.
* @param null|string $only If set, only this element of the
* definition will be returned.
*
* @return array|string|null Returns either the property's definition or
* null if not found.
*/
private function getPropertyDefinition($activectx, $property, $only = null)
{
$result = array(
'@reverse' => false,
'@type' => null,
'@language' => (isset($activectx['@language']))
? $activectx['@language']
: null,
'@index' => null,
'@container' => null,
'isKeyword' => false,
'compactArrays' => true
);
if (in_array($property, self::$keywords)) {
$result['@type'] = (('@id' === $property) || ('@type' === $property))
? '@id'
: null;
$result['@language'] = null;
$result['isKeyword'] = true;
$result['compactArrays'] = (bool) (('@list' !== $property) && ('@graph' !== $property));
} else {
$def = (isset($activectx[$property])) ? $activectx[$property] : null;
if (null !== $def) {
$result['@id'] = $def['@id'];
$result['@reverse'] = $def['@reverse'];
if (isset($def['@type'])) {
$result['@type'] = $def['@type'];
$result['@language'] = null;
} elseif (array_key_exists('@language', $def)) { // could be null
$result['@language'] = $def['@language'];
}
if (isset($def['@container'])) {
$result['@container'] = $def['@container'];
if (('@list' === $def['@container']) || ('@set' === $def['@container'])) {
$result['compactArrays'] = false;
}
}
}
}
if ($only) {
return (isset($result[$only])) ? $result[$only] : null;
}
return $result;
}
/**
* Processes a local context to update the active context
*
* @param mixed $loclctx The local context.
* @param array $activectx The active context.
* @param array $remotectxs The already included remote contexts.
*
* @throws JsonLdException
*/
public function processContext($loclctx, &$activectx, $remotectxs = array())
{
if (is_object($loclctx)) {
$loclctx = clone $loclctx;
}
if (false === is_array($loclctx)) {
$loclctx = array($loclctx);
}
foreach ($loclctx as $context) {
if (null === $context) {
$activectx = array('@base' => $this->baseIri);
} elseif (is_object($context)) {
// make sure we don't modify the passed context
$context = clone $context;
if (property_exists($context, '@base')) {
if (count($remotectxs) > 0) {
// do nothing, @base is ignored in a remote context
} elseif (null === $context->{'@base'}) {
$activectx['@base'] = null;
} elseif (false === is_string($context->{'@base'})) {
throw new JsonLdException(
JsonLdException::INVALID_BASE_IRI,
'The value of @base must be an IRI or null.',
$context
);
} else {
$base = new IRI($context->{'@base'});
if (false === $base->isAbsolute()) {
if (null === $activectx['@base']) {
throw new JsonLdException(
JsonLdException::INVALID_BASE_IRI,
'The relative base IRI cannot be resolved to an absolute IRI.',
$context
);
}
$activectx['@base'] = $activectx['@base']->resolve($base);
} else {
$activectx['@base'] = $base;
}
}
unset($context->{'@base'});
}
if (property_exists($context, '@vocab')) {
if (null === $context->{'@vocab'}) {
unset($activectx['@vocab']);
} elseif ((false === is_string($context->{'@vocab'})) ||
(false === strpos($context->{'@vocab'}, ':'))) {
throw new JsonLdException(
JsonLdException::INVALID_VOCAB_MAPPING,
'The value of @vocab must be an absolute IRI or null.invalid vocab mapping, ',
$context
);
} else {
$activectx['@vocab'] = $context->{'@vocab'};
}
unset($context->{'@vocab'});
}
if (property_exists($context, '@language')) {
if ((null !== $context->{'@language'}) && (false === is_string($context->{'@language'}))) {
throw new JsonLdException(
JsonLdException::INVALID_DEFAULT_LANGUAGE,
'The value of @language must be a string.',
$context
);
}
$activectx['@language'] = $context->{'@language'};
unset($context->{'@language'});
}
foreach ($context as $key => $value) {
unset($context->{$key});
unset($activectx[$key]);
if (in_array($key, self::$keywords)) {
throw new JsonLdException(JsonLdException::KEYWORD_REDEFINITION, null, $key);
}
if ((null === $value) || is_string($value)) {
$value = (object) array('@id' => $value);
} elseif (is_object($value)) {
$value = clone $value; // make sure we don't modify context entries
} else {
throw new JsonLdException(JsonLdException::INVALID_TERM_DEFINITION);
}
if (property_exists($value, '@reverse')) {
if (property_exists($value, '@id')) {
throw new JsonLdException(
JsonLdException::INVALID_REVERSE_PROPERTY,
"Invalid term definition using both @reverse and @id detected",
$value
);
}
if (property_exists($value, '@container') &&
('@index' !== $value->{'@container'}) &&
('@set' !== $value->{'@container'})) {
throw new JsonLdException(
JsonLdException::INVALID_REVERSE_PROPERTY,
"Terms using the @reverse feature support only @set- and @index-containers.",
$value
);
}
$value->{'@id'} = $value->{'@reverse'};
$value->{'@reverse'} = true;
} else {
$value->{'@reverse'} = false;
}
if (property_exists($value, '@id')) {
if ((null !== $value->{'@id'}) && (false === is_string($value->{'@id'}))) {
throw new JsonLdException(JsonLdException::INVALID_IRI_MAPPING, null, $value->{'@id'});
}
$path = array();
if ($key !== $value->{'@id'}) {
$path[] = $key;
}
$expanded = $this->expandIri($value->{'@id'}, $activectx, false, true, $context, $path);
if ($value->{'@reverse'} && (false === strpos($expanded, ':'))) {
throw new JsonLdException(
JsonLdException::INVALID_IRI_MAPPING,
"Reverse properties must expand to absolute IRIs, \"$key\" expands to \"$expanded\"."
);
} elseif ('@context' === $expanded) {
throw new JsonLdException(
JsonLdException::INVALID_KEYWORD_ALIAS,
'Aliases for @context are not supported',
$value
);
}
} else {
$expanded = $this->expandIri($key, $activectx, false, true, $context);
}
if ((null === $expanded) || in_array($expanded, self::$keywords)) {
// if it's an aliased keyword or the IRI is null, we ignore all other properties
// TODO Should we throw an exception if there are other properties?
$activectx[$key] = array('@id' => $expanded, '@reverse' => false);
continue;
} elseif (false === strpos($expanded, ':')) {
throw new JsonLdException(
JsonLdException::INVALID_IRI_MAPPING,
"Failed to expand \"$key\" to an absolute IRI.",
$loclctx
);
}
$activectx[$key] = array('@id' => $expanded, '@reverse' => $value->{'@reverse'});
if (isset($value->{'@type'})) {
if (false === is_string($value->{'@type'})) {
throw new JsonLdException(JsonLdException::INVALID_TYPE_MAPPING);
}
$expanded = $this->expandIri($value->{'@type'}, $activectx, false, true, $context);
if (('@id' !== $expanded) && ('@vocab' !== $expanded) &&
((false === strpos($expanded, ':') || (0 === strpos($expanded, '_:'))))) {
throw new JsonLdException(
JsonLdException::INVALID_TYPE_MAPPING,
"Failed to expand $expanded to an absolute IRI.",
$loclctx
);
}
$activectx[$key]['@type'] = $expanded;
} elseif (property_exists($value, '@language')) {
if ((false === is_string($value->{'@language'})) && (null !== $value->{'@language'})) {
throw new JsonLdException(
JsonLdException::INVALID_LANGUAGE_MAPPING,
'The value of @language must be a string or null.',
$value
);
}
// Note the else. Language tagging applies just to term without type coercion
$activectx[$key]['@language'] = $value->{'@language'};
}
if (isset($value->{'@container'})) {
if (in_array($value->{'@container'}, array('@list', '@set', '@language', '@index'))) {
$activectx[$key]['@container'] = $value->{'@container'};
} else {
throw new JsonLdException(
JsonLdException::INVALID_CONTAINER_MAPPING,
'A container mapping of ' . $value->{'@container'} . ' is not supported.'
);
}
}
}
} elseif (is_string($context)) {
$remoteContext = new IRI($context);
if ($remoteContext->isAbsolute()) {
$remoteContext = (string) $remoteContext;
} elseif (null === $activectx['@base']) {
throw new JsonLdException(
JsonLdException::INVALID_BASE_IRI,
'Can not resolve the relative URL of the remote context as no base has been set: ' . $remoteContext
);
} else {
$remoteContext = (string) $activectx['@base']->resolve($context);
}
if (in_array($remoteContext, $remotectxs)) {
throw new JsonLdException(
JsonLdException::RECURSIVE_CONTEXT_INCLUSION,
'Recursive inclusion of remote context: ' . join(' -> ', $remotectxs) . ' -> ' . $remoteContext
);
}
$remotectxs[] = $remoteContext;
try {
$remoteContext = $this->loadDocument($remoteContext);
} catch (JsonLdException $e) {
throw new JsonLdException(
JsonLdException::LOADING_REMOTE_CONTEXT_FAILED,
"Loading $remoteContext failed",
null,
null,
$e
);
}
if (is_object($remoteContext) && property_exists($remoteContext, '@context')) {
// TODO Use the context's IRI as base IRI when processing remote contexts (ISSUE-24)
$this->processContext($remoteContext->{'@context'}, $activectx, $remotectxs);
} else {
throw new JsonLdException(
JsonLdException::INVALID_REMOTE_CONTEXT,
'Remote context "' . $context . '" is invalid.',
$remoteContext
);
}
} else {
throw new JsonLdException(JsonLdException::INVALID_LOCAL_CONTEXT);
}
}
}
/**
* Load a JSON-LD document
*
* The document can be supplied directly as string, by passing a file
* path, or by passing a URL.
*
* @param null|string|array|JsonObject $input The JSON-LD document or a path
* or URL pointing to one.
*
* @return mixed The loaded JSON-LD document
*
* @throws JsonLdException
*/
private function loadDocument($input)
{
if (false === is_string($input)) {
// Return as is - it has already been parsed
return $input;
}
$document = $this->documentLoader->loadDocument($input);
return $document->document;
}
/**
* Creates an inverse context to simplify IRI compaction
*
* The inverse context is a multidimensional array that has the
* following shape:
*
* <code>
* [container|@null|term]
* [@type|@language][typeIRI|languageCode]
* [@null][@null]
* [term|propGen]
* [ array of terms ]
* </code>
*
* @param array $activectx The active context.
*
* @return array The inverse context.
*/
public function createInverseContext($activectx)
{
$inverseContext = array();
$defaultLanguage = isset($activectx['@language']) ? $activectx['@language'] : '@null';
$propertyGenerators = isset($activectx['@propertyGenerators']) ? $activectx['@propertyGenerators'] : array();
unset($activectx['@base']);
unset($activectx['@vocab']);
unset($activectx['@language']);
unset($activectx['@propertyGenerators']);
$activectx = array_merge($activectx, $propertyGenerators);
unset($propertyGenerators);
uksort($activectx, array($this, 'sortTerms'));
// Put every IRI of each term into the inverse context
foreach ($activectx as $term => $def) {
if (null === $def['@id']) {
// this is necessary since some terms can be decoupled from @vocab
continue;
}
$container = (isset($def['@container'])) ? $def['@container'] : '@null';
$iri = $def['@id'];
if (false === isset($inverseContext[$iri]['term']) && (false === $def['@reverse'])) {
$inverseContext[$iri]['term'] = $term;
}
$typeOrLang = '@null';
$typeLangValue = '@null';
if (true === $def['@reverse']) {
$typeOrLang = '@type';
$typeLangValue = '@reverse';
} elseif (isset($def['@type'])) {
$typeOrLang = '@type';
$typeLangValue = $def['@type'];
} elseif (array_key_exists('@language', $def)) { // can be null
$typeOrLang = '@language';
$typeLangValue = (null === $def['@language']) ? '@null' : $def['@language'];
} else {
// Every untyped term is implicitly set to the default language
if (false === isset($inverseContext[$iri][$container]['@language'][$defaultLanguage])) {
$inverseContext[$iri][$container]['@language'][$defaultLanguage] = $term;
}
}
if (false === isset($inverseContext[$iri][$container][$typeOrLang][$typeLangValue])) {
$inverseContext[$iri][$container][$typeOrLang][$typeLangValue] = $term;
}
}
// Sort the whole inverse context in reverse order, the longest IRI comes first
uksort($inverseContext, array($this, 'sortTerms'));
$inverseContext = array_reverse($inverseContext);
return $inverseContext;
}
/**
* Creates a node map of an expanded JSON-LD document
*
* All keys in the node map are prefixed with "-" to support empty strings.
*
* @param JsonObject $nodeMap The object holding the node map.
* @param JsonObject|JsonObject[] $element An expanded JSON-LD element to
* be put into the node map
* @param string $activegraph The graph currently being processed.
* @param null|string $activeid The node currently being processed.
* @param null|string $activeprty The property currently being processed.
* @param null|JsonObject $list The list object if a list is being
* processed.
*/
private function generateNodeMap(
&$nodeMap,
$element,
$activegraph = JsonLD::DEFAULT_GRAPH,
$activeid = null,
$activeprty = null,
&$list = null
) {
if (is_array($element)) {
foreach ($element as $item) {
$this->generateNodeMap($nodeMap, $item, $activegraph, $activeid, $activeprty, $list);
}
return;
}
// Relabel blank nodes in @type and add a node to the current graph
if (property_exists($element, '@type')) {
$types = null;
if (is_array($element->{'@type'})) {
$types = &$element->{'@type'};
} else {
$types = array(&$element->{'@type'});
}
foreach ($types as &$type) {
if (0 === strncmp($type, '_:', 2)) {
$type = $this->getBlankNodeId($type);
}
}
}
if (property_exists($element, '@value')) {
// Handle value objects
if (null === $list) {
$this->mergeIntoProperty(
$nodeMap->{'-' . $activegraph}->{'-' . $activeid},
$activeprty,
$element,
true,
true
);
} else {
$this->mergeIntoProperty($list, '@list', $element, true, false);
}
} elseif (property_exists($element, '@list')) {
// lists
$result = new JsonObject();
$result->{'@list'} = array();
$this->generateNodeMap($nodeMap, $element->{'@list'}, $activegraph, $activeid, $activeprty, $result);
$this->mergeIntoProperty(
$nodeMap->{'-' . $activegraph}->{'-' . $activeid},
$activeprty,
$result,
true,
false
);
} else {
// and node objects
if (false === property_exists($element, '@id')) {
$id = $this->getBlankNodeId();
} elseif (0 === strncmp($element->{'@id'}, '_:', 2)) {
$id = $this->getBlankNodeId($element->{'@id'});
} else {
$id = $element->{'@id'};
}
unset($element->{'@id'});
// Create node in node map if it doesn't exist yet
if (false === property_exists($nodeMap->{'-' . $activegraph}, '-' . $id)) {
$node = new JsonObject();
$node->{'@id'} = $id;
$nodeMap->{'-' . $activegraph}->{'-' . $id} = $node;
} else {
$node = $nodeMap->{'-' . $activegraph}->{'-' . $id};
}
// Add reference to active property
if (is_object($activeid)) {
$this->mergeIntoProperty($node, $activeprty, $activeid, true, true);
} elseif (null !== $activeprty) {
$reference = new JsonObject();
$reference->{'@id'} = $id;
if (null === $list) {
$this->mergeIntoProperty(
$nodeMap->{'-' . $activegraph}->{'-' . $activeid},
$activeprty,
$reference,
true,
true
);
} else {
$this->mergeIntoProperty($list, '@list', $reference, true, false);
}
}
if (property_exists($element, '@type')) {
$this->mergeIntoProperty($node, '@type', $element->{'@type'}, true, true);
unset($element->{'@type'});
}
if (property_exists($element, '@index')) {
$this->setProperty(
$node,
'@index',
$element->{'@index'},
JsonLdException::CONFLICTING_INDEXES
);
unset($element->{'@index'});
}
if (property_exists($element, '@reverse')) {
$reference = array('@id' => $id);
// First, add the reverse property to all nodes pointing to this node and then
// add them to the node mape
foreach (get_object_vars($element->{'@reverse'}) as $property => $value) {
foreach ($value as $val) {
$this->generateNodeMap($nodeMap, $val, $activegraph, (object) $reference, $property);
}
}
unset($element->{'@reverse'});
}
// This node also represent a named graph, process it
if (property_exists($element, '@graph')) {
if (JsonLD::MERGED_GRAPH !== $activegraph) {
if (false === property_exists($nodeMap, '-' . $id)) {
$nodeMap->{'-' . $id} = new JsonObject();
}
$this->generateNodeMap($nodeMap, $element->{'@graph'}, $id);
} else {
$this->generateNodeMap($nodeMap, $element->{'@graph'}, JsonLD::MERGED_GRAPH);
}
unset($element->{'@graph'});
}
// Process all other properties in order
$properties = get_object_vars($element);
ksort($properties);
foreach ($properties as $property => $value) {
if (0 === strncmp($property, '_:', 2)) {
$property = $this->getBlankNodeId($property);
}
if (false === property_exists($node, $property)) {
$node->{$property} = array();
}
$this->generateNodeMap($nodeMap, $value, $activegraph, $id, $property);
}
}
}
/**
* Generate a new blank node identifier
*
* If an identifier is passed, a new blank node identifier is generated
* for it and stored for subsequent use. Calling the method with the same
* identifier (except null) will thus always return the same blank node
* identifier.
*
* @param null|string $id If available, existing blank node identifier.
*
* @return string Returns a blank node identifier.
*/
private function getBlankNodeId($id = null)
{
if ((null !== $id) && isset($this->blankNodeMap[$id])) {
return $this->blankNodeMap[$id];
}
$bnode = '_:b' . $this->blankNodeCounter++;
$this->blankNodeMap[$id] = $bnode;
return $bnode;
}
/**
* Flattens a JSON-LD document
*
* @param mixed $element A JSON-LD element to be flattened.
*
* @return array An array representing the flattened element.
*/
public function flatten($element)
{
$nodeMap = new JsonObject();
$nodeMap->{'-' . JsonLD::DEFAULT_GRAPH} = new JsonObject();
$this->generateNodeMap($nodeMap, $element);
$defaultGraph = $nodeMap->{'-' . JsonLD::DEFAULT_GRAPH};
unset($nodeMap->{'-' . JsonLD::DEFAULT_GRAPH});
// Store named graphs in the @graph property of the node representing
// the graph in the default graph
foreach ($nodeMap as $graphName => $graph) {
if (!isset($defaultGraph->{$graphName})) {
$defaultGraph->{$graphName} = new JsonObject();
$defaultGraph->{$graphName}->{'@id'} = substr($graphName, 1);
}
$graph = (array) $graph;
ksort($graph);
$defaultGraph->{$graphName}->{'@graph'} = array_values(
array_filter($graph, array($this, 'hasNodeProperties'))
);
}
$defaultGraph = (array) $defaultGraph;
ksort($defaultGraph);
return array_values(
array_filter($defaultGraph, array($this, 'hasNodeProperties'))
);
}
/**
* Converts an expanded JSON-LD document to RDF quads
*
* The result is an array of Quads.
*
* @param array $document The expanded JSON-LD document to be transformed into quads.
*
* @return Quad[] The extracted quads.
*/
public function toRdf(array $document)
{
$nodeMap = new JsonObject();
$nodeMap->{'-' . JsonLD::DEFAULT_GRAPH} = new JsonObject();
$this->generateNodeMap($nodeMap, $document);
$result = array();
foreach ($nodeMap as $graphName => $graph) {
$graphName = substr($graphName, 1);
if (JsonLD::DEFAULT_GRAPH === $graphName) {
$activegraph = null;
} else {
$activegraph = new IRI($graphName);
if (false === $activegraph->isAbsolute()) {
continue;
}
}
foreach ($graph as $subject => $node) {
$activesubj = new IRI(substr($subject, 1));
if (false === $activesubj->isAbsolute()) {
continue;
}
foreach ($node as $property => $values) {
if ('@id' === $property) {
continue;
} elseif ('@type' === $property) {
$activeprty = new IRI(RdfConstants::RDF_TYPE);
foreach ($values as $value) {
$result[] = new Quad($activesubj, $activeprty, new IRI($value), $activegraph);
}
continue;
} elseif ('@' === $property[0]) {
continue;
}
// Exclude triples/quads with a blank node predicate if generalized RDF isn't enabled
if ((0 === strncmp($property, '_:', 2)) && (false === $this->generalizedRdf)) {
continue;
}
$activeprty = new IRI($property);
if (false === $activeprty->isAbsolute()) {
continue;
}
foreach ($values as $value) {
if (property_exists($value, '@list')) {
$quads = array();
$head = $this->listToRdf($value->{'@list'}, $quads, $activegraph);
$result[] = new Quad($activesubj, $activeprty, $head, $activegraph);
foreach ($quads as $quad) {
$result[] = $quad;
}
} else {
$object = $this->elementToRdf($value);
if (null === $object) {
continue;
}
$result[] = new Quad($activesubj, $activeprty, $object, $activegraph);
}
}
}
}
}
return $result;
}
/**
* Converts a JSON-LD element to a RDF Quad object
*
* @param JsonObject $element The element to be converted.
*
* @return IRI|TypedValue|LanguageTagged|null The converted element to be used as Quad object.
*/
private function elementToRdf(JsonObject $element)
{
if (property_exists($element, '@value')) {
return Value::fromJsonLd($element);
}
$iri = new IRI($element->{'@id'});
return $iri->isAbsolute() ? $iri : null;
}
/**
* Converts a JSON-LD list to a linked RDF list (quads)
*
* @param array $entries The list entries
* @param array $quads The array to be used to hold the linked list
* @param null|IRI $graph The graph to be used in the constructed Quads
*
* @return IRI Returns the IRI of the head of the list
*/
private function listToRdf(array $entries, array &$quads, IRI $graph = null)
{
if (0 === count($entries)) {
return new IRI(RdfConstants::RDF_NIL);
}
$head = new IRI($this->getBlankNodeId());
$quads[] = new Quad($head, new IRI(RdfConstants::RDF_FIRST), $this->elementToRdf($entries[0]), $graph);
$bnode = $head;
for ($i = 1, $len = count($entries); $i < $len; $i++) {
$next = new IRI($this->getBlankNodeId());
$quads[] = new Quad($bnode, new IRI(RdfConstants::RDF_REST), $next, $graph);
$object = $this->elementToRdf($entries[$i]);
if (null !== $object) {
$quads[] = new Quad($next, new IRI(RdfConstants::RDF_FIRST), $object, $graph);
}
$bnode = $next;
}
$quads[] = new Quad($bnode, new IRI(RdfConstants::RDF_REST), new IRI(RdfConstants::RDF_NIL), $graph);
return $head;
}
/**
* Converts an array of RDF quads to a JSON-LD document
*
* The resulting JSON-LD document will be in expanded form.
*
* @param Quad[] $quads The quads to convert
*
* @return array The JSON-LD document.
*
* @throws InvalidQuadException If the quad is invalid.
*/
public function fromRdf(array $quads)
{
$graphs = new JsonObject();
$graphs->{JsonLD::DEFAULT_GRAPH} = new JsonObject();
$usages = new JsonObject();
foreach ($quads as $quad) {
$graphName = JsonLD::DEFAULT_GRAPH;
if ($quad->getGraph()) {
$graphName = (string) $quad->getGraph();
// Add a reference to this graph to the default graph if it
// doesn't exist yet
if (false === isset($graphs->{JsonLD::DEFAULT_GRAPH}->{$graphName})) {
$graphs->{JsonLD::DEFAULT_GRAPH}->{$graphName} =
self::objectToJsonLd($quad->getGraph());
}
}
if (false === isset($graphs->{$graphName})) {
$graphs->{$graphName} = new JsonObject();
}
$graph = $graphs->{$graphName};
// Subjects and properties are always IRIs (blank nodes are IRIs
// as well): convert them to a string representation
$subject = (string) $quad->getSubject();
$property = (string) $quad->getProperty();
$object = $quad->getObject();
// All nodes are stored in the node map
if (false === isset($graph->{$subject})) {
$graph->{$subject} = self::objectToJsonLd($quad->getSubject());
}
$node = $graph->{$subject};
// ... as are all objects that are IRIs or blank nodes
if (($object instanceof IRI) && (false === isset($graph->{(string) $object}))) {
$graph->{(string) $object} = self::objectToJsonLd($object);
}
if (($property === RdfConstants::RDF_TYPE) && (false === $this->useRdfType) &&
($object instanceof IRI)) {
self::mergeIntoProperty($node, '@type', (string) $object, true, true);
} else {
$value = self::objectToJsonLd($object, $this->useNativeTypes);
self::mergeIntoProperty($node, $property, $value, true, true);
// If the object is an IRI or blank node it might be the
// beginning of a list. Store a reference to its usage so
// that we can replace it with a list object later
if ($object instanceof IRI) {
$objectStr = (string) $object;
// Usages of rdf:nil are stored per graph, while...
if (RdfConstants::RDF_NIL == $objectStr) {
$graph->{$objectStr}->usages[] = array(
'node' => $node,
'prop' => $property,
'value' => $value);
// references to other nodes are stored globally (blank nodes could be shared across graphs)
} else {
if (!isset($usages->{$objectStr})) {
$usages->{$objectStr} = array();
}
// Make sure that the same triple isn't counted multiple times
// TODO Making $usages->{$objectStr} a set would make this code simpler
$graphSubjectProperty = $graphName . '|' . $subject . '|' . $property;
if (false === isset($usages->{$objectStr}[$graphSubjectProperty])) {
$usages->{$objectStr}[$graphSubjectProperty] = array(
'graph' => $graphName,
'node' => $node,
'prop' => $property,
'value' => $value);
}
}
}
}
}
// Transform linked lists to @list objects
$this->createListObjects($graphs, $usages);
// Generate the resulting document starting with the default graph
$document = array();
$nodes = get_object_vars($graphs->{JsonLD::DEFAULT_GRAPH});
ksort($nodes);
foreach ($nodes as $id => $node) {
// is it a named graph?
if (isset($graphs->{$id})) {
$node->{'@graph'} = array();
$graphNodes = get_object_vars($graphs->{$id});
ksort($graphNodes);
foreach ($graphNodes as $graphNodeId => $graphNode) {
// Only add the node when it has properties other than @id
if (count(get_object_vars($graphNode)) > 1) {
$node->{'@graph'}[] = $graphNode;
}
}
}
if (count(get_object_vars($node)) > 1) {
$document[] = $node;
}
}
return $document;
}
/**
* Reconstruct @list arrays from linked list structures
*
* @param JsonObject $graphs The graph map
* @param JsonObject $usages The global node usage map
*/
private function createListObjects($graphs, $usages)
{
foreach ($graphs as $graph) {
if (false === isset($graph->{RdfConstants::RDF_NIL})) {
continue;
}
$nil = $graph->{RdfConstants::RDF_NIL};
foreach ($nil->usages as $usage) {
$u = $usage;
$node = $u['node'];
$prop = $u['prop'];
$head = $u['value'];
$list = array();
$listNodes = array();
while ((RdfConstants::RDF_REST === $prop) &&
(1 === count($usages->{$node->{'@id'}})) &&
property_exists($node, RdfConstants::RDF_FIRST) &&
property_exists($node, RdfConstants::RDF_REST) &&
(1 === count($node->{RdfConstants::RDF_FIRST})) &&
(1 === count($node->{RdfConstants::RDF_REST})) &&
((3 === count(get_object_vars($node))) || // only @id, rdf:first & rdf:next
((4 === count(get_object_vars($node))) && // or an additional rdf:type = rdf:List
property_exists($node, '@type') &&
($node->{'@type'} === array(RdfConstants::RDF_LIST)))
)
) {
$list[] = reset($node->{RdfConstants::RDF_FIRST});
$listNodes[] = $node->{'@id'};
$u = reset($usages->{$node->{'@id'}});
$node = $u['node'];
$prop = $u['prop'];
$head = $u['value'];
if (0 !== strncmp($node->{'@id'}, '_:', 2)) {
break;
}
};
// The list is nested in another list
if (RdfConstants::RDF_FIRST === $prop) {
// If it is empty, we can't do anything but keep the rdf:nil node
if (RdfConstants::RDF_NIL === $head->{'@id'}) {
continue;
}
// ... otherwise we keep the head and convert the rest to @list
$head = $graph->{$head->{'@id'}};
$head = reset($head->{RdfConstants::RDF_REST});
array_pop($list);
array_pop($listNodes);
}
unset($head->{'@id'});
$head->{'@list'} = array_reverse($list);
foreach ($listNodes as $node) {
unset($graph->{$node});
}
}
unset($nil->usages);
}
}
/**
* Frames a JSON-LD document according a supplied frame
*
* @param array|JsonObject $element A JSON-LD element to be framed.
* @param mixed $frame The frame.
*
* @return array $result The framed element in expanded form.
*
* @throws JsonLdException
*/
public function frame($element, $frame)
{
if ((false === is_array($frame)) || (1 !== count($frame)) || (false === is_object($frame[0]))) {
throw new JsonLdException(
JsonLdException::UNSPECIFIED,
'The frame is invalid. It must be a single object.',
$frame
);
}
$frame = $frame[0];
$options = new JsonObject();
$options->{'@embed'} = true;
$options->{'@embedChildren'} = true; // TODO Change this as soon as the tests haven been updated
foreach (self::$framingKeywords as $keyword) {
if (property_exists($frame, $keyword)) {
$options->{$keyword} = $frame->{$keyword};
unset($frame->{$keyword});
} elseif (false === property_exists($options, $keyword)) {
$options->{$keyword} = false;
}
}
$procOptions = new JsonObject();
$procOptions->base = (string) $this->baseIri; // TODO Check which base IRI to use
$procOptions->compactArrays = $this->compactArrays;
$procOptions->optimize = $this->optimize;
$procOptions->useNativeTypes = $this->useNativeTypes;
$procOptions->useRdfType = $this->useRdfType;
$procOptions->produceGeneralizedRdf = $this->generalizedRdf;
$procOptions->documentFactory = $this->documentFactory;
$procOptions->documentLoader = $this->documentLoader;
$processor = new Processor($procOptions);
$graph = JsonLD::MERGED_GRAPH;
if (property_exists($frame, '@graph')) {
$graph = JsonLD::DEFAULT_GRAPH;
}
$nodeMap = new JsonObject();
$nodeMap->{'-' . $graph} = new JsonObject();
$processor->generateNodeMap($nodeMap, $element, $graph);
// Sort the node map to ensure a deterministic output
// TODO Move this to a separate function as basically the same is done in flatten()?
$nodeMap = (array) $nodeMap;
foreach ($nodeMap as &$nodes) {
$nodes = (array) $nodes;
ksort($nodes);
$nodes = (object) $nodes;
}
$nodeMap = (object) $nodeMap;
unset($processor);
$result = array();
foreach ($nodeMap->{'-' . $graph} as $node) {
$this->nodeMatchesFrame($node, $frame, $options, $nodeMap, $graph, $result);
}
return $result;
}
/**
* Checks whether a node matches a frame or not.
*
* @param JsonObject $node The node.
* @param null|JsonObject $frame The frame.
* @param JsonObject $options The current framing options.
* @param JsonObject $nodeMap The node map.
* @param string $graph The currently used graph.
* @param array $parent The parent to which matching results should be added.
* @param array $path The path of already processed nodes.
*
* @return bool Returns true if the node matches the frame, otherwise false.
*/
private function nodeMatchesFrame($node, $frame, $options, $nodeMap, $graph, &$parent, $path = array())
{
// TODO How should lists be handled? Is the @list required in the frame (current behavior) or not?
// https://github.com/json-ld/json-ld.org/issues/110
// TODO Add support for '@omitDefault'?
$filter = null;
if (null !== $frame) {
$filter = get_object_vars($frame);
}
$result = new JsonObject();
// Make sure that @id is always in the result if the node matches the filter
if (property_exists($node, '@id')) {
$result->{'@id'} = $node->{'@id'};
if ((null === $filter) && in_array($node->{'@id'}, $path)) {
$parent[] = $result;
return true;
}
$path[] = $node->{'@id'};
}
// If no filter is specified, simply return the passed node - {} is a wildcard
if ((null === $filter) || (0 === count($filter))) {
// TODO What effect should @explicit have with a wildcard match?
if (is_object($node)) {
if ((true === $options->{'@embed'}) || (false === property_exists($node, '@id'))) {
$this->addMissingNodeProperties($node, $options, $nodeMap, $graph, $result, $path);
}
$parent[] = $result;
} else {
$parent[] = $node;
}
return true;
}
foreach ($filter as $property => $validValues) {
if (is_array($validValues) && (0 === count($validValues))) {
if (property_exists($node, $property) ||
(('@graph' === $property) && isset($result->{'@id'}) &&
property_exists($nodeMap, $result->{'@id'}))) {
return false; // [] says that the property must not exist but it does
}
continue;
}
// If the property does not exist or is empty
if ((false === property_exists($node, $property)) || (is_array($node->{$property}) && 0 === count($node->{$property}))) {
// first check if it's @graph and whether the referenced graph exists
if ('@graph' === $property) {
if (isset($result->{'@id'}) && property_exists($nodeMap, $result->{'@id'})) {
$result->{'@graph'} = array();
$match = false;
foreach ($nodeMap->{'-' . $result->{'@id'}} as $item) {
foreach ($validValues as $validValue) {
$match |= $this->nodeMatchesFrame(
$item,
$validValue,
$options,
$nodeMap,
$result->{'@id'},
$result->{'@graph'}
);
}
}
if (false === $match) {
return false;
} else {
continue; // with next property
}
} else {
// the referenced graph doesn't exist
return false;
}
}
// otherwise, look if we have a default value for it
if (false === is_array($validValues)) {
$validValues = array($validValues);
}
$defaultFound = false;
foreach ($validValues as $validValue) {
if (is_object($validValue) && property_exists($validValue, '@default')) {
if (null === $validValue->{'@default'}) {
$result->{$property} = new JsonObject();
$result->{$property}->{'@null'} = true;
} else {
$result->{$property} = (is_array($validValue->{'@default'}))
? $validValue->{'@default'}
: array($validValue->{'@default'});
}
$defaultFound = true;
break;
}
}
if (true === $defaultFound) {
continue;
}
return false; // required property does not exist and no default value was found
}
// Check whether the values of the property match the filter
$match = false;
$result->{$property} = array();
if (false === is_array($validValues)) {
if ($node->{$property} === $validValues) {
$result->{$property} = $node->{$property};
continue;
} else {
return false;
}
}
foreach ($validValues as $validValue) {
if (is_object($validValue)) {
// Extract framing options from subframe ($validValue is a subframe)
$validValue = clone $validValue;
$newOptions = clone $options;
unset($newOptions->{'@default'});
foreach (self::$framingKeywords as $keyword) {
if (property_exists($validValue, $keyword)) {
$newOptions->{$keyword} = $validValue->{$keyword};
unset($validValue->{$keyword});
}
}
$nodeValues = $node->{$property};
if (false === is_array($nodeValues)) {
$nodeValues = array($nodeValues);
}
foreach ($nodeValues as $value) {
if (is_object($value) && property_exists($value, '@id')) {
$match |= $this->nodeMatchesFrame(
$nodeMap->{'-' . $graph}->{'-' . $value->{'@id'}},
$validValue,
$newOptions,
$nodeMap,
$graph,
$result->{$property},
$path
);
} else {
$match |= $this->nodeMatchesFrame(
$value,
$validValue,
$newOptions,
$nodeMap,
$graph,
$result->{$property},
$path
);
}
}
} elseif (is_array($validValue)) {
throw new JsonLdException(
JsonLdException::UNSPECIFIED,
"Invalid frame detected. Property \"$property\" must not be an array of arrays.",
$frame
);
} else {
// This will just catch non-expanded IRIs for @id and @type
$nodeValues = $node->{$property};
if (false === is_array($nodeValues)) {
$nodeValues = array($nodeValues);
}
if (in_array($validValue, $nodeValues)) {
$match = true;
$result->{$property} = $node->{$property};
}
}
}
if (false === $match) {
return false;
}
}
// Discard subtree if this object should not be embedded
if ((false === $options->{'@embed'}) && property_exists($node, '@id')) {
$result = new JsonObject();
$result->{'@id'} = $node->{'@id'};
$parent[] = $result;
return true;
}
// all properties matched the filter, add the properties of the
// node which haven't been added yet
if (false === $options->{'@explicit'}) {
$this->addMissingNodeProperties($node, $options, $nodeMap, $graph, $result, $path);
}
$parent[] = $result;
return true;
}
/**
* Adds all properties from node to result if they haven't been added yet
*
* @param JsonObject $node The node whose properties should processed.
* @param JsonObject $options The current framing options.
* @param JsonObject $nodeMap The node map.
* @param string $graph The currently used graph.
* @param JsonObject $result The object to which the properties should be added.
* @param array $path The path of already processed nodes.
*/
private function addMissingNodeProperties($node, $options, $nodeMap, $graph, &$result, $path)
{
foreach ($node as $property => $value) {
if (property_exists($result, $property)) {
continue; // property has already been added
}
if (true === $options->{'@embedChildren'}) {
if (false === is_array($value)) {
$result->{$property} = unserialize(serialize($value)); // create a deep-copy
continue;
}
$result->{$property} = array();
foreach ($value as $item) {
if (is_object($item)) {
if (property_exists($item, '@id')) {
$item = $nodeMap->{'-' . $graph}->{'-' . $item->{'@id'}};
}
$this->nodeMatchesFrame($item, null, $options, $nodeMap, $graph, $result->{$property}, $path);
} else {
$result->{$property}[] = $item;
}
}
} else {
// TODO Perform deep object copy??
$result->{$property} = unserialize(serialize($value)); // create a deep-copy
}
}
}
/**
* Adds a property to an object if it doesn't exist yet
*
* If the property already exists, an exception is thrown as otherwise
* the existing value would be lost.
*
* @param JsonObject $object The object.
* @param string $property The name of the property.
* @param mixed $value The value of the property.
*
* @throws JsonLdException If the property exists already JSON-LD.
*/
private static function setProperty(&$object, $property, $value, $errorCode = null)
{
if (property_exists($object, $property) &&
(false === self::subtreeEquals($object->{$property}, $value))) {
if ($errorCode) {
throw new JsonLdException(
$errorCode,
"Object already contains a property \"$property\".",
$object
);
}
throw new JsonLdException(
JsonLdException::UNSPECIFIED,
"Object already contains a property \"$property\".",
$object
);
}
$object->{$property} = $value;
}
/**
* Merges a value into a property of an object
*
* @param JsonObject $object The object.
* @param string $property The name of the property to which the value
* should be merged into.
* @param mixed $value The value to merge into the property.
* @param bool $alwaysArray If set to true, the resulting property will
* always be an array.
* @param bool $unique If set to true, the value is only added if
* it doesn't exist yet.
*/
private static function mergeIntoProperty(&$object, $property, $value, $alwaysArray = false, $unique = false)
{
// No need to add a null value
if (null === $value) {
return;
}
if (is_array($value)) {
// Make sure empty arrays are created since we preserve them in expansion
if ((0 === count($value)) && (false === property_exists($object, $property))) {
$object->{$property} = array();
}
foreach ($value as $val) {
static::mergeIntoProperty($object, $property, $val, $alwaysArray, $unique);
}
return;
}
if (property_exists($object, $property)) {
if (false === is_array($object->{$property})) {
$object->{$property} = array($object->{$property});
}
if ($unique) {
foreach ($object->{$property} as $item) {
if (self::subtreeEquals($item, $value)) {
return;
}
}
}
$object->{$property}[] = $value;
} else {
$object->{$property} = ($alwaysArray) ? array($value) : $value;
}
}
/**
* Compares two values by their length and then lexicographically
*
* If two strings have different lengths, the shorter one will be
* considered less than the other. If they have the same length, they
* are compared lexicographically.
*
* @param mixed $a Value A.
* @param mixed $b Value B.
*
* @return int If value A is shorter than value B, -1 will be returned; if it's
* longer 1 will be returned. If both values have the same length
* and value A is considered lexicographically less, -1 will be
* returned, if they are equal 0 will be returned, otherwise 1
* will be returned.
*/
private static function sortTerms($a, $b)
{
$lenA = strlen($a);
$lenB = strlen($b);
if ($lenA < $lenB) {
return -1;
} elseif ($lenA === $lenB) {
return strcmp($a, $b);
} else {
return 1;
}
}
/**
* Converts an object to a JSON-LD representation
*
* Only {@link IRI IRIs}, {@link LanguageTaggedString language-tagged strings},
* and {@link TypedValue typed values} are converted by this method. All
* other objects are returned as-is.
*
* @param JsonObject $object The object to convert.
* @param boolean $useNativeTypes If set to true, native types are used
* for xsd:integer, xsd:double, and
* xsd:boolean, otherwise typed strings
* will be used instead.
*
* @return mixed The JSON-LD representation of the object.
*/
private static function objectToJsonLd($object, $useNativeTypes = true)
{
if ($object instanceof IRI) {
$result = new JsonObject();
$result->{'@id'} = (string) $object;
return $result;
} elseif ($object instanceof Value) {
return $object->toJsonLd($useNativeTypes);
}
return $object;
}
/**
* Checks whether a node has properties and not just an @id
*
* This is used to filter nodes consisting just of an @id-member when
* flattening and converting from RDF.
*
* @param JsonObject $node The node
*
* @return boolean True if the node has properties (other than @id),
* false otherwise.
*/
private function hasNodeProperties($node)
{
return (count(get_object_vars($node)) > 1);
}
}
|