Overview

Namespaces

  • Net
    • Bazzline
      • Component
        • Cli
          • Readline
            • Configuration

Classes

  • Net\Bazzline\Component\Cli\Readline\Autocomplete
  • Net\Bazzline\Component\Cli\Readline\Configuration\Assembler
  • Net\Bazzline\Component\Cli\Readline\Configuration\Executable
  • Net\Bazzline\Component\Cli\Readline\Configuration\Validator
  • Net\Bazzline\Component\Cli\Readline\DebugManager
  • Net\Bazzline\Component\Cli\Readline\Manager
  • Net\Bazzline\Component\Cli\Readline\ManagerFactory
  • Net\Bazzline\Component\Cli\Readline\ReadLine
  • Overview
  • Namespace
  • Class
  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 
<?php
/**
 * @author stev leibelt <artodeto@bazzline.net>
 * @since 2015-07-05 
 */

namespace Net\Bazzline\Component\Cli\Readline\Configuration;

use Closure;
use Net\Bazzline\Component\GenericAgreement\Data\ValidatorInterface;
use Net\Bazzline\Component\GenericAgreement\Exception\InvalidArgument;

class Validator implements ValidatorInterface
{
    /** @var string */
    private $message;

    /** @var string */
    private $trace;

    /**
     * @param mixed $data
     * @return boolean
     */
    public function isValid($data)
    {
        $this->resetMessageAndTrace();

        try {
            $this->validate($data);
            $isValid = true;
        } catch (InvalidArgument $exception) {
            $this->message  = $exception->getMessage();
            $this->trace    = $exception->getTraceAsString();
            $isValid = false;
        }

        return $isValid;
    }

    /**
     * @return null|string
     */
    public function getMessage()
    {
        return $this->message;
    }

    /**
     * @return null|string
     */
    public function getTrace()
    {
        return $this->trace;
    }

    private function resetMessageAndTrace()
    {
        $this->message  = null;
        $this->trace    = null;
    }

    /**
     * @param array $configuration
     * @param null $path
     */
    private function validate($configuration, $path = null)
    {
        if (!is_array($configuration)) {
            throw new InvalidArgument('configuration ' . (is_null($path) ? '' : ' in path "' . $path . '" ') . 'must be an array');
        }

        if (empty($configuration)) {
            throw new InvalidArgument('configuration ' . (is_null($path) ? '' : ' in path "' . $path . '" ') . 'can not be empty');
        }

        foreach ($configuration as $index => $arrayOrCallable) {
            $currentPath = (is_null($path)) ? $index : $path . '/' . $index;

            if (is_string($arrayOrCallable)) {
                if (!is_callable($arrayOrCallable)) {
                    throw new InvalidArgument('method in path "' . $currentPath . '" must be callable');
                }
            } else if (is_array($arrayOrCallable)) {
                $object = current($arrayOrCallable);

                if (is_object($object) && $this->isNotAnClosure($object)) {
                    $methodName = $arrayOrCallable[1];
                    if (!method_exists($object, $methodName)) {
                        throw new InvalidArgument(
                            'provided instance of "' . get_class($object) . '" in path "' . $currentPath . '" does not have the method "' . $methodName . '"'
                        );
                    }
                } else {
                    $this->validate($arrayOrCallable, $currentPath);
                }
            } else {
                if ($this->isNotAnClosure($arrayOrCallable)) {
                    throw new InvalidArgument(
                        'can not handle value "' . var_export($arrayOrCallable, true) . '" in path "' . $currentPath . '"'
                    );
                }
            }
        }
    }

    /**
     * @param mixed $data
     * @return bool
     */
    private function isNotAnClosure($data)
    {
        return !($data instanceof Closure);
    }
}
PHP Cli Readline Component by bazzline.net API documentation generated by ApiGen