<?php
/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */

namespace Kufer\Web\Classes\Pdf\Helper;


abstract class InnerBorderContainer extends Container
{
  
  protected $widths = array();

  
  protected $innerBorders = false;

  
  public function setWidths($widths)
  {
    $this->widths = $widths;
  }

  public function getWidths(): array
  {
    return $this->widths;
  }

  public function setInnerBorder(int $i, $allowed)
  {
    $this->innerBorders = $this->getInnerBorders();
    $this->innerBorders &= 0xFFFF ^ (($allowed ? 1 : 0) << $i);
  }

  
  public function setInnerBorders($borders): void
  {
    $this->innerBorders = $borders;
  }

  public function getInnerBorders()
  {
    if ($this->innerBorders === false) {
      return 0;
    }

    if ($this->innerBorders === true) {
      $result = 0;
      foreach ($this->children as $idx => $child) {
        $result |= 1 << $idx;
      }

      return $result;
    }

    return $this->innerBorders;
  }

  public function hasInnerBorder(int $i): bool
  {
    if (\is_bool($this->innerBorders)) {
      return $this->innerBorders;
    }

    $val = 1 << $i;

    return ($this->innerBorders & $val) === $val;
  }
}