| Total Complexity | 35 |
| Total Lines | 152 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 15 | final class Loader |
||
| 16 | { |
||
| 17 | //public |
||
| 18 | |||
| 19 | /* @var null|string */ |
||
| 20 | public $error; |
||
| 21 | |||
| 22 | public const EXCLUDE_DIRECTIVES = 1;//DONT include_directive |
||
| 23 | public const IGNORE_COMMENTS = 2;//DONT include_comments |
||
| 24 | public const NO_PARSING_EXCEPTIONS = 4;//THROW Exception on parsing Errors |
||
| 25 | public const NO_OBJECT_FOR_DATE = 8;//DONT import date strings as dateTime Object |
||
| 26 | //privates |
||
| 27 | /* @var null|string */ |
||
| 28 | private $content; |
||
| 29 | /* @var null|string */ |
||
| 30 | private $filePath; |
||
| 31 | /* @var integer */ |
||
| 32 | private $debug = 0;///TODO: determine levels |
||
| 33 | /* @var integer */ |
||
| 34 | private $options = 0; |
||
| 35 | //Exceptions messages |
||
| 36 | private const INVALID_VALUE = self::class.": at line %d"; |
||
| 37 | private const EXCEPTION_NO_FILE = self::class.": file '%s' does not exists (or path is incorrect?)"; |
||
| 38 | private const EXCEPTION_READ_ERROR = self::class.": file '%s' failed to be loaded (permission denied ?)"; |
||
| 39 | private const EXCEPTION_LINE_SPLIT = self::class.": content is not a string(maybe a file error?)"; |
||
| 40 | |||
| 41 | public function __construct($absolutePath = null, $options = null, $debug = 0) |
||
| 42 | { |
||
| 43 | $this->debug = is_int($debug) ? min($debug, 3) : 1; |
||
| 44 | $this->options = is_int($options) ? $options : $this->options; |
||
| 45 | if (is_string($absolutePath)) { |
||
| 46 | $this->load($absolutePath); |
||
| 47 | } |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * load a file and save its content as $content |
||
| 52 | * |
||
| 53 | * @param string $absolutePath The absolute path of a file |
||
|
3 ignored issues
–
show
|
|||
| 54 | * |
||
| 55 | * @throws \Exception if file don't exist OR reading failed |
||
|
1 ignored issue
–
show
|
|||
| 56 | * |
||
| 57 | * @return self ( returns the same Loader ) |
||
|
1 ignored issue
–
show
|
|||
| 58 | */ |
||
| 59 | public function load(string $absolutePath):Loader |
||
| 60 | { |
||
| 61 | $this->filePath = $absolutePath; |
||
| 62 | if (!file_exists($absolutePath)) { |
||
| 63 | throw new \Exception(sprintf(self::EXCEPTION_NO_FILE, $absolutePath)); |
||
| 64 | } |
||
| 65 | $adle = "auto_detect_line_endings"; |
||
| 66 | $prevADLE = ini_get($adle); |
||
| 67 | !$prevADLE && ini_set($adle, "true"); |
||
| 68 | $content = file($absolutePath, FILE_IGNORE_NEW_LINES); |
||
| 69 | !$prevADLE && ini_set($adle, "false"); |
||
| 70 | if (is_bool($content)) { |
||
| 71 | throw new \Exception(sprintf(self::EXCEPTION_READ_ERROR, $absolutePath)); |
||
| 72 | } |
||
| 73 | $this->content = $content; |
||
| 74 | return $this; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Parse Yaml lines into a hierarchy of Node |
||
| 79 | * |
||
| 80 | * @param string $strContent The Yaml string or null to parse loaded content |
||
|
3 ignored issues
–
show
|
|||
| 81 | * @throws \Exception if content is not available as $strContent or as $this->content (from file) |
||
|
1 ignored issue
–
show
|
|||
| 82 | * @throws \ParseError if any error during parsing or building |
||
|
1 ignored issue
–
show
|
|||
| 83 | * |
||
| 84 | * @return array|YamlObject|null null on errors if NO_PARSING_EXCEPTIONS is set, otherwise an array of YamlObject or just YamlObject |
||
|
1 ignored issue
–
show
|
|||
| 85 | */ |
||
| 86 | public function parse($strContent = null) |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | private function onSpecialType(&$n, &$previous, &$emptyLines):bool |
||
| 145 | } |
||
| 146 | |||
| 147 | private function onContextType(&$n, &$previous, $lineString):bool |
||
| 167 | } |
||
| 168 | } |
||
| 169 |