Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Zauberfisch committed Nov 15, 2016
0 parents commit 77f912f
Show file tree
Hide file tree
Showing 11 changed files with 447 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# For more information about the properties used in this file,
# please see the EditorConfig documentation:
# http://editorconfig.org

[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = tab
insert_final_newline = true
trim_trailing_whitespace = true

[{*.yml,package.json}]
indent_size = 2

# The indent size used in the package.json file cannot be changed:
# https://github.com/npm/npm/pull/3180#issuecomment-16336516
3 changes: 3 additions & 0 deletions .htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<FilesMatch "\.(php|php3|php4|php5|phtml|inc)$">
Deny from all
</FilesMatch>
17 changes: 17 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Copyright (c) 2016, Zauberfisch - www.zauberfisch.at
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of SilverStripe nor the names of its contributors may be used to endorse or promote products derived from this software
without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# SilverStripe SerializedDataObject module

SilverStripe database field that allows saving arbitrary data in a single db field using serialization

## Maintainer Contact

* Zauberfisch <[email protected]>

## Requirements

* silverstripe/framework >=3.1

## Installation

* `composer require "zauberfisch/silverstripe-serialized-dataobject"`
* rebuild manifest (flush)

## Documentation

haha, right
Empty file added _config/.gitignore
Empty file.
73 changes: 73 additions & 0 deletions code/SerializedDBField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

/**
* Class SerializedDBField
*
* @author Zauberfisch
*/
abstract class SerializedDBField extends DBField {
protected $isChanged = false;

/**
* @return SerializedDataList|SerializedDataObject|null
*/
function getValue() {
if ($this->value && is_string($this->value) && $this->value[0] == "'" && $this->value[strlen($this->value) - 1] == "'") {
$this->value = substr($this->value, 1, -1);
}
if (!$this->value) {
$this->value = $this->nullValue();
} elseif (is_string($this->value)) {
$this->value = unserialize($this->value);
}
return $this->value;
}

/**
* @param SerializedDataList|SerializedDataObject|SerializedDBField|array|null|string $value
* @param null $record
* @param bool|true $markAsChanged
*/
public function setValue($value, $record = null, $markAsChanged = true) {
$this->isChanged = $this->isChanged || $markAsChanged;
if (is_a($value, __CLASS__)) {
$value = $value->getValue();
}
parent::setValue($value, $record);
}

public function isChanged() {
return $this->isChanged;
}

public function prepValueForDB($value) {
if (is_a($value, __CLASS__)) {
$value = $value->getValue();
}
if (is_a($value, 'Serializable')) {
$value = serialize($value);
}
if (is_array($value)) {
$value = serialize($value);
}
return parent::prepValueForDB($value);
}

public function requireField() {
// keep using deprecated DB::requireField() for 3.1 compatibility
/** @noinspection PhpDeprecationInspection */
DB::requireField($this->tableName, $this->name, [
'type' => 'text',
'parts' => [
'datatype' => 'mediumtext',
'character set' => 'utf8',
'collate' => 'utf8_general_ci',
'arrayValue' => $this->arrayValue,
],
]);
}

public function __toString() {
return $this->prepValueForDB($this->getValue());
}
}
12 changes: 12 additions & 0 deletions code/SerializedDBFieldHasMany.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

/**
* Class SerializedDBFieldHasMany
*
* @author Zauberfisch
*/
class SerializedDBFieldHasMany extends SerializedDBField {
public function nullValue() {
return new SerializedDataList();
}
}
9 changes: 9 additions & 0 deletions code/SerializedDBFieldHasOne.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

/**
* Class SerializedDBFieldHasOne
*
* @author Zauberfisch
*/
class SerializedDBFieldHasOne extends SerializedDBField {
}
75 changes: 75 additions & 0 deletions code/SerializedDataList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

/**
* Class SerializedDataList
*
* @author Zauberfisch
*/
class SerializedDataList extends ArrayList implements Serializable, JsonSerializable {
public function jsonSerialize() {
return [
'class' => $this->class,
'items' => $this->toArray(),
];
}

public function serialize() {
return serialize($this->toArray());
}

public function unserialize($serialized) {
$this->items = unserialize($serialized);
}

public function __toString() {
return "'" . $this->serialize() . "'";
}

public function __construct(array $items = []) {
array_walk($items, function ($item) {
if (!is_a($item, 'SerializedDataObject')) {
throw new InvalidArgumentException();
}
});
parent::__construct($items);
}

public function push($item) {
if (!is_a($item, 'SerializedDataObject')) {
throw new InvalidArgumentException();
}
parent::push($item);
}

public function add($item) {
$this->push($item);
}

public function remove($item) {
if (!is_a($item, 'SerializedDataObject')) {
throw new InvalidArgumentException();
}
parent::remove($item);
}

public function replace($item, $with) {
if (!is_a($item, 'SerializedDataObject') || !is_a($with, 'SerializedDataObject')) {
throw new InvalidArgumentException();
}
parent::replace($item, $with);
}

public function merge($item) {
if (!is_a($item, 'SerializedDataObject')) {
throw new InvalidArgumentException();
}
parent::merge($item);
}

public function unshift($item) {
if (!is_a($item, 'SerializedDataObject')) {
throw new InvalidArgumentException();
}
parent::unshift($item);
}
}
Loading

0 comments on commit 77f912f

Please sign in to comment.