Changeset View
Changeset View
Standalone View
Standalone View
src/app/Console/ObjectRelationListCommand.php
<?php | <?php | ||||
namespace App\Console; | namespace App\Console; | ||||
use Illuminate\Support\Str; | |||||
/** | /** | ||||
* This abstract class provides a means to treat objects in our model using CRUD, with the exception that | * This abstract class provides a means to treat objects in our model using CRUD, with the exception that | ||||
* this particular abstract class lists objects' relations. | * this particular abstract class lists objects' relations. | ||||
*/ | */ | ||||
abstract class ObjectRelationListCommand extends ObjectCommand | abstract class ObjectRelationListCommand extends ObjectCommand | ||||
{ | { | ||||
/** | /** | ||||
* The "relation" -- a method or property. | * The "relation" -- a method or property. | ||||
* | * | ||||
* @var string | * @var string | ||||
*/ | */ | ||||
protected $objectRelation; | protected $objectRelation; | ||||
/** | /** | ||||
* Optional arguments for $objectRelation method | |||||
* | |||||
* @var array | |||||
*/ | |||||
protected $objectRelationArgs = []; | |||||
/** | |||||
* Supplement the base command constructor with a derived or generated signature and | * Supplement the base command constructor with a derived or generated signature and | ||||
* description. | * description. | ||||
* | * | ||||
* @return mixed | * @return mixed | ||||
*/ | */ | ||||
public function __construct() | public function __construct() | ||||
{ | { | ||||
$this->description = "List {$this->objectRelation} for a {$this->objectName}"; | $this->description = "List {$this->objectRelation} for a {$this->objectName}"; | ||||
$this->signature = sprintf( | $this->signature = sprintf( | ||||
"%s%s:%s {%s}", | "%s%s:%s {%s}", | ||||
$this->commandPrefix ? $this->commandPrefix . ":" : "", | $this->commandPrefix ? $this->commandPrefix . ":" : "", | ||||
$this->objectName, | $this->objectName, | ||||
$this->objectRelation, | Str::kebab($this->objectRelation), | ||||
$this->objectName | $this->objectName | ||||
); | ); | ||||
$this->signature .= " {--attr=* : Attributes other than the primary unique key to include}"; | $this->signature .= " {--attr=* : Attributes other than the primary unique key to include}"; | ||||
parent::__construct(); | parent::__construct(); | ||||
} | } | ||||
Show All 13 Lines | |||||
); | ); | ||||
if (!$object) { | if (!$object) { | ||||
$this->error("No such {$this->objectName} {$argument}"); | $this->error("No such {$this->objectName} {$argument}"); | ||||
return 1; | return 1; | ||||
} | } | ||||
if (method_exists($object, $this->objectRelation)) { | if (method_exists($object, $this->objectRelation)) { | ||||
$result = call_user_func([$object, $this->objectRelation]); | $result = call_user_func_array([$object, $this->objectRelation], $this->objectRelationArgs); | ||||
} elseif (property_exists($object, $this->objectRelation)) { | } elseif (property_exists($object, $this->objectRelation)) { | ||||
$result = $object->{"{$this->objectRelation}"}; | $result = $object->{"{$this->objectRelation}"}; | ||||
} else { | } else { | ||||
$this->error("No such relation {$this->objectRelation}"); | $this->error("No such relation {$this->objectRelation}"); | ||||
return 1; | return 1; | ||||
} | } | ||||
// Convert query builder into a collection | // Convert query builder into a collection | ||||
if ($result instanceof \Illuminate\Database\Eloquent\Relations\Relation) { | if ( | ||||
($result instanceof \Illuminate\Database\Eloquent\Relations\Relation) | |||||
|| ($result instanceof \Illuminate\Database\Eloquent\Builder) | |||||
) { | |||||
$result = $result->get(); | $result = $result->get(); | ||||
} | } | ||||
// Print the result | // Print the result | ||||
if ( | if ( | ||||
($result instanceof \Illuminate\Database\Eloquent\Collection) | ($result instanceof \Illuminate\Database\Eloquent\Collection) | ||||
|| is_array($result) | || is_array($result) | ||||
) { | ) { | ||||
foreach ($result as $entry) { | foreach ($result as $entry) { | ||||
$this->info($this->toString($entry)); | $this->info($this->toString($entry)); | ||||
} | } | ||||
} else { | } else { | ||||
$this->info($this->toString($result)); | $this->info($this->toString($result)); | ||||
} | } | ||||
} | } | ||||
} | } |