Skip to content

Using constants for field positions #51

Description

@pluk77

Would you be open to receiving a PR for adding constants to the segments (and perhaps drop the $position argument completely)? Hard-coding the same value in setter and getters seems a bit odd and by using constants you have additional benefits in your IDE and documentation.

For simple segments, one could even completely bypass the getter and setters without the risk of using the wrong position:

$orc = new ORC();
$orc->setField(ORC::ORDER_CONTROL, $value);

I propose to change the segments like this:

class ORC extends Segment
{
    const ORDER_CONTROL = 1;
    const PLACE_ORDER_NUMBER = 2;
    
    public function __construct(array $fields = null)
    {
        parent::__construct('ORC', $fields);
    }

    public function setOrderControl($value)
    {
        return $this->setField(self::ORDER_CONTROL, $value);
    }

    public function setPlacerOrderNumber($value)
    {
        return $this->setField(self::PLACE_ORDER_NUMBER, $value);
    }

    public function getOrderControl()
    {
        return $this->getField(self::ORDER_CONTROL);
    }

    public function getPlacerOrderNumber()
    {
        return $this->getField(self::PLACE_ORDER_NUMBER);
    }
}

or like this

class ORC extends Segment
{
    const ORDER_CONTROL = 1;
    const PLACE_ORDER_NUMBER = 2;
    
    public function __construct(array $fields = null)
    {
        parent::__construct('ORC', $fields);
    }

    public function setOrderControl($value, int $position = self::ORDER_CONTROL)
    {
        return $this->setField($position, $value);
    }

    public function setPlacerOrderNumber($value, int $position = self::PLACE_ORDER_NUMBER)
    {
        return $this->setField($position, $value);
    }

    public function getOrderControl(int $position = self::ORDER_CONTROL)
    {
        return $this->getField($position);
    }

    public function getPlacerOrderNumber(int $position = self::PLACE_ORDER_NUMBER)
    {
        return $this->getField($position);
    }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions