SuiteCRM-Core/Api/V8/JsonApi/Helper/AttributeObjectHelper.php
Dillon-Brown 8e4cc94994 Squashed 'public/legacy/' content from commit 817a12dc0
git-subtree-dir: public/legacy
git-subtree-split: 817a12dc0c30c189f56d5cb1f7dc37a9631bdbe3
2021-03-31 15:37:32 +01:00

49 lines
1.2 KiB
PHP

<?php
namespace Api\V8\JsonApi\Helper;
use Api\V8\BeanDecorator\BeanManager;
use Api\V8\JsonApi\Response\AttributeResponse;
class AttributeObjectHelper
{
/**
* @var BeanManager
*/
protected $beanManager;
/**
* @param BeanManager $beanManager
*/
public function __construct(BeanManager $beanManager)
{
$this->beanManager = $beanManager;
}
/**
* @param \SugarBean $bean
* @param array|null $fields
*
* @return AttributeResponse
*/
public function getAttributes(\SugarBean $bean, $fields = null)
{
$bean->fixUpFormatting();
// using the ISO 8601 format for dates
$attributes = array_map(function ($value) {
return is_string($value)
? (\DateTime::createFromFormat('Y-m-d H:i:s', $value)
? date(\DateTime::ATOM, strtotime($value))
: $value)
: $value;
}, $bean->toArray());
if ($fields !== null) {
$attributes = array_intersect_key($attributes, array_flip($fields));
}
unset($attributes['id']);
return new AttributeResponse($attributes);
}
}