1<?php 2 3 4namespace ComboStrap; 5 6 7class Json 8{ 9 10 const TYPE_OBJECT = "{"; 11 const PARENT_TYPE_ARRAY = "["; 12 const TAB_SPACES_COUNTER = 4; 13 const EXTENSION = "json"; 14 15 /** 16 * @var array 17 */ 18 private $jsonArray; 19 20 /** 21 * Json constructor. 22 * @param array|null $jsonValue 23 */ 24 public function __construct(?array $jsonValue = null) 25 { 26 $this->jsonArray = $jsonValue; 27 28 } 29 30 public static function createEmpty(): Json 31 { 32 return new Json([]); 33 } 34 35 public static function createFromArray(array $actual): Json 36 { 37 return new Json($actual); 38 } 39 40 public static function getValidationLink(string $json): string 41 { 42 return "See the errors it by clicking on <a href=\"https://jsonformatter.curiousconcept.com/?data=" . urlencode($json) . "\">this link</a>"; 43 } 44 45 /** 46 * @throws ExceptionBadSyntax 47 * @throws ExceptionNotFound 48 */ 49 public static function createFromPath(Path $path): Json 50 { 51 52 53 $content = FileSystems::getContent($path); 54 return self::createFromString($content); 55 56 } 57 58 /** 59 * Used to make diff 60 * @return false|string 61 */ 62 public function toNormalizedJsonString() 63 { 64 $jsonArray = $this->getJsonArray(); 65 if ($jsonArray === null) { 66 /** 67 * Edge case empty string 68 */ 69 return ""; 70 } 71 return json_encode($jsonArray, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); 72 } 73 74 /** 75 * This formatting make the object on one line for a list of object 76 * making the frontmatter compacter (one line, one meta) 77 * @return string 78 * @deprecated You should use the {@link MetadataFrontmatterStore::toFrontmatterJsonString()} instead 79 */ 80 public function toFrontMatterFormat(): string 81 { 82 83 $jsonArray = $this->getJsonArray(); 84 return MetadataFrontmatterStore::toFrontmatterJsonString($jsonArray); 85 86 } 87 88 89 /** 90 * @throws ExceptionBadSyntax 91 */ 92 public 93 static function createFromString($jsonString): Json 94 { 95 if ($jsonString === null || $jsonString === "") { 96 return new Json(); 97 } 98 $jsonArray = json_decode($jsonString, true); 99 if ($jsonArray === null) { 100 throw new ExceptionBadSyntax("The string is not a valid json. Value: ($jsonString)"); 101 } 102 return new Json($jsonArray); 103 } 104 105 /** 106 * @return mixed 107 */ 108 public 109 function toJsonObject() 110 { 111 $jsonString = $this->getJsonString(); 112 return json_decode($jsonString); 113 114 } 115 116 public 117 function toArray(): ?array 118 { 119 return $this->getJsonArray(); 120 121 } 122 123 public 124 function toPrettyJsonString() 125 { 126 return $this->toNormalizedJsonString(); 127 } 128 129 private 130 function getJsonString() 131 { 132 return json_encode($this->jsonArray, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); 133 } 134 135 /** 136 * @return array|null 137 */ 138 private 139 function getJsonArray(): ?array 140 { 141 return $this->jsonArray; 142 } 143 144 public 145 function toMinifiedJsonString() 146 { 147 return json_encode($this->jsonArray); 148 } 149 150 151} 152