app/Plugin/TabaCustomFields2/Types/TextArrayType.php line 47

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the TabaCustomFields2 plugin
  4.  *
  5.  * Copyright (C) SPREAD WORKS Inc. All Rights Reserved.
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Plugin\TabaCustomFields2\Types;
  11. use Doctrine\DBAL\Platforms\AbstractPlatform;
  12. use Doctrine\DBAL\Types\TextType;
  13. /**
  14.  * カスタムフィールドで保存するType
  15.  */
  16. class TextArrayType extends TextType
  17. {
  18.     const TEXT_ARRAY 'text_array'// modify to match your type name
  19.     /**
  20.      * {@inheritdoc}
  21.      */
  22.     public function getSQLDeclaration(array $fieldDeclarationAbstractPlatform $platform)
  23.     {
  24.         return $platform->getClobTypeDeclarationSQL($fieldDeclaration);
  25.     }
  26.     /**
  27.      *
  28.      */
  29.     public function convertToDatabaseValue($valueAbstractPlatform $platform)
  30.     {
  31.         if (is_array($value)) {
  32.             return serialize($value);
  33.         } else {
  34.             return $value;
  35.         }
  36.     }
  37.     /**
  38.      * シリアライズされているかを確認し、TEXTとARRAYに分けて返却する
  39.      */
  40.     public function convertToPHPValue($valueAbstractPlatform $platform)
  41.     {
  42.         $value = (is_resource($value)) ? stream_get_contents($value) : $value;
  43.         if ($value === null) {
  44.             return null;
  45.         }
  46.         $val = @unserialize($value);
  47.         if ($val === false && $value != 'b:0;') {
  48.             // TEXT
  49.             return $value;
  50.         }
  51.         // ARRAY
  52.         return $val;
  53.     }
  54.     /**
  55.      * {@inheritdoc}
  56.      */
  57.     public function getName()
  58.     {
  59.         return self::TEXT_ARRAY;
  60.     }
  61. }