Skip to content

Commit

Permalink
use json instead of php serialization for objects (backwards compatible)
Browse files Browse the repository at this point in the history
moved php requirement to >=7.0
  • Loading branch information
Zauberfisch committed Oct 4, 2018
1 parent 1517bb8 commit d28ea63
Show file tree
Hide file tree
Showing 20 changed files with 249 additions and 99 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
}
],
"require": {
"php": "^7",
"ext-json": "*",
"silverstripe/framework": "^3.6.4",
"zauberfisch/silverstripe-namespace-templates": "^1.0"
}
Expand Down
88 changes: 55 additions & 33 deletions src/AbstractDataObject.php
Original file line number Diff line number Diff line change
@@ -1,34 +1,56 @@
<?php
declare(strict_types=1);

namespace zauberfisch\SerializedDataObject;

require_once 'Serialize/JsonSerializer.php';

use zauberfisch\NamespaceTemplates\Form\FormField;
use zauberfisch\SerializedDataObject\Serialize\JsonSerializable;
use zauberfisch\SerializedDataObject\Serialize\JsonSerializer;
use zauberfisch\SerializedDataObject\Serialize\Serializer;

/**
* @author Zauberfisch
* @method static \Config_ForClass|\stdClass config
*/
abstract class AbstractDataObject extends \ViewableData implements \Serializable, \JsonSerializable, \i18nEntityProvider {
abstract class AbstractDataObject extends \ViewableData implements \Serializable, JsonSerializable, \i18nEntityProvider {
private static $fields = [];
private static $lists = [];
protected $fieldsData = [];
protected $listsData = [];


use JsonSerializer {
jsonSerialize as jsonSerializeTrait;
}

public function jsonSerialize() {
return array_merge([
'class' => $this->class,
'fieldsData' => $this->fieldsData,
'listsData' => $this->listsData,
]);
], $this->jsonSerializeTrait());
}


public function jsonDeserialize(array $data = null) {
$this->fieldsData = isset($data['fieldsData']) ? $data['fieldsData'] : [];
$this->listsData = isset($data['listsData']) ? $data['listsData'] : [];
}

/**
* @deprecated 4.0 Support for php serialisation will be removed in Version 4.0
* @return string
*/
public function serialize() {
return serialize([
'fieldsData' => $this->fieldsData,
'listsData' => $this->listsData,
]);
}


/**
* @deprecated 4.0 Support for php serialisation will be removed in Version 4.0
* @param string $serialized
*/
public function unserialize($serialized) {
$data = unserialize($serialized);
$this->class = get_class($this);
Expand All @@ -40,19 +62,19 @@ public function unserialize($serialized) {
}
$extensions = \Config::inst()->get($class, 'extensions',
\Config::UNINHERITED | \Config::EXCLUDE_EXTRA_SOURCES);

if ($extensions) foreach ($extensions as $extension) {
$instance = self::create_from_string($extension);
$instance->setOwner(null, $class);
$this->extension_instances[$instance->class] = $instance;
}
}
}

public function __get($fieldName) {
return $this->getField($fieldName);
}

public function __set($fieldName, $value) {
return $this->setField($fieldName, $value);
}
Expand All @@ -69,7 +91,7 @@ public function __set($fieldName, $value) {
// // }
// return parent::__call($method, $arguments);
// }

public function defineMethods() {
parent::defineMethods();
// TODO how to handle method name collisions?
Expand All @@ -86,11 +108,11 @@ public function defineMethods() {
//$this->addWrapperMethod("get$field", 'getField');
}
}

public function hasField($name) {
return in_array($name, static::config()->fields);
}

public function getField($name) {
if ($this->hasField($name)) {
if (isset($this->fieldsData[$name])) {
Expand All @@ -100,19 +122,19 @@ public function getField($name) {
}
throw new \Exception("Could not find field '$name'.");
}

public function setField($name, $value) {
if ($this->hasField($name)) {
$this->fieldsData[$name] = $value;
return $this;
}
throw new \Exception("Could not find field '$name'.");
}

public function hasList($name) {
return in_array($name, static::config()->lists);
}

public function getList($name) {
if ($this->hasList($name)) {
if (!isset($this->listsData[$name])) {
Expand All @@ -123,15 +145,15 @@ public function getList($name) {
}
throw new \Exception("Could not find field '$name'.");
}

public function setList($name, AbstractList $value) {
if ($this->hasList($name)) {
$this->listsData[$name] = $value;
return $this;
}
throw new \Exception("Could not find field '$name'.");
}

/**
* @param array $data
* @return $this
Expand All @@ -145,16 +167,16 @@ public function update($data) {
if ($this->hasList($name)) {
$this->setList($name, $value);
}
} elseif ($this->hasField($name)) {
} else if ($this->hasField($name)) {
$this->setField($name, $value);
}
}
}
return $this;
}

private static $_cache_field_labels = [];

protected function i18nFields() {
$fields = [];
$ancestry = array_reverse(\ClassInfo::ancestry($this->class));
Expand All @@ -177,7 +199,7 @@ protected function i18nFields() {
}
return $fields;
}

public function fieldLabels() {
$cacheKey = $this->class;
if (!isset(self::$_cache_field_labels[$cacheKey])) {
Expand All @@ -196,12 +218,12 @@ public function fieldLabels() {
}
return self::$_cache_field_labels[$cacheKey];
}

public function fieldLabel($name) {
$labels = $this->fieldLabels();
return (isset($labels[$name])) ? $labels[$name] : \FormField::name_to_label($name);
}

public function provideI18nEntities() {
$entities = [];
foreach ($this->i18nFields() as $className => $types) {
Expand All @@ -213,12 +235,12 @@ public function provideI18nEntities() {
}
return $entities;
}

public function __toString() {
return serialize($this);
return Serializer::serialize($this);
}


/**
* Process tri-state responses from permission-alterting extensions. The extensions are
* expected to return one of three values:
Expand Down Expand Up @@ -249,7 +271,7 @@ public function extendedCan($methodName, $member) {
}
return null;
}

/**
* @param \Member $member
* @return boolean
Expand All @@ -261,7 +283,7 @@ public function canView($member = null) {
}
return \Permission::check('ADMIN', 'any', $member);
}

/**
* @param \Member $member
* @return boolean
Expand All @@ -273,7 +295,7 @@ public function canEdit($member = null) {
}
return \Permission::check('ADMIN', 'any', $member);
}

/**
* @param \Member $member
* @return boolean
Expand All @@ -285,7 +307,7 @@ public function canDelete($member = null) {
}
return \Permission::check('ADMIN', 'any', $member);
}

/**
* @todo Should canCreate be a static method?
* @param \Member $member
Expand All @@ -298,14 +320,14 @@ public function canCreate($member = null) {
}
return \Permission::check('ADMIN', 'any', $member);
}

public function i18n_singular_name() {
// TODO fix class name
$class = explode('\\', $this->class);
$class = $class[count($class) - 1];
return _t("{$this->class}.SINGULARNAME", FormField::name_to_label($class));
}

public function i18n_plural_name() {
$name = $this->i18n_singular_name();
//if the penultimate character is not a vowel, replace "y" with "ies"
Expand Down
Loading

0 comments on commit d28ea63

Please sign in to comment.