<?php

/*
 * This file is part of the AuthStack project.
 *
 * (c) Buckhill Ltd <development@buckhill.co.uk>
 *
 * This source file is subject to the Buckhill license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Buckhill\LDAP;

use Buckhill\LDAP\Errors\BadEntryDataError;

class Entry
{
    protected $name;
    protected $value;
    protected $count;


    /**
     * @param       $name
     * @param array $data
     *
     * @return static
     * @throws BadEntryDataError
     */
    public static function fromRecord($name, array $data)
    {
        if(!isset($data['count']))
        {
            throw new BadEntryDataError("Invalid data supplied. Required key 'count' is not present.");
        }

        $me = new static;

        $me->name = $name;
        $me->count = (int)$data['count'];

        unset($data['count']);

        if($me->count === 1)
        {
            $me->value = $data[0];
        }
        else
        {
            $me->value = $data;
        }

        return $me;
    }


    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }


    /**
     * @return array|string|int|float
     */
    public function getValue()
    {
        return $this->value;
    }


    /**
     * @return int
     */
    public function count()
    {
        return $this->count;
    }
}