{@inheritDoc}

@throws InvalidArgumentException If $file is not a valid value
@throws UnexpectedValueException If $file is an array or FileBag that
contains a value that’s not a File or SplInfoObject object.
176050
public function remove($file)
    {
        if (($isArray = is_array($file)) || $file instanceof Traversable) {
            $result = true;
            foreach ($file as $key => $f) {
                try {
                    if ( ! $this->remove($f)) {
                        $result = false;
                    }
                }
                catch (InvalidArgumentException $e) {
                    throw new UnexpectedValueException(
                        sprintf(
                            "%s's value at key %s caused InvalidArgumentException",
                            $isArray ? 'Array' : 'FileBag',
                            $key
                        ),
                        null,
                        $e
                    );
                }
                catch (UnexpectedValueException $e) {
                    throw new UnexpectedValueException(
                        sprintf(
                            "%s's value at key %s caused UnexpectedValueException",
                            $isArray ? 'Array' : 'FileBag',
                            $key
                        ),
                        null,
                        $e
                    );
                }
            }
            return $result;
        }
        elseif ($file instanceof SplFileInfo
            || $file instanceof File
        ) {
            if ($this->files->contains($file)) {
                $this->files->removeElement($file);
                return true;
            }
            return false;
        }

        throw InvalidArgumentException::create(
            implode('|', array(
                'Axstrad\Component\Filesystem\FileBag',
                'SplFileInfo[]',
                'Axstrad\Component\Filesystem\File[]',
                'SplFileInfo',
                'Axstrad\Component\Filesystem\File'
            )),
            $file
        );
    }
1369899
public function validateFile($file)
    {
        if (file_exists($file) !== true) {
            throw new Exception('File not found');
        }
        if (is_readable($file) !== true) {
            throw new Exception('File not readable');
        }

        $data = file_get_contents($file);
        if ($data === false) {
            throw new Exception('Failed get file');
        }

        return $this->validateFragment($data);
    }
27007
private function parseFile($file)
    {
        $data = sscanf($file, '@%[^:]:%[^?]');

        if (empty($data[0])) {
            throw new \InvalidArgumentException(sprintf('File "%s" has invalid syntax', $file));
        }

        return $data;
    }
151400
public static function isArray($value, $message = null)
    {
        if (!is_array($value)) {
            throw new InvalidArgumentException(sprintf(
                $message ?: 'Expected array, but got %s',
                Helper::typeToString($value)
            ));
        }
    }
1112248
public static function fromFile($file, array $option = array())
    {
        if (!file_exists($file)) {
            throw new \InvalidArgumentException('File not found: ' . $file);
        }

        return new self(new FileIterator($file), $option);
    }
279443
public function isValid($value, $file = null)
    {
        if (array_key_exists($value, $this->_files)) {
            $files[$value] = $this->_files[$value];
        } else {
            foreach ($this->_files as $file => $content) {
                if (isset($content['name']) && ($content['name'] === $value)) {
                    $files[$file] = $this->_files[$file];
                }

                if (isset($content['tmp_name']) && ($content['tmp_name'] === $value)) {
                    $files[$file] = $this->_files[$file];
                }
            }
        }

        if (empty($files)) {
            return $this->_throw($file, self::FILE_NOT_FOUND);
        }

        foreach ($files as $file => $content) {
            $this->_value = $file;
            switch($content['error']) {
                case 0:
                    if (!is_uploaded_file($content['tmp_name'])) {
                        $this->_throw($file, self::ATTACK);
                    }
                    break;

                case 1:
                    $this->_throw($file, self::INI_SIZE);
                    break;

                case 2:
                    $this->_throw($file, self::FORM_SIZE);
                    break;

                case 3:
                    $this->_throw($file, self::PARTIAL);
                    break;

                case 4:
                    $this->_throw($file, self::NO_FILE);
                    break;

                case 6:
                    $this->_throw($file, self::NO_TMP_DIR);
                    break;

                case 7:
                    $this->_throw($file, self::CANT_WRITE);
                    break;

                case 8:
                    $this->_throw($file, self::EXTENSION);
                    break;

                default:
                    $this->_throw($file, self::UNKNOWN);
                    break;
            }
        }

        if (count($this->_messages) > 0) {
            return false;
        } else {
            return true;
        }
    }
