Skip to content

Commit

Permalink
Added serialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateu Aguiló Bosch committed Dec 27, 2014
1 parent f1351b9 commit 6aa553c
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/**
* @file
* Contains \Drupal\Component\Serialization\Exception\InvalidDataTypeException.
*/

namespace Drupal\Component\Serialization\Exception;

/**
* Exception thrown when a data type is invalid.
*/
class InvalidDataTypeException extends \InvalidArgumentException {
}
41 changes: 41 additions & 0 deletions lib/Drupal/Component/Serialization/Json.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/**
* @file
* Contains \Drupal\Component\Serialization\Json.
*/

namespace Drupal\Component\Serialization;

/**
* Default serialization for JSON.
*
* @ingroup third_party
*/
class Json implements SerializationInterface {

/**
* {@inheritdoc}
*
* Uses HTML-safe strings, with several characters escaped.
*/
public static function encode($variable) {
// Encode <, >, ', &, and ".
return json_encode($variable, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT);
}

/**
* {@inheritdoc}
*/
public static function decode($string) {
return json_decode($string, TRUE);
}

/**
* {@inheritdoc}
*/
public static function getFileExtension() {
return 'json';
}

}
36 changes: 36 additions & 0 deletions lib/Drupal/Component/Serialization/PhpSerialize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* @file
* Contains \Drupal\Component\Serialization\PhpSerialize.
*/

namespace Drupal\Component\Serialization;

/**
* Default serialization for serialized PHP.
*/
class PhpSerialize implements SerializationInterface {

/**
* {@inheritdoc}
*/
public static function encode($data) {
return serialize($data);
}

/**
* {@inheritdoc}
*/
public static function decode($raw) {
return unserialize($raw);
}

/**
* {@inheritdoc}
*/
public static function getFileExtension() {
return 'serialized';
}

}
45 changes: 45 additions & 0 deletions lib/Drupal/Component/Serialization/SerializationInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/**
* @file
* Contains \Drupal\Component\Serialization\SerializationInterface.
*/

namespace Drupal\Component\Serialization;

/**
* Defines an interface for serialization formats.
*/
interface SerializationInterface {

/**
* Encodes data into the serialization format.
*
* @param mixed $data
* The data to encode.
*
* @return string
* The encoded data.
*/
public static function encode($data);

/**
* Decodes data from the serialization format.
*
* @param string $raw
* The raw data string to decode.
*
* @return mixed
* The decoded data.
*/
public static function decode($raw);

/**
* Returns the file extension for this serialization format.
*
* @return string
* The file extension, without leading dot.
*/
public static function getFileExtension();

}
49 changes: 49 additions & 0 deletions lib/Drupal/Component/Serialization/Yaml.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/**
* @file
* Contains \Drupal\Component\Serialization\Yaml.
*/

namespace Drupal\Component\Serialization;

use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
use Symfony\Component\Yaml\Yaml as Symfony;

/**
* Default serialization for YAML using the Symfony component.
*/
class Yaml implements SerializationInterface {

/**
* {@inheritdoc}
*/
public static function encode($data) {
try {
return Symfony::dump($data, PHP_INT_MAX, 2, TRUE);
}
catch (\Exception $e) {
throw new InvalidDataTypeException($e->getMessage(), $e->getCode(), $e);
}
}

/**
* {@inheritdoc}
*/
public static function decode($raw) {
try {
return Symfony::parse($raw, TRUE);
}
catch (\Exception $e) {
throw new InvalidDataTypeException($e->getMessage(), $e->getCode(), $e);
}
}

/**
* {@inheritdoc}
*/
public static function getFileExtension() {
return 'yml';
}

}

0 comments on commit 6aa553c

Please sign in to comment.