Apple_Exporter\Builders\Component_Layouts::set_anchor_layout_for PHP Method

set_anchor_layout_for() public method

Sets the required layout for a component to anchor another component or be anchored.
public set_anchor_layout_for ( Component $component )
$component Apple_Exporter\Components\Component
    public function set_anchor_layout_for($component)
    {
        // Are we anchoring left or right?
        $position = null;
        switch ($component->get_anchor_position()) {
            case Component::ANCHOR_NONE:
                return;
            case Component::ANCHOR_LEFT:
                $position = 'left';
                break;
            case Component::ANCHOR_RIGHT:
                $position = 'right';
                break;
            case Component::ANCHOR_AUTO:
                // The alignment position is the opposite of the body_orientation
                // setting. In the case of centered body orientation, use left alignment.
                // This behaviour was chosen by design.
                if ('left' == $this->get_setting('body_orientation')) {
                    $position = 'right';
                } else {
                    $position = 'left';
                }
                break;
        }
        $layout_name = "anchor-layout-{$position}";
        if (!$this->layout_exists($layout_name)) {
            // Cache settings
            $alignment_offset = $this->get_setting('alignment_offset');
            $body_column_span = $this->get_setting('body_column_span');
            $layout_columns = $this->get_setting('layout_columns');
            // Find out the starting column. This is easy enough if we are anchoring
            // left, but for right side alignment, we have to make some math :)
            $col_start = 0;
            if ('right' == $position) {
                if ($component->is_anchor_target()) {
                    $col_start = $layout_columns - $body_column_span + $alignment_offset;
                } else {
                    $col_start = $body_column_span - $alignment_offset;
                }
            }
            // Find the column span. For the target element, let's use the same
            // column span as the Body component, that is, 5 columns, minus the
            // defined offset. The element to be anchored uses the remaining space.
            $col_span = 0;
            if ($component->is_anchor_target()) {
                $col_span = $body_column_span - $alignment_offset;
            } else {
                $col_span = $layout_columns - $body_column_span + $alignment_offset;
            }
            // Finally, register the layout
            $this->register_layout($layout_name, array('columnStart' => $col_start, 'columnSpan' => $col_span));
        }
        $component->set_json('layout', $layout_name);
    }

Usage Example

 public function testLeftLayoutGetsAdded()
 {
     $layouts = new Component_Layouts($this->content, $this->settings);
     $this->assertFalse(array_key_exists('anchor-layout-left', $layouts->to_array()));
     $component = $this->prophet->prophesize('\\Apple_Exporter\\Components\\Component');
     $component->get_anchor_position()->willReturn(Component::ANCHOR_LEFT)->shouldBeCalled();
     $component->is_anchor_target()->willReturn(false)->shouldBeCalled();
     $component->set_json('layout', 'anchor-layout-left')->shouldBeCalled();
     $layouts->set_anchor_layout_for($component->reveal());
     $this->assertTrue(array_key_exists('anchor-layout-left', $layouts->to_array()));
 }