1085852
public function isValid($value, $file = null)
    {
        $directories = $this->getDirectory(true);
        if (($file !== null) and (!empty($file['destination']))) {
            $directories[] = $file['destination'];
        } else if (!isset($file['name'])) {
            $file['name'] = $value;
        }

        foreach ($directories as $directory) {
            if (empty($directory)) {
                continue;
            }

            $check = true;
            if (file_exists($directory . DIRECTORY_SEPARATOR . $file['name'])) {
                $this->_throw($file, self::DOES_EXIST);
                return false; 
            }
        }

        if (!isset($check)) {
            $this->_throw($file, self::DOES_EXIST);
            return false; 
        }

        return true;
    }
372783
public static function fromValue($value): self
    {
        if ($value instanceof self) {
            return $value;
        }

        if (\is_string($value)) {
            try {
                return self::fromJson($value);
            } catch (InvalidArgumentException $e) {
                return self::fromJsonFile($value);
            }
        }

        if (\is_array($value)) {
            return self::fromArray($value);
        }

        throw new InvalidArgumentException('Invalid service account specification.');
    }
498711
public function load($file = '@config/app.php')
    {
        /** @noinspection PhpIncludeInspection */
        $data = require $this->alias->resolve($file);

        foreach ((array)$data as $field => $value) {
            if (!property_exists($this, $field)) {
                throw new NotSupportedException(['`:item` item is not allowed: it must be a public property of `configure` component', 'item' => $field]);
            }

            $this->$field = $value;
        }

        return $this;
    }
955994
public function parse(string $file): array
    {
        if (false === file_exists($file)) {
            throw InvalidArgumentExceptionFactory::createForFileCouldNotBeFound($file);
        }

        try {
            $data = json_decode(file_get_contents($file), true);

            if (null === $data) {
                throw ParseExceptionFactory::createForInvalidJson($file);
            }

            return $data;
        } catch (\Exception $exception) {
            if ($exception instanceof UnparsableFileException) {
                throw $exception;
            }

            throw ParseExceptionFactory::createForUnparsableFile($file, 0, $exception);
        }
    }
314452
public function setData($data)
    {
        if ( ! ($data instanceof SplFileInfo)) {
            throw new UnexpectedValueException('Expected SplFileInfo instance');
        }

        $this->file = $data;

        $this->setDerivedFileProperties();

        return $this;
    }
494368
protected function initialize()
    {
        parent::initialize();

        if (!$this->file->exists()) {
            return;
        }

        try {
            $packages = $this->file->read();

            if (!is_array($packages)) {
                throw new \UnexpectedValueException('Could not parse package list from the repository');
            }
        } catch (\Exception $e) {
            throw new InvalidRepositoryException('Invalid repository data in '.$this->file->getPath().', packages could not be loaded: ['.get_class($e).'] '.$e->getMessage());
        }

        $loader = new ArrayLoader(null, true);
        foreach ($packages as $packageData) {
            $package = $loader->load($packageData);
            $this->addPackage($package);
        }
    }
269250
public static void checkValidFile(final File file) {
        checkNotNull("file", file);
        if (!file.exists()) {
            throw new IllegalArgumentException("The file '" + file + "' does not exist!");
        }
        if (!file.isFile()) {
            throw new IllegalArgumentException("The name '" + file + "' is not a file!");
        }
    }
51591
protected function getFileDescriptor($element)
    {
        $fileDescriptor = $element instanceof FileDescriptor
            ? $element
            : $element->getFile();

        if (!$fileDescriptor instanceof FileDescriptor) {
            throw new \UnexpectedValueException('An element should always have a file associated with it');
        }

        return $fileDescriptor;
    }
620328
public function setValue($value)
    {
        if (!is_string($value) || (is_object($value) && !method_exists($value, "__toString"))) {
            throw new InvalidArgumentException(sprintf(
                "Invalid argument given to %s, expected a string, %s given",
                __METHOD__,
                is_object($value) ? get_class($value) : gettype($value)
            ));
        }

        $value = (string) $value;

        if (null !== $regex = $this->getValidationRegex()) {
            if (! preg_match($regex, $value)) {
                throw new InvalidArgumentException(sprintf(
                    "The given value '%s' doesn't match the validation regular expression: %s",
                    $value,
                    $regex
                ));
            }
        }

        $this->value = $value;

        return $this;
    }
