src/Entity/Gos/Uniqskills/Landing/LandingModuleJson.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Gos\Uniqskills\Landing;
  3. use Doctrine\ORM\Mapping as ORM;
  4. /**
  5.  * @ORM\Entity()
  6.  */
  7. class LandingModuleJson extends LandingModuleItem
  8. {
  9.     private function validateJson($string)
  10.     {
  11.         json_decode($string);
  12.         return (json_last_error() == JSON_ERROR_NONE);
  13.     }
  14.     public function getVariableValueDecoded()
  15.     {
  16.         return $this->sortByPosition();
  17.     }
  18.     private function isArrayAssociative(array $array)
  19.     {
  20.         if (array() === $array) return false;
  21.         return array_keys($array) !== range(0count($array) - 1);
  22.     }
  23.     private static function sortExecute($a$b)
  24.     {
  25.         if (!isset($a['position']) || !isset($b['position'])) return 0;
  26.         if ($a['position'] == $b['position']) return 0;
  27.         return ($a['position'] < $b['position']) ? -1;
  28.     }
  29.     private function sortByPosition()
  30.     {
  31.         //sort by position if isset
  32.         $decoded json_decode($this->variableValuetrue);
  33.         //handle associative and normal arrays
  34.         $this->isArrayAssociative($decoded) ? uasort($decoded, array($this'sortExecute'))
  35.                                             : usort($decoded, array($this'sortExecute'));
  36.         return $decoded;
  37.     }
  38.     /* ****************************** GETTERS & SETTERS ******************************  */
  39.     /**
  40.      * @param string $variableValue
  41.      *
  42.      * @return LandingModuleJson
  43.      * @throws \Exception
  44.      */
  45.     public function setVariableValue($variableValue)
  46.     {
  47.         if(!$variableValue) {
  48.             $this->variableValue json_encode([]);
  49.             return $this;
  50.         }
  51.         if ($this->validateJson($variableValue))
  52.         {
  53.             $this->variableValue $variableValue;
  54.             return $this;
  55.         }
  56.         throw new \Exception('Invalid json value');
  57.     }
  58.     /**
  59.      * @return string|null
  60.      */
  61.     public function getVariableValue()
  62.     {
  63.         return json_encode($this->sortByPosition());
  64.     }
  65.     /**
  66.      * @return string
  67.      */
  68.     public function getVariableType()
  69.     {
  70.         return $this::VARIABLE_TYPE_JSON;
  71.     }
  72. }