You are here

class JsonSchemaEncoder in Schemata 8

Encodes data into json_schema.

Simply respond to application/vnd.api+json format requests using encoder.

Hierarchy

  • class \Drupal\serialization\Encoder\JsonEncoder extends \Symfony\Component\Serializer\Encoder\JsonEncoder implements \Symfony\Component\Serializer\Encoder\EncoderInterface, \Symfony\Component\Serializer\Encoder\DecoderInterface

Expanded class hierarchy of JsonSchemaEncoder

1 string reference to 'JsonSchemaEncoder'
schemata_json_schema.services.yml in schemata_json_schema/schemata_json_schema.services.yml
schemata_json_schema/schemata_json_schema.services.yml
1 service uses JsonSchemaEncoder
serializer.encoder.json_schema.json in schemata_json_schema/schemata_json_schema.services.yml
Drupal\schemata_json_schema\Encoder\JsonSchemaEncoder

File

schemata_json_schema/src/Encoder/JsonSchemaEncoder.php, line 13

Namespace

Drupal\schemata_json_schema\Encoder
View source
class JsonSchemaEncoder extends JsonEncoder {

  /**
   * The formats that this Encoder supports.
   *
   * @var string
   */
  protected $baseFormat = 'schema_json';

  /**
   * The decorated encoder.
   *
   * @var \Symfony\Component\Serializer\Encoder\EncoderInterface
   */
  protected $innerEncoder;

  /**
   * Create a JsonSchemaEncoder instance.
   *
   * @param \Symfony\Component\Serializer\Encoder\EncoderInterface $inner_encoder
   */
  public function __construct(EncoderInterface $inner_encoder) {
    parent::__construct();
    $this->innerEncoder = $inner_encoder;
  }

  /**
   * {@inheritdoc}
   */
  public function supportsEncoding($format) {
    $parts = explode(':', $format, 2);
    if (count($parts) < 2 || empty($parts[1])) {

      // Require sub type.
      return FALSE;
    }
    list($base, $encoded) = $parts;

    // Verify the correct base and that the sub type is supported by inner.
    return $base === $this->baseFormat && $this->innerEncoder
      ->supportsEncoding($encoded);
  }

  /**
   * {@inheritdoc}
   */
  public function supportsDecoding($format) {

    // We don't decode have a decoding system for json_schema.
    // @TODO: Implement conversion of json_schema to typed data.
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
JsonEncoder::$format protected static property The formats that this Encoder supports.
JsonEncoder::getJsonDecode private function Instantiates a JsonDecode instance.
JsonEncoder::getJsonEncode private function Instantiates a JsonEncode instance.
JsonSchemaEncoder::$baseFormat protected property The formats that this Encoder supports.
JsonSchemaEncoder::$innerEncoder protected property The decorated encoder.
JsonSchemaEncoder::supportsDecoding public function Checks whether the deserializer can decode from given format. Overrides JsonEncoder::supportsDecoding
JsonSchemaEncoder::supportsEncoding public function Checks whether the serializer can encode to given format. Overrides JsonEncoder::supportsEncoding
JsonSchemaEncoder::__construct public function Create a JsonSchemaEncoder instance. Overrides JsonEncoder::__construct