Need to migrate your PHP legacy system to a new framework quickly and without breaking the bank? Get in touch—I'd be happy to help.
$array = [
new Quote(quote: 'That\'s all any of us are: amateurs. we don\'t live long enough to be anything else.', author: 'Charlie Chaplin'),
'dowj',
215,
false
];
array:4 [▼
0 =>
App\Model
\Quote {#725 ▶}
1 => "dowj"
2 => 215
3 => false
]
foreach ($array as $data){
if($data instanceof Quote){
// do somthing...
}elseif (is_string($data)){
// do somthing...
}elseif (is_int($data)){
// do somthing...
}elseif (is_bool($data)){
// do somthing...
}else{
// do something else
}
}
$someSpecificDataFromArray = [];
foreach ($array as $data){
if(/** do some check */) {
$someSpecificDataFromArray[] = $data['some_key'];
}
}
// do somehting with $someSpecificDataFromArray
$quotes = new ArrayCollection([
new Quote(quote: 'That\'s all any of us are: amateurs. we don\'t live long enough to be anything else.', author: 'Charlie Chaplin'),
new Quote(quote: 'Pull back the curtain on your process', author: 'Ann Friedman'),
new Quote(quote: 'This is the true joy in life, being used for a purpose recognized by yourself as a mighty one. Being a force of nature instead of a feverish, selfish little clod of ailments and grievances, complaining that the world will not devote itself to making you happy. I am of the opinion that my life belongs to the whole community and as long as I live, it is my privilege to do for it what I can. I want to be thoroughly used up when I die, for the harder I work, the more I live. I rejoice in life for its own sake. Life is no brief candle to me. It is a sort of splendid torch which I have got hold of for the moment and I want to make it burn as brightly as possible before handing it on to future generations.', author: 'George Bernard Shaw'),
new Quote(quote: 'Wealth of information creates a poverty of attention', author: 'Herbert Simon'),
]);
$charlieChaplinQuotes = $quotes->filter(fn(Quote $quote) => str_contains( haystack: $quote->getAuthor(), needle: 'Charlie Chaplin' ));
dd($charlieChaplinQuotes);
AppController.php on line 42:
Doctrine\Common\Collections\ArrayCollection {#6877 ▼
-elements: array:1 [▼
0 =>
App\Model
\Quote {#1267 ▼
-author: "Charlie Chaplin"
-quote: "That's all any of us are: amateurs. we don't live long enough to be anything else."
}
]
}
#[ORM\Entity(repositoryClass: ArticleRepository::class)]
class Article
{
....
/**
* @var Collection|ArrayCollection
*/
#[ORM\ManyToMany(targetEntity: Tag::class, inversedBy: 'articles')]
private Collection $tags;
public function __construct()
{
$this->tags = new ArrayCollection();
}
/**
* @return Collection
*/
public function getTags(): Collection
{
return $this->tags;
}
public function addTag(Tag $tag): static
{
if (!$this->tags->contains($tag)) {
$this->tags->add($tag);
}
return $this;
}
public function removeTag(Tag $tag): static
{
$this->tags->removeElement($tag);
return $this;
}
}
if (array_key_exists(key: 'name', array: $composerJsonData)) {
$composer->setName($composerJsonData['name']);
}
public function getPackageByName(string $name): null|Package
{
$packages = new ArrayCollection([
...$this->getRequires(),
...$this->getDevRequires()
]);
return $packages->findFirst(fn(int $key, Package $package) => $name === $package->getName());
}
$composer = (new ParserFacade())->extract();
$package = $composer->getPackageByName('rector/rector');
// object(ComposerJsonParser\Model\PackageVersion)#29 (2) {
// ["version"] => float(0.18)
// ["versionConstraints"] => string(1) "^"
// }