Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add skip_add param to StaticAddToTrait trait methods #143

Merged
merged 1 commit into from
Mar 16, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 12 additions & 22 deletions src/StaticAddToTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,8 @@ trait StaticAddToTrait
*
* @return static
*/
public static function addTo(object $parent, $seed = [], array $add_args = [])
public static function addTo(object $parent, $seed = [], array $add_args = [], bool $skip_add = false)
{
if (func_num_args() > 3) { // prevent bad usage
throw new \Error('Too many method arguments');
}

if (is_object($seed)) {
$object = $seed;
} else {
Expand All @@ -52,13 +48,13 @@ public static function addTo(object $parent, $seed = [], array $add_args = [])
}
}

return static::_addTo_add($parent, $object, false, $add_args);
return static::_addTo_add($parent, $object, false, $add_args, $skip_add);
}

/**
* @return static
*/
private static function _addTo_add(object $parent, object $object, bool $unsafe, array $add_args)
private static function _addTo_add(object $parent, object $object, bool $unsafe, array $add_args, bool $skip_add = false)
{
// check if object is instance of this class
if (!$unsafe && !($object instanceof static)) {
Expand All @@ -68,7 +64,9 @@ private static function _addTo_add(object $parent, object $object, bool $unsafe,
}

// add to parent
$parent->add($object, ...$add_args);
if (!$skip_add) {
$parent->add($object, ...$add_args);
}

return $object;
}
Expand All @@ -80,13 +78,9 @@ private static function _addTo_add(object $parent, object $object, bool $unsafe,
*
* @return static
*/
public static function addToWithClassName(object $parent, $seed = [], array $add_args = [])
public static function addToWithClassName(object $parent, $seed = [], array $add_args = [], bool $skip_add = false)
{
if (func_num_args() > 3) { // prevent bad usage
throw new \Error('Too many method arguments');
}

return static::_addToWithClassName($parent, $seed, false, $add_args);
return static::_addToWithClassName($parent, $seed, false, $add_args, $skip_add);
}

/**
Expand All @@ -96,19 +90,15 @@ public static function addToWithClassName(object $parent, $seed = [], array $add
*
* @return static
*/
public static function addToWithClassNameUnsafe(object $parent, $seed = [], array $add_args = [])
public static function addToWithClassNameUnsafe(object $parent, $seed = [], array $add_args = [], bool $skip_add = false)
{
if (func_num_args() > 3) { // prevent bad usage
throw new \Error('Too many method arguments');
}

return static::_addToWithClassName($parent, $seed, true, $add_args);
return static::_addToWithClassName($parent, $seed, true, $add_args, $skip_add);
}

/**
* @return static
*/
private static function _addToWithClassName(object $parent, $seed, bool $unsafe, array $add_args)
private static function _addToWithClassName(object $parent, $seed, bool $unsafe, array $add_args, bool $skip_add = false)
{
if (is_object($seed)) {
$object = $seed;
Expand All @@ -131,6 +121,6 @@ private static function _addToWithClassName(object $parent, $seed, bool $unsafe,
}
}

return static::_addTo_add($parent, $object, $unsafe, $add_args);
return static::_addTo_add($parent, $object, $unsafe, $add_args, $skip_add);
}
}