Source of file Error.php

Size: 2,246 Bytes - Last Modified: 2015-12-22T09:12:14-05:00

../src/Error.php

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
Covered by 4 test(s):
  • MvcLite\ErrorTest::testHandle with data set #0
  • MvcLite\ErrorTest::testHandle with data set #1
  • MvcLite\ErrorTest::testHandle with data set #2
  • MvcLite\ErrorTest::testHandle with data set #3
484950
Covered by 4 test(s):
  • MvcLite\ErrorTest::testHandle with data set #0
  • MvcLite\ErrorTest::testHandle with data set #1
  • MvcLite\ErrorTest::testHandle with data set #2
  • MvcLite\ErrorTest::testHandle with data set #3
51
Covered by 4 test(s):
  • MvcLite\ErrorTest::testHandle with data set #0
  • MvcLite\ErrorTest::testHandle with data set #1
  • MvcLite\ErrorTest::testHandle with data set #2
  • MvcLite\ErrorTest::testHandle with data set #3
52
Covered by 4 test(s):
  • MvcLite\ErrorTest::testHandle with data set #0
  • MvcLite\ErrorTest::testHandle with data set #1
  • MvcLite\ErrorTest::testHandle with data set #2
  • MvcLite\ErrorTest::testHandle with data set #3
53
Covered by 4 test(s):
  • MvcLite\ErrorTest::testHandle with data set #0
  • MvcLite\ErrorTest::testHandle with data set #1
  • MvcLite\ErrorTest::testHandle with data set #2
  • MvcLite\ErrorTest::testHandle with data set #3
54
Covered by 4 test(s):
  • MvcLite\ErrorTest::testHandle with data set #0
  • MvcLite\ErrorTest::testHandle with data set #1
  • MvcLite\ErrorTest::testHandle with data set #2
  • MvcLite\ErrorTest::testHandle with data set #3
55
Covered by 4 test(s):
  • MvcLite\ErrorTest::testHandle with data set #0
  • MvcLite\ErrorTest::testHandle with data set #1
  • MvcLite\ErrorTest::testHandle with data set #2
  • MvcLite\ErrorTest::testHandle with data set #3
56
Covered by 4 test(s):
  • MvcLite\ErrorTest::testHandle with data set #0
  • MvcLite\ErrorTest::testHandle with data set #1
  • MvcLite\ErrorTest::testHandle with data set #2
  • MvcLite\ErrorTest::testHandle with data set #3
57585960
Covered by 4 test(s):
  • MvcLite\ErrorTest::testHandle with data set #0
  • MvcLite\ErrorTest::testHandle with data set #1
  • MvcLite\ErrorTest::testHandle with data set #2
  • MvcLite\ErrorTest::testHandle with data set #3
61
Covered by 4 test(s):
  • MvcLite\ErrorTest::testHandle with data set #0
  • MvcLite\ErrorTest::testHandle with data set #1
  • MvcLite\ErrorTest::testHandle with data set #2
  • MvcLite\ErrorTest::testHandle with data set #3
62
Covered by 4 test(s):
  • MvcLite\ErrorTest::testHandle with data set #0
  • MvcLite\ErrorTest::testHandle with data set #1
  • MvcLite\ErrorTest::testHandle with data set #2
  • MvcLite\ErrorTest::testHandle with data set #3
63
Covered by 4 test(s):
  • MvcLite\ErrorTest::testHandle with data set #0
  • MvcLite\ErrorTest::testHandle with data set #1
  • MvcLite\ErrorTest::testHandle with data set #2
  • MvcLite\ErrorTest::testHandle with data set #3
64
Covered by 4 test(s):
  • MvcLite\ErrorTest::testHandle with data set #0
  • MvcLite\ErrorTest::testHandle with data set #1
  • MvcLite\ErrorTest::testHandle with data set #2
  • MvcLite\ErrorTest::testHandle with data set #3
6566
Covered by 3 test(s):
  • MvcLite\ErrorTest::testHandle with data set #0
  • MvcLite\ErrorTest::testHandle with data set #1
  • MvcLite\ErrorTest::testHandle with data set #2
676869
Covered by 1 test(s):
  • MvcLite\ErrorTest::testHandle with data set #3
70
Covered by 1 test(s):
  • MvcLite\ErrorTest::testHandle with data set #3
71
Covered by 1 test(s):
  • MvcLite\ErrorTest::testHandle with data set #3
72737475767778798081828384
Covered by 4 test(s):
  • MvcLite\ErrorTest::testHandle with data set #0
  • MvcLite\ErrorTest::testHandle with data set #1
  • MvcLite\ErrorTest::testHandle with data set #2
  • MvcLite\ErrorTest::testHandle with data set #3
85
Covered by 4 test(s):
  • MvcLite\ErrorTest::testHandle with data set #0
  • MvcLite\ErrorTest::testHandle with data set #1
  • MvcLite\ErrorTest::testHandle with data set #2
  • MvcLite\ErrorTest::testHandle with data set #3
86878889909192939495
Covered by 1 test(s):
  • MvcLite\ErrorTest::testGetErrors with data set #0
969798
<?php
/**
 * Class to handle errors throughout the site
 *
 * @category    PHP
 * @package     MvcLite
 * @subpackage  Error
 * @since       File available since release 1.2.x
 * @author      Cory Collier <corycollier@corycollier.com>
 */

namespace MvcLite;

use MvcLite\Traits\Singleton as SingletonTrait;

/**
 * Class to handle errors throughout the site
 *
 * @category    PHP
 * @package     MvcLite
 * @subpackage  Error
 * @since       File available since release 1.2.x
 * @author      Cory Collier <corycollier@corycollier.com>
 */

class Error extends ObjectAbstract
{
    use SingletonTrait;

    /**
     * Holder for all the errors that have occured for the request.
     *
     * @var array $errors
     */
    protected $errors = [];

    /**
     * handler for errors
     */
    public static function handle(
        $errno,
        $errstr,
        $errfile = null,
        $errline = null,
        $errcontext = []
    ) {
        $self = get_called_class();

        // append the errors to the list of errors that have occured so far
        $self::getInstance()->addError([
            'errno'         => $errno,
            'errstr'        => $errstr,
            'errfile'       => $errfile,
            'errline'       => $errline,
            'errcontext'    => $errcontext,
        ]);

        // switch, based on the error number given
        switch ($errno) {
            case E_ERROR:
            case E_WARNING:
            case E_PARSE:
            case E_CORE_WARNING:
            case E_USER_ERROR:
                // figure out something appropriate to do
                throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);

            // the default stuff
            default:
                return;
        }

    }

    /**
     * Adds errors to the instance's error property.
     *
     * @param array $error An array representing an error.
     *
     * @return MvcLite\Error $this for object-chaining.
     */
    protected function addError($error = [])
    {
        $this->errors[] = $error;
        return $this;
    }

    /**
     * Getter for the errors property.
     *
     * @return array
     */
    public function getErrors()
    {
        return $this->errors;
    }
}