Skip to content

Commit

Permalink
move fieldtype info to a trait and share between entities
Browse files Browse the repository at this point in the history
  • Loading branch information
rossriley committed Sep 3, 2017
1 parent f6c9301 commit 0d79537
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 37 deletions.
36 changes: 3 additions & 33 deletions src/Legacy/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
class Content implements \ArrayAccess
{
use Entity\ContentTypeTrait;
use Entity\ContentRelationTrait;
use Entity\ContentRouteTrait;
use Entity\ContentSearchTrait;
Expand Down Expand Up @@ -143,8 +144,8 @@ public function getDecodedValue($name)
$value = null;

if (isset($this->values[$name])) {
$fieldtype = $this->fieldtype($name);
$fieldinfo = $this->fieldinfo($name);
$fieldtype = $this->fieldType($name);
$fieldinfo = $this->fieldInfo($name);
$allowtwig = !empty($fieldinfo['allowtwig']);

switch ($fieldtype) {
Expand Down Expand Up @@ -325,37 +326,6 @@ public function next($field = 'datepublish', $where = [])
return $next;
}

/**
* Get field information for the given field.
*
* @param string $key
*
* @return array An associative array containing at least the key 'type',
* and, depending on the type, other keys.
*/
public function fieldinfo($key)
{
if (isset($this->contenttype['fields'][$key])) {
return $this->contenttype['fields'][$key];
} else {
return ['type' => ''];
}
}

/**
* Get the fieldtype for a given fieldname.
*
* @param string $key
*
* @return string
*/
public function fieldtype($key)
{
$field = $this->fieldinfo($key);

return $field['type'];
}

/**
* ArrayAccess support.
*
Expand Down
1 change: 1 addition & 0 deletions src/Storage/Entity/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
class Content extends Entity
{
use ContentRouteTrait;
use ContentTypeTrait;
use ContentTypeTitleTrait;

/** @var string|Mapping\ContentType */
Expand Down
45 changes: 45 additions & 0 deletions src/Storage/Entity/ContentTypeTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Bolt\Storage\Entity;

/**
* Trait class for ContentType definitions.
*
* These traits should be considered transitional, the functionality in the
* process of refactor, and not representative of a valid approach.
*
* @author Ross Riley <riley.ross@gmail.com>
*/
trait ContentTypeTrait
{
/**
* Get field information for the given field.
*
* @param string $key
*
* @return array An associative array containing at least the key 'type',
* and, depending on the type, other keys.
*/
public function fieldInfo($key)
{
if (isset($this->contenttype['fields'][$key])) {
return $this->contenttype['fields'][$key];
} else {
return ['type' => ''];
}
}

/**
* Get the field type for a given field name.
*
* @param string $key
*
* @return string
*/
public function fieldType($key)
{
$field = $this->fieldInfo($key);

return $field['type'];
}
}
8 changes: 4 additions & 4 deletions src/Storage/Entity/ContentValuesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ public function setValues(array $values)
];
// Check if the values need to be unserialized, and pre-processed.
foreach ($this->values as $key => $value) {
if ((in_array($this->fieldtype($key), $serializedFieldTypes)) || ($key === 'templatefields')) {
if ((in_array($this->fieldType($key), $serializedFieldTypes)) || ($key === 'templatefields')) {
if (!empty($value) && is_string($value) && (substr($value, 0, 2) === 'a:' || $value[0] === '[' || $value[0] === '{')) {
try {
$unserdata = Lib::smartUnserialize($value);
Expand All @@ -390,7 +390,7 @@ public function setValues(array $values)
}
}

if ($this->fieldtype($key) === 'video' && is_array($this->values[$key]) && !empty($this->values[$key]['url'])) {
if ($this->fieldType($key) === 'video' && is_array($this->values[$key]) && !empty($this->values[$key]['url'])) {
$defaultValues = [
'html' => '',
'responsive' => '',
Expand Down Expand Up @@ -427,7 +427,7 @@ public function setValues(array $values)
$this->values[$key] = $video;
}

if ($this->fieldtype($key) === 'repeater' && is_array($this->values[$key]) && !$this->isRootType) {
if ($this->fieldType($key) === 'repeater' && is_array($this->values[$key]) && !$this->isRootType) {
$originalMapping = null;
$originalMapping[$key]['fields'] = $this->contenttype['fields'][$key]['fields'];
$originalMapping[$key]['type'] = 'repeater';
Expand All @@ -443,7 +443,7 @@ public function setValues(array $values)
$this->values[$key] = $repeater;
}

if ($this->fieldtype($key) === 'date' || $this->fieldtype($key) === 'datetime') {
if ($this->fieldType($key) === 'date' || $this->fieldType($key) === 'datetime') {
if ($this->values[$key] === '') {
$this->values[$key] = null;
}
Expand Down

0 comments on commit 0d79537

Please sign in to comment.