<commit_before><?php
/**
 * This file is a part of the datatypes package.
 *
 * Read more at https://phpdatatypes.com/
 */

namespace DataTypes;

use DataTypes\Exceptions\EmailAddressInvalidArgumentException;
use DataTypes\Exceptions\HostnameInvalidArgumentException;
use DataTypes\Exceptions\IPAddressInvalidArgumentException;
use DataTypes\Interfaces\EmailAddressInterface;
use DataTypes\Interfaces\HostInterface;

/**
 * Class representing an email address.
 *
 * @since 1.1.0
 */
class EmailAddress implements EmailAddressInterface
{
    /**
     * Returns the host of the email address.
     *
     * @since 1.1.0
     *
     * @return HostInterface The host of the email address.
     */
    public function getHost()
    {
        return $this->myHost;
    }

    /**
     * Returns the username of the email address.
     *
     * @since 1.1.0
     *
     * @return string The username of the email address.
     */
    public function getUsername()
    {
        return $this->myUsername;
    }

    /**
     * Returns a copy of the email address instance with the specified host.
     *
     * @since 1.1.0
     *
     * @param HostInterface $host The host.
     *
     * @return EmailAddressInterface The email address instance.
     */
    public function withHost(HostInterface $host)
    {
        return new self($this->myUsername, $host);
    }

    /**
     * Returns a copy of the email address instance with the specified username.
     *
     * @since 1.1.0
     *
     * @param string $username The username.
     *
     * @throws \InvalidArgumentException            If the $username parameter is not a string.
     * @throws EmailAddressInvalidArgumentException If the $username parameter is not a valid username.
     *
     * @return EmailAddressInterface The email address instance.
     */
    public function withUsername($username)
    {
        if (!is_string($username)) {
            throw new \InvalidArgumentException('$username parameter is not a string.');
        }

        if (!self::myValidateUsername($username, $error)) {
            throw new EmailAddressInvalidArgumentException($error);
        }

        return new self($username, $this->myHost);
    }

    /**
     * Returns the email address as a string.
     *
     * @since 1.1.0
     *
     * @return string The email address as a string.
     */
    public function __toString()
    {
        return $this->myUsername . '@' . ($this->myHost->getIPAddress() !== null ? '[' . $this->myHost->__toString() . ']' : $this->myHost->__toString());
    }

    /**
     * Creates an email address from parts.
     *
     * @since 1.1.0
     *
     * @param string        $username The username.
     * @param HostInterface $host     The host.
     *
     * @throws \InvalidArgumentException            If the $username parameter is not a string.
     * @throws EmailAddressInvalidArgumentException If the $username parameter is not a valid username.
     *
     * @return EmailAddress The email address.
     */
    public static function fromParts($username, HostInterface $host)
    {
        if (!is_string($username)) {
            throw new \InvalidArgumentException('$username parameter is not a string.');
        }

        if (!self::myValidateUsername($username, $error)) {
            throw new EmailAddressInvalidArgumentException($error);
        }

        return new self($username, $host);
    }

    /**
     * Checks if an email address is valid.
     *
     * @since 1.1.0<commit_msg>Change return type in fromParts method in EmailAddress<commit_after><?php
/**
 * This file is a part of the datatypes package.
 *
 * Read more at https://phpdatatypes.com/
 */

namespace DataTypes;

use DataTypes\Exceptions\EmailAddressInvalidArgumentException;
use DataTypes\Exceptions\HostnameInvalidArgumentException;
use DataTypes\Exceptions\IPAddressInvalidArgumentException;
use DataTypes\Interfaces\EmailAddressInterface;
use DataTypes\Interfaces\HostInterface;

/**
 * Class representing an email address.
 *
 * @since 1.1.0
 */
class EmailAddress implements EmailAddressInterface
{
    /**
     * Returns the host of the email address.
     *
     * @since 1.1.0
     *
     * @return HostInterface The host of the email address.
     */
    public function getHost()
    {
        return $this->myHost;
    }

    /**
     * Returns the username of the email address.
     *
     * @since 1.1.0
     *
     * @return string The username of the email address.
     */
    public function getUsername()
    {
        return $this->myUsername;
    }

    /**
     * Returns a copy of the email address instance with the specified host.
     *
     * @since 1.1.0
     *
     * @param HostInterface $host The host.
     *
     * @return EmailAddressInterface The email address instance.
     */
    public function withHost(HostInterface $host)
    {
        return new self($this->myUsername, $host);
    }

    /**
     * Returns a copy of the email address instance with the specified username.
     *
     * @since 1.1.0
     *
     * @param string $username The username.
     *
     * @throws \InvalidArgumentException            If the $username parameter is not a string.
     * @throws EmailAddressInvalidArgumentException If the $username parameter is not a valid username.
     *
     * @return EmailAddressInterface The email address instance.
     */
    public function withUsername($username)
    {
        if (!is_string($username)) {
            throw new \InvalidArgumentException('$username parameter is not a string.');
        }

        if (!self::myValidateUsername($username, $error)) {
            throw new EmailAddressInvalidArgumentException($error);
        }

        return new self($username, $this->myHost);
    }

    /**
     * Returns the email address as a string.
     *
     * @since 1.1.0
     *
     * @return string The email address as a string.
     */
    public function __toString()
    {
        return $this->myUsername . '@' . ($this->myHost->getIPAddress() !== null ? '[' . $this->myHost->__toString() . ']' : $this->myHost->__toString());
    }

    /**
     * Creates an email address from parts.
     *
     * @since 1.1.0
     *
     * @param string        $username The username.
     * @param HostInterface $host     The host.
     *
     * @throws \InvalidArgumentException            If the $username parameter is not a string.
     * @throws EmailAddressInvalidArgumentException If the $username parameter is not a valid username.
     *
     * @return EmailAddressInterface The email address.
     */
    public static function fromParts($username, HostInterface $host)
    {
        if (!is_string($username)) {
            throw new \InvalidArgumentException('$username parameter is not a string.');
        }

        if (!self::myValidateUsername($username, $error)) {
            throw new EmailAddressInvalidArgumentException($error);
        }

        return new self($username, $host);
    }

    /**
     * Checks if an email address is valid.
     *
     * @since 1.1.0
{'fraction_non_alphanumeric': 0.10249858035207268, 'fraction_numerical': 0.0068143100511073255, 'mean_word_length': 2.335700757575758, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2057045', 'n_tokens_mistral': 1909, 'n_tokens_neox': 1862, 'n_words': 582}