Type.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /*
  3. * This file is part of the ActivityPhp package.
  4. *
  5. * Copyright (c) landrok at github.com/landrok
  6. *
  7. * For the full copyright and license information, please see
  8. * <https://github.com/landrok/activitypub/blob/master/LICENSE>.
  9. */
  10. namespace Plugin\ActivityStreamsTwo\Util;
  11. use Exception;
  12. use Plugin\ActivityStreamsTwo\Util\Type\AbstractObject;
  13. use Plugin\ActivityStreamsTwo\Util\Type\TypeResolver;
  14. use Plugin\ActivityStreamsTwo\Util\Type\Validator;
  15. /**
  16. * \ActivityPhp\Type is a Factory for ActivityStreams 2.0 types.
  17. *
  18. * It provides shortcuts methods for type instantiation and more.
  19. *
  20. * @see https://www.w3.org/TR/activitystreams-vocabulary/#types
  21. * @see https://www.w3.org/TR/activitystreams-vocabulary/#activity-types
  22. * @see https://www.w3.org/TR/activitystreams-vocabulary/#actor-types
  23. * @see https://www.w3.org/TR/activitystreams-vocabulary/#object-types
  24. */
  25. abstract class Type
  26. {
  27. /**
  28. * Factory method to create type instance and set attributes values
  29. *
  30. * To see which default types are defined and their attributes:
  31. *
  32. * @see https://www.w3.org/TR/activitystreams-vocabulary/#types
  33. * @see https://www.w3.org/TR/activitystreams-vocabulary/#activity-types
  34. * @see https://www.w3.org/TR/activitystreams-vocabulary/#actor-types
  35. * @see https://www.w3.org/TR/activitystreams-vocabulary/#object-types
  36. *
  37. * @param array<string,mixed>|string $type
  38. * @param array<string,mixed> $attributes
  39. *
  40. * @throws Exception
  41. *
  42. * @return mixed
  43. */
  44. public static function create($type, array $attributes = [])
  45. {
  46. if (!is_string($type) && !is_array($type)) {
  47. throw new Exception(
  48. 'Type parameter must be a string or an array. Given='
  49. . gettype($type)
  50. );
  51. }
  52. if (is_array($type)) {
  53. if (!isset($type['type'])) {
  54. throw new Exception(
  55. "Type parameter must have a 'type' key"
  56. );
  57. }
  58. $attributes = $type;
  59. }
  60. try {
  61. $class = is_array($type)
  62. ? TypeResolver::getClass($type['type'])
  63. : TypeResolver::getClass($type);
  64. } catch (Exception $exception) {
  65. $message = json_encode($attributes, JSON_PRETTY_PRINT);
  66. throw new Exception(
  67. $exception->getMessage() . "\n{$message}"
  68. );
  69. }
  70. if (is_string($class)) {
  71. $class = new $class();
  72. }
  73. foreach ($attributes as $name => $value) {
  74. try {
  75. $class->set($name, $value);
  76. } catch (Exception) {
  77. // Discard invalid properties
  78. }
  79. }
  80. return $class;
  81. }
  82. /**
  83. * Create an activitystream type from a JSON string
  84. */
  85. public static function fromJson(string $json): AbstractObject
  86. {
  87. $data = json_decode($json, true);
  88. if (json_last_error() === JSON_ERROR_NONE
  89. && is_array($data)
  90. ) {
  91. return self::create($data);
  92. }
  93. throw new Exception(
  94. sprintf(
  95. "An error occurred during the JSON decoding.\n '%s'",
  96. $json
  97. )
  98. );
  99. }
  100. /**
  101. * Add a custom validator for an attribute.
  102. * It checks that it implements Validator\Interface
  103. *
  104. * @param string $name An attribute name to validate.
  105. * @param string $class A validator class name
  106. */
  107. public static function addValidator(string $name, string $class): void
  108. {
  109. Validator::add($name, $class);
  110. }
  111. }