1276189
public function validateValueType($value)
    {
        if ($this->hasOption(Entity\Reflection\Property\Option\Primary::KEY)
            && (Entity\Reflection\Property\Option\Primary::isEmpty($value))
        ) {
            throw new Exception\InvalidArgumentException(
                "Primary value can not be empty string or null!",
                $value
            );
        }

        if ($value === null) {
            return;
        }

        // Enumeration
        if ($this->hasOption(Entity\Reflection\Property\Option\Enum::KEY)
            && !$this->getOption(Entity\Reflection\Property\Option\Enum::KEY)->isValid($value)
        ) {
            throw new Exception\InvalidArgumentException(
                "Value " . $value . " is not from defined entity enumeration "
                . "range on property " . $this->name . "!",
                $value
            );
        }

        // Scalar or array type
        if (self::isScalarType($this->type) || $this->type === self::TYPE_ARRAY) {

            if (gettype($value) === $this->type) {
                return;
            }
            throw new Exception\InvalidArgumentException(
                "Expected " . $this->type . " but " . gettype($value)
                . " given on property " . $this->name . "!",
                $value
            );
        }

        // Object validation
        $givenType = gettype($value);
        if ($givenType === "object") {
            $givenType = get_class($value);
        }

        if ($this->type === self::TYPE_ENTITY) {
            // Entity

            $expectedEntityClass = Convention::nameToClass($this->typeOption, Convention::ENTITY_MASK);
            if ($value instanceof $expectedEntityClass) {
                return;
            } else {
                throw new Exception\InvalidArgumentException(
                    "Expected entity " . $this->typeOption . " but " . $givenType
                    . " given on property " . $this->name . "!",
                    $value
                );
            }
        } elseif ($this->type === self::TYPE_COLLECTION) {
            // Collection

            $entityClass = Convention::nameToClass($this->typeOption, Convention::ENTITY_MASK);
            if (!$value instanceof Entity\Collection) {

                throw new Exception\InvalidArgumentException(
                    "Expected entity collection but " . $givenType . " given on"
                    . " property " . $this->name . "!",
                    $value
                );
            } elseif ($value->getEntityClass() !== $entityClass) {
                throw new Exception\InvalidArgumentException(
                    "Expected collection of entity " . $entityClass
                    . " but collection of entity " . $value->getEntityClass()
                    . " given on property " . $this->name . "!",
                    $value
                );
            } else {
                return;
            }
        } elseif ($this->type === self::TYPE_DATETIME) {
            // DateTime

            if ($value instanceof \DateTime) {
                return;
            } else {
                throw new Exception\InvalidArgumentException(
                    "Expected DateTime but " . $givenType . " given on"
                    . " property " . $this->name . "!",
                    $value
                );
            }
        } elseif ($this->type === self::TYPE_DATE) {
            // Date

            if ($value instanceof \DateTime) {
                return;
            } else {
                throw new Exception\InvalidArgumentException(
                    "Expected date as DateTime object but " . $givenType
                    . " given on property " . $this->name . "!",
                    $value
                );
            }
        } else {
            // Unexpected

            throw new \Exception("Unsupported type " . $this->type . "!");
        }
    }
362461
public function load($file, $type = null)
    {
        $path = $this->locator->locate($file);

        if (!stream_is_local($path)) {
            throw new \InvalidArgumentException(sprintf('This is not a local file "%s".', $path));
        }

        if (!file_exists($path)) {
            throw new \InvalidArgumentException(sprintf('File "%s" not found.', $path));
        }

        if (null === $this->yamlParser) {
            $this->yamlParser = new YamlParser;
        }

        try {
            $parsedConfig = $this->yamlParser->parse(file_get_contents($path));
        } catch (ParseException $e) {
            throw new \InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $path), 0, $e);
        }

        $collection = [];

        // empty file
        if (null === $parsedConfig) {
            return $collection;
        }

        // not an array
        if (!is_array($parsedConfig)) {
            throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $path));
        }

        foreach ($parsedConfig as $name => $config) {
            $this->validate($config, $name, $path);

            if (isset($config['resource'])) {
                $this->parseImport($collection, $config, $path, $file);
            } else {
                $this->parseMetadata($collection, $name, $config);
            }
        }

        return $collection;
    }