<?php

namespace App\Helpers;

class ValidationHelper
{
    public function directoryExists($attribute, $value, $parameters, $validator)
    {
        return file_exists($value) && is_dir($value);
    }

    public function isDirectoryEmpty($attribute, $value, $parameters, $validator)
    {
        if (!$this->directoryExists($attribute, $value, $parameters, $validator))
        {
            return false;
        }

        $iterator = new \DirectoryIterator($value);
        $count = 0;

        foreach ($iterator as $item)
        {
            if ($item->isDot())
            {
                continue;
            }

            $count++;
        }

        return ($count == 0);
    }

    public function isPathWriteable($attribute, $value, $parameters, $validator)
    {
        return $this->directoryExists($attribute, $value, $parameters, $validator) && is_writeable($value);
    }
}