<?php
/**
 * // Examples for each type of value:
 *
 * $intPrintable = new preference(42);
 * echo $intPrintable; // Outputs: 42
 *
 * $stringPrintable = new preference('Hello, World!');
 * echo $stringPrintable; // Outputs: 'Hello, World!'
 *
 * $floatPrintable = new preference(3.14);
 * echo $floatPrintable; // Outputs: 3.14
 *
 * $boolPrintable = new preference(true);
 * echo $boolPrintable; // Outputs: true
 *
 * $arrayPrintable = new preference([1, 2, 3]);
 * echo $arrayPrintable; // Outputs: [1, 2, 3]
 *
 * $objectPrintable = new preference(new stdClass());
 * echo $objectPrintable; // Outputs: {}
 *
 * $nullPrintable = new preference(null);
 * echo $nullPrintable; // Outputs: NULL
 *
 * // Example usage as an array:
 * $array = $arrayPrintable->getArrayCopy();
 * print_r($array);
 */

namespace CMSMSExt\ModuleFunctions;


use ArrayObject;
use CMSMSExt\core\morphable;

class preference extends morphable
{
  public function __construct($value)
  {
    if(\is_scalar($value) || NULL === $value)
    {
      parent::__construct([$value], ArrayObject::ARRAY_AS_PROPS | ArrayObject::STD_PROP_LIST);
    }
    else if(\is_array($value) || (\is_object($value) && $value instanceof \stdClass))
    {
      parent::__construct($value, ArrayObject::ARRAY_AS_PROPS | ArrayObject::STD_PROP_LIST);
    }
    else
    {
      parent::__construct(['Unsupported type'], ArrayObject::ARRAY_AS_PROPS | ArrayObject::STD_PROP_LIST);
    }
  }
  
  public function __toString()
  {
    if( 1 === $this->count() )
    {
      return (string)\var_export($this[0], TRUE);
    }
    
    if($this->count() > 1)
    {
      return (string)\json_encode($this->getArrayCopy(), \JSON_PRETTY_PRINT);
    }
    
    return 'Unsupported type';
  }
}
?>

