Last active
November 22, 2020 16:30
-
-
Save dustinsgoodman/ea3cf266f164f2586fef907eafdc5a11 to your computer and use it in GitHub Desktop.
How I managed to encounter and recover from Computer Science's Two Hardest Problems Snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace MyObject; | |
use Utils\ArrayUtils as _; // basically lodash | |
class DAO extends \Base\MySQL\DAO { | |
// ... | |
/** | |
* @param int|int[] $relationAId | |
* @param int|int[]|null $relationBId | |
* @param int|int[]|null $relationCId | |
* | |
* @return MyObject|MyObject[] | |
* @throws \Exception | |
*/ | |
public static function getByRelation($relationAId, $relationBId = null, $relationCId = null) { | |
$result = self::getDAOCache()->getByForeignKey(MyObject::$FK_RELATION_A, $relationAId); | |
if (isset($relationBId)) { | |
if (empty($relationBId)) { | |
return []; | |
} | |
$result = _::filter($result, function ($item) use ($relationBId) { | |
return $item->relationBId === $relationBId; | |
}); | |
} else if (isset($relationCId)) { | |
if (empty($relationCId)) { | |
return []; | |
} | |
$result = _::filter($result, function ($item) use ($relationCId) { | |
return $item->relationCId === $relationCId; | |
}); | |
} | |
if ((!$relationBId && !$relationCId) || is_array($relationAId)) { | |
return $result; | |
} | |
if (count($result) > 1) { | |
throw new \Exception("Ambiguous object for relationA $relationAId, relationB $relationBId"); | |
} | |
return $result; | |
} | |
// ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace MyObject; | |
use Utils\ArrayUtils as _; // basically lodash | |
class DAO extends \Base\MySQL\DAO { | |
// ... | |
/** | |
* @param int|int[] $relationAId | |
* @param int|int[]|null $relationBId | |
* @param int|int[]|null $relationCId | |
* | |
* @return MyObject|MyObject[] | |
* @throws \Exception | |
*/ | |
public static function getByRelation($relationAId, $relationBId = null, $relationCId = null) { | |
$result = self::getDAOCache()->getByForeignKey(MyObject::$FK_RELATION_A, $relationAId); | |
if (isset($relationBId)) { | |
if (empty($relationBId)) { | |
return []; | |
} | |
$relationBId = _::castArray($relationBId); | |
$result = _::filter($result, function ($item) use ($relationBId) { | |
return _::contains($relationBId, $item->relationBId); | |
}); | |
} else if (isset($relationCId)) { | |
if (empty($relationCId)) { | |
return []; | |
} | |
$relationCId = _::castArray($relationCId); | |
$result = _::filter($result, function ($item) use ($relationCId) { | |
return _::contains($relationCId , $item->relationCId); | |
}); | |
} | |
if ((!$relationBId && !$relationCId) || is_array($relationAId)) { | |
return $result; | |
} | |
if (count($result) > 1) { | |
throw new \Exception("Ambiguous object for relationA $relationAId, relationB $relationBId"); | |
} | |
return _::first($result); | |
} | |
// ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace MyObject; | |
use Utils\ArrayUtils as _; // basically lodash | |
class DAO extends \Base\MySQL\DAO { | |
// ... | |
/** | |
* @param int|int[] $relationAId | |
* @param int|int[]|null $relationBId | |
* @param int|int[]|null $relationCId | |
* | |
* @return MyObject|MyObject[] | |
* @throws \Exception | |
*/ | |
public static function getByRelation($relationAId, $relationBId = null, $relationCId = null) { | |
if (empty($relationAId)) { | |
return []; | |
} | |
$query = self::select()->where('relationAId', $relationAId); | |
if (isset($relationBId)) { | |
if (empty($relationBId)) { | |
return []; | |
} | |
$query = $query->where('relationBId', $relationBId); | |
} else if (isset($relationCId)) { | |
if (empty($relationCId)) { | |
return []; | |
} | |
$query = $query->where('relationCId', $relationCId); | |
} | |
$result = $query->toObjects(); | |
if ((!$relationBId && !$relationCId) || is_array($relationAId)) { | |
return $result; | |
} | |
if (count($result) > 1) { | |
throw new \Exception("Ambiguous object for relationA $relationAId, relationB $relationBId"); | |
} | |
return reset($result); | |
} | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment