-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mateu Aguiló Bosch
committed
Dec 27, 2014
1 parent
f1351b9
commit 6aa553c
Showing
5 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
lib/Drupal/Component/Serialization/Exception/InvalidDataTypeException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
45
lib/Drupal/Component/Serialization/SerializationInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
|
||
